Skip to content

perf: cache FindExternalExpression results, including misses - #120

Open
koenbeuk wants to merge 1 commit into
mainfrom
perf/cache-external-expression-misses
Open

koenbeuk wants to merge 1 commit into
mainfrom
perf/cache-external-expression-misses

Conversation

@koenbeuk

Copy link
Copy Markdown
Collaborator

Fixes #113

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical null-value caching bug causes first-time plain-member lookups to throw instead of returning null, leaving the miss-caching behavior incorrect and its test failing.

1 open issue
What changed in this PR

This PR improves query-expansion performance by caching external [ExpressiveFor] lookups, including misses, and replacing repeated assembly-count scans with generation-based invalidation.

Changes:

  • Added external-expression caching and assembly-load invalidation.
  • Updated polymorphic plan caching to use the resolver’s registry generation.
  • Added cache behavior tests and a benchmark covering plain-member lookups.
File Description
tests/​ExpressiveSharp.Tests/​Services/​ExpressiveResolverExternalCacheTests.cs Adds tests for external-expression hit and miss caching, including cache invalidation behavior.
src/​ExpressiveSharp/​Services/​ExpressiveResolver.cs Adds external lookup caching, registry-generation tracking, and incremental assembly scanning.
src/​ExpressiveSharp/​Services/​ExpressiveReplacer.cs Updates polymorphic plan invalidation to use the resolver’s registry generation.
benchmarks/​ExpressiveSharp.Benchmarks/​ExpressionReplacerBenchmarks.cs Adds a benchmark for expression replacement involving plain-member accesses.

Review effort: Lite

Comment on lines +163 to +175
if (_externalExpressionCache.TryGetValue(memberInfo, out var cached))
return cached;

var generation = Volatile.Read(ref _registryGeneration);
var found = FindExternalExpressionCore(memberInfo);

// A scan that completed meanwhile may have added a registry this lookup did not see.
if (generation == Volatile.Read(ref _registryGeneration))
{
_externalExpressionCache.TryAdd(memberInfo, found);
if (generation != Volatile.Read(ref _registryGeneration))
_externalExpressionCache.TryRemove(memberInfo, out _);
}
@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.79592% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/ExpressiveSharp/Services/ExpressiveResolver.cs 88.63% 3 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FindExternalExpression misses are never cached: query expansion calls AppDomain.GetAssemblies() for every plain member, on every execution

2 participants