Stream: dotnet
Topic: serialization performance
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...
Brian Postlethwaite (Oct 30 2019 at 23:30):
The string or stream serializer?
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.
Brian Postlethwaite (Oct 30 2019 at 23:32):
On a single instance not so significant, but for a server it all adds up.
Christiaan Knaap (Oct 31 2019 at 13:20):
Are you serializing from a POCO or an ITypedElement?
Michele Mottini (Oct 31 2019 at 13:57):
Serializing from a POCO to string
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);
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.
Michele Mottini (Oct 31 2019 at 20:45):
Not the first one - first one takes multiple seconds
Michele Mottini (Oct 31 2019 at 21:21):
3 msec to serialize 13 KB is not thumbs up...
Brian Postlethwaite (Nov 01 2019 at 11:14):
I agree with that for sure. Was confirming what you're seeing.
Michele Mottini (Nov 01 2019 at 13:47):
Thanks Brian - I wanted confirmation that was not something specific to our fork of the library
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
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
Michele Mottini (Nov 08 2019 at 15:07):
VP Herisse (Nov 08 2019 at 15:45):
That seems better :)
Brian Postlethwaite (Nov 10 2019 at 20:37):
I did a similar experiment with the xmlwriter a while ago too.
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.
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.
Brian Postlethwaite (Nov 10 2019 at 20:40):
And while we're here, could do it with the new netcore 30 json handling.
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
Brian Postlethwaite (Feb 21 2020 at 11:30):
Mind if I grabbed this to have an alternative serializers for the core packages?
Michele Mottini (Feb 21 2020 at 21:13):
Not at all
Last updated: Apr 12 2022 at 19:14 UTC