FHIR Chat · serialization performance · dotnet

Stream: dotnet

Topic: serialization performance


view this post on Zulip Michele Mottini (Oct 30 2019 at 23:25):

I am doing some performance testing and it takes 2 / 3 msec to serialize a 13 KB bundle containing 3 patients - that seems pretty slow. Is this the expected performance? Or maybe I am doing something wrong...

view this post on Zulip Brian Postlethwaite (Oct 30 2019 at 23:30):

The string or stream serializer?

view this post on Zulip Brian Postlethwaite (Oct 30 2019 at 23:32):

In my server I've gone to the stream serializer to reduce string allocations and garbage collection.

view this post on Zulip Brian Postlethwaite (Oct 30 2019 at 23:32):

On a single instance not so significant, but for a server it all adds up.

view this post on Zulip Christiaan Knaap (Oct 31 2019 at 13:20):

Are you serializing from a POCO or an ITypedElement?

view this post on Zulip Michele Mottini (Oct 31 2019 at 13:57):

Serializing from a POCO to string

view this post on Zulip Michele Mottini (Oct 31 2019 at 13:59):

            var xml = File.ReadAllText(@"c:\temp\bundle.xml");

            var xmlParser = new FhirSerialization.FhirXmlParser(FhirModel.Version.DSTU2);
            var bundle = xmlParser.Parse<FhirModel2.Bundle>(xml);
            var watch = Stopwatch.StartNew();
            bundle = xmlParser.Parse<FhirModel2.Bundle>(xml);
            watch.Stop();
            Console.WriteLine("Parse: {0}ms", watch.ElapsedMilliseconds);

            var xmlSerializer = new FhirSerialization.FhirXmlSerializer(FhirModel.Version.DSTU2);
            var bundleXml = xmlSerializer.SerializeToString(bundle);
            watch.Restart();
            const int count = 10;
            for (var i=0; i<count; i++)
            {
                xmlSerializer = new FhirSerialization.FhirXmlSerializer(FhirModel.Version.DSTU2);
                bundleXml = xmlSerializer.SerializeToString(bundle);
            }
            watch.Stop();
            Console.WriteLine("Serialize x {1}: {0}ms", watch.ElapsedMilliseconds, count);

view this post on Zulip Brian Postlethwaite (Oct 31 2019 at 20:04):

Also assuming this is not the first time when it leads in all the reflection stuff?
The first one is always considerably longer.
Looking at the code I think it's outside your timer.

view this post on Zulip Michele Mottini (Oct 31 2019 at 20:45):

Not the first one - first one takes multiple seconds

view this post on Zulip Michele Mottini (Oct 31 2019 at 21:21):

3 msec to serialize 13 KB is not thumbs up...

view this post on Zulip Brian Postlethwaite (Nov 01 2019 at 11:14):

I agree with that for sure. Was confirming what you're seeing.

view this post on Zulip Michele Mottini (Nov 01 2019 at 13:47):

Thanks Brian - I wanted confirmation that was not something specific to our fork of the library

view this post on Zulip Michele Mottini (Nov 08 2019 at 03:46):

I did an experiment of writing a trivial serializer for a bundle of patients to get a baseline speed:

Serialize X 1,000: 3,275.0ms
New serialize X 1,000: 123.0ms

view this post on Zulip Michele Mottini (Nov 08 2019 at 03:49):

Code is something like this:

        public void Serialize(JsonWriter writer)
        {
            writer.WriteStartObject();
            writer.WritePropertyName("resourceType"); writer.WriteValue("Bundle");
            writer.WritePropertyName("type"); writer.WriteValue(TypeElement.ObjectValue);
            writer.WritePropertyName("total"); writer.WriteValue(TotalElement.ObjectValue);
            writer.WritePropertyName("link");
            writer.WriteStartArray();
            foreach(var link in Link)
            {
                link.Serialize(writer);
            }
            writer.WriteEndArray();
            writer.WritePropertyName("entry");
            writer.WriteStartArray();
            foreach (var entry in Entry)
            {
                entry.Serialize(writer);
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
        }

and so on down for the various classes

view this post on Zulip Michele Mottini (Nov 08 2019 at 15:07):

https://github.com/CareEvolution/fhir-net-api/compare/multiversions...CareEvolution:serializationexperiment

view this post on Zulip VP Herisse (Nov 08 2019 at 15:45):

That seems better :)

view this post on Zulip Brian Postlethwaite (Nov 10 2019 at 20:37):

I did a similar experiment with the xmlwriter a while ago too.

view this post on Zulip Brian Postlethwaite (Nov 10 2019 at 20:38):

Have been considering doing a code generated version of these as a separate assembly too, providing extension methods.

view this post on Zulip Brian Postlethwaite (Nov 10 2019 at 20:39):

What you're missing is all the extensions processing too, but that's not going to break anything in terms of raw speed.

view this post on Zulip Brian Postlethwaite (Nov 10 2019 at 20:40):

And while we're here, could do it with the new netcore 30 json handling.

view this post on Zulip Michele Mottini (Nov 23 2019 at 22:16):

I implemented complete JSON and XML fast serialization: https://github.com/CareEvolution/fhir-net-api/pull/1

view this post on Zulip Brian Postlethwaite (Feb 21 2020 at 11:30):

Mind if I grabbed this to have an alternative serializers for the core packages?

view this post on Zulip Michele Mottini (Feb 21 2020 at 21:13):

Not at all


Last updated: Apr 12 2022 at 19:14 UTC