hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2.03k stars 1.33k forks source link

Extreme validation slowdown due to changed primitive type check implementation #6252

Closed Boereck closed 3 weeks ago

Boereck commented 2 months ago

Describe the bug We noticed an extreme slowdown of resource validation in our project when switching to HAPI 7.4.0. Instead of minutes the validation takes hours. We profiled the application and found out that almost 100% of the time was spent in VersionSpecificWorkerContextWrapper#isPrimitiveType(String). The code is always traversing all StructureDefinitions to check if any type that is primitive has a matching name.

To Reproduce Validate a resource with lots of StructureDefinitions available to the ValidationContext, triggering isPrimitive call. The simplest is a CodeSystem, since constraint csd-1 triggers a lot of those calls when evaluating the isDistinct function.

Expected behavior The expected behavior would be a reasonable evaluation time of isPrimitive(String).
We would suggest to add a field of type Set<String> of names of primitive types to VersionSpecificWorkerContextWrapper to cache the names of all primitive types, similar to how it is done for all known StructureDefinitions already.

If it is wanted, we can work on a PR implementing the suggested solution.

Environment (please complete the following information):

jamesagnew commented 2 months ago

Thanks for the submission! That does sound like an issue. For what it's worth, a PR implementing the fix you propose would be gladly accepted.

jamesagnew commented 2 months ago

Possibly related to #6091