You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When I deserialize a large (1-4MB) Bundle resource the performance goes down 10-20x times. A 4MB Bundle can take up to 400ms to deserialize.
If I instead used dynamic object instead of Bundle, the performance is closer to 11ms for the same 4MB Bundle.
To Reproduce
Steps to reproduce the behavior:
Load a 1+ MB json file, for example the json definitions as they are pretty big Fhir R4 definitions. I used the valuesets.json which is around 9.3 million characters.
Deserialize it multiple times using Bundle -> get 400ms or more per object
Deserialize it multiple times using object -> get around 11ms per object
Expected behavior
The deserializer should not lose 95%+ of it's performance just because I have set the type.
Code
The code also prewarms the Bundle deserialization as otherwise it can go up to 1.5 seconds. All tests were done on a AMD 5900x CPU, 64GB 3600MHz RAM, and Windows 11. The file is loaded only once in memory.
usingSystem.Text.Json;usingHl7.Fhir.Model;usingHl7.Fhir.Serialization;classProgram{publicstaticvoidFhirR4SerializerTest(intruns,stringfilename){Console.WriteLine("Running C# FHIR SDK benchmark!");stringsource=File.ReadAllText(filename);varparser=newFhirJsonParser();for(inti=0;i<20;i++){Bundlebundle=parser.Parse<Bundle>(source);}DateTimedateTimeStart=DateTime.Now;for(inti=0;i<runs;i++){Bundlebundle=parser.Parse<Bundle>(source);}DateTimedateTimeFinish=DateTime.Now;vartotalTimeTake=dateTimeFinish-dateTimeStart;Console.WriteLine($"Fhir SDK miliseconds taken: {totalTimeTake.TotalMilliseconds}");}publicstaticvoidFhirR4SerializerTestv2(intruns,stringfilename){Console.WriteLine("Running C# FHIR SDK system.text benchmark!");stringsource=File.ReadAllText(filename);varparser=newFhirJsonParser();varoptions=newJsonSerializerOptions().ForFhir(ModelInfo.ModelInspector);for(inti=0;i<20;i++){Bundlebundle=JsonSerializer.Deserialize<Bundle>(source,options);}DateTimedateTimeStart=DateTime.Now;for(inti=0;i<runs;i++){Bundlebundle=JsonSerializer.Deserialize<Bundle>(source,options);}DateTimedateTimeFinish=DateTime.Now;vartotalTimeTake=dateTimeFinish-dateTimeStart;Console.WriteLine($"Fhir SDK system.text miliseconds taken: {totalTimeTake.TotalMilliseconds}");}publicstaticvoidFhirObjectSerializerTest(intruns,stringfilename){Console.WriteLine("Running C# FHIR object benchmark!");stringsource=File.ReadAllText(filename);for(inti=0;i<20;i++){objectbundle=JsonSerializer.Deserialize<object>(source);}DateTimedateTimeStart=DateTime.Now;for(inti=0;i<runs;i++){objectbundle=JsonSerializer.Deserialize<object>(source);}DateTimedateTimeFinish=DateTime.Now;vartotalTimeTake=dateTimeFinish-dateTimeStart;Console.WriteLine($"Fhir object miliseconds taken: {totalTimeTake.TotalMilliseconds}");}staticvoidMain(string[]args){Console.WriteLine("Running C# benchmark!");intruns=100;stringfilename="bundle_file.json";// Replace with the name of your fileFhirR4SerializerTest(runs,filename);FhirObjectSerializerTest(runs,filename);FhirR4SerializerTestv2(runs,filename);}}
Thanks for reaching out.
The tests you've attached show indeed a big difference in performance, however FhirObjectSerializerTest doesn't only differ in the fact that you've set a type compared to FhirR4SerializerTestv2.
In the FhirObjectSerializerTest you do not use options (new JsonSerializerOptions().ForFhir(ModelInfo.ModelInspector)), which contains all the FHIR logic of the Deserializer (all the parsing from string to DateTime, Int, Bool, etc. But also all the FHIR extension logic, validation, and other checks. This takes time. So it's not a fair comparison.
The way you test now, you just get a dictionary returned that corresponds to the Json file, it doesn't parse to our FHIR Poco models.
You can still improve the time of deserialization by turning off the validator in the settings (set it to null) if you want, but it will never come close to parsing json to a dictionary without any FHIR logic.
If you have concrete ideas on how to speed up serializer further, please let us know.
Describe the bug
When I deserialize a large (1-4MB) Bundle resource the performance goes down 10-20x times. A 4MB Bundle can take up to 400ms to deserialize.
If I instead used dynamic object instead of Bundle, the performance is closer to 11ms for the same 4MB Bundle.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The deserializer should not lose 95%+ of it's performance just because I have set the type.
Code
The code also prewarms the Bundle deserialization as otherwise it can go up to 1.5 seconds. All tests were done on a AMD 5900x CPU, 64GB 3600MHz RAM, and Windows 11. The file is loaded only once in memory.
Results
With custom Bundle that we use at work:
With valuesets.json:
Version used:
The text was updated successfully, but these errors were encountered: