-
Notifications
You must be signed in to change notification settings - Fork 352
Fix FhirPath performance regression by optimizing cache cleanup #3219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
5
commits into
develop
Choose a base branch
from
copilot/fix-3174
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d72a88a
Initial plan
Copilot 5a82b6d
Fix FhirPath performance regression by optimizing cache cleanup
Copilot 4cc5810
Merge branch 'develop' into copilot/fix-3174
ewoutkramer aeaebc3
Revert cache optimizations and establish performance baseline
Copilot 67722ca
Final analysis: Cache optimizations don't solve real performance bott…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathCachePerformanceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Hl7.Fhir.ElementModel; | ||
using Hl7.Fhir.Model; | ||
using Hl7.Fhir.FhirPath; | ||
using Hl7.FhirPath; | ||
|
||
namespace Hl7.Fhir | ||
{ | ||
[TestClass] | ||
public class FhirPathCachePerformanceTests | ||
{ | ||
[TestMethod] | ||
public void FhirPathCachePerformanceRegression() | ||
{ | ||
// Create a test patient for FhirPath evaluation | ||
var patient = new Patient | ||
{ | ||
Id = "test-patient", | ||
Name = { new HumanName { Family = "Doe", Given = new[] { "John" } } }, | ||
BirthDate = "1990-01-01", | ||
Gender = AdministrativeGender.Male | ||
}; | ||
|
||
// Test expressions that would be commonly cached | ||
var expressions = new[] | ||
{ | ||
"name.family", | ||
"name.given.first()", | ||
"birthDate", | ||
"gender", | ||
"name.where(family = 'Doe').given", | ||
"extension.where(url = 'test').value", | ||
"id", | ||
"name.given.last()" | ||
}; | ||
|
||
var cache = new FhirPathCompilerCache(); | ||
var typedElement = patient.ToTypedElement(); | ||
|
||
// Warmup - this will populate the cache with the expressions | ||
foreach (var expr in expressions) | ||
{ | ||
cache.Select(typedElement, expr); | ||
} | ||
|
||
// Now measure performance of cached evaluations | ||
var sw = Stopwatch.StartNew(); | ||
int iterations = 10000; | ||
|
||
for (int i = 0; i < iterations; i++) | ||
{ | ||
foreach (var expr in expressions) | ||
{ | ||
var result = cache.Select(typedElement, expr); | ||
// Consume the result to ensure evaluation happens | ||
foreach (var item in result) { } | ||
} | ||
} | ||
|
||
sw.Stop(); | ||
|
||
Console.WriteLine($"FhirPath cache performance: {sw.ElapsedMilliseconds}ms for {iterations * expressions.Length} cached evaluations"); | ||
Console.WriteLine($"Average time per evaluation: {(double)sw.ElapsedMilliseconds / (iterations * expressions.Length):F3}ms"); | ||
|
||
// The test should complete in reasonable time even with many iterations | ||
// This is not an exact performance test but ensures we don't have major regressions | ||
Assert.IsTrue(sw.ElapsedMilliseconds < 5000, | ||
$"FhirPath cache performance regression detected. Took {sw.ElapsedMilliseconds}ms for {iterations * expressions.Length} evaluations"); | ||
} | ||
|
||
[TestMethod] | ||
public void FhirPathCacheEvictionPerformance() | ||
{ | ||
var cache = new FhirPathCompilerCache(cacheSize: 100); // Small cache to trigger eviction | ||
var patient = new Patient { Id = "test" }; | ||
var typedElement = patient.ToTypedElement(); | ||
|
||
var sw = Stopwatch.StartNew(); | ||
|
||
// Create many unique expressions to trigger cache eviction | ||
for (int i = 0; i < 500; i++) // More than cache size to trigger cleanup | ||
{ | ||
var expr = $"id.where(value = 'test{i}')"; | ||
cache.Select(typedElement, expr); | ||
} | ||
|
||
sw.Stop(); | ||
|
||
Console.WriteLine($"Cache eviction test: {sw.ElapsedMilliseconds}ms for 500 unique expressions"); | ||
|
||
// Should handle cache eviction efficiently | ||
Assert.IsTrue(sw.ElapsedMilliseconds < 2000, | ||
$"Cache eviction performance issue detected. Took {sw.ElapsedMilliseconds}ms"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What allocation overhead? The LastAccessed property is DateTimeOffset, so it will be a struct and updating it's value will just change value in memory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely correct. DateTimeOffset is a struct, so updating it doesn't cause heap allocation. My comment was misleading. The real performance cost comes from calling DateTimeOffset.Now on every cache access, which involves system calls and has CPU overhead, not memory allocation overhead. I'll clarify this and focus on finding the actual performance bottleneck.