perf(codegen): avoid runtime provider scanning - #10566
Merged
ReubenBond merged 1 commit intoAug 12, 2026
Merged
ReubenBond merged 1 commit into
ReubenBond merged 1 commit into
Conversation
Register generated provider metadata through module initializers and resolve it without assembly enumeration. Keep legacy binaries working through a lazy, one-time reflection fallback and isolate registry state by assembly load context. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Orleans’ configuration-driven provider resolution by avoiding eager runtime assembly enumeration/reflection when provider metadata was already computed by the Orleans source generator. It introduces an AssemblyLoadContext-scoped registry populated via generated module initializers, and updates client/silo configuration to consult that registry first while retaining a lazy, cached legacy reflection fallback for older binaries.
Changes:
- Added
ProviderMetadataRegistryto store generator-emitted provider metadata perAssemblyLoadContext, with deterministic duplicate handling and unload cleanup for collectible contexts. - Updated
ProviderRegistrationResolverto read from the registry on the fast path, refresh on misses (for newly loaded generated assemblies), and only then trigger a one-time legacy scan. - Extended unit tests to cover the registry fast-path behavior, fallback initialization semantics, deterministic duplicate resolution under concurrency, and collectible
AssemblyLoadContextunload behavior; updated generator tests to validate module initializer emission.
Show a summary per file
| File | Description |
|---|---|
| test/Orleans.Core.Tests/ProviderRegistrationResolverTests.cs | Adds tests for registry fast path, lazy fallback behavior, deterministic duplicates under concurrency, and collectible ALC unload behavior. |
| test/Orleans.CodeGenerator.Tests/OrleansSourceGeneratorTests.cs | Verifies generated provider metadata emits a compilable module initializer which registers into the runtime registry. |
| test/Orleans.CodeGenerator.Tests/IncrementalCachingTests.cs | Ensures incremental output changes include module initializer emission only when provider metadata appears. |
| src/Orleans.Serialization/Orleans.Serialization.csproj | Grants InternalsVisibleTo access needed for Orleans.Core and test projects to call internal registry read APIs. |
| src/Orleans.Serialization/Configuration/ProviderMetadataRegistry.cs | Introduces the ALC-scoped runtime registry and unload cleanup to avoid rooting collectible contexts. |
| src/Orleans.Runtime/Hosting/DefaultSiloServices.cs | Switches silo provider resolution to use ProviderRegistrationResolver.Default registry-first path. |
| src/Orleans.Core/Core/ProviderRegistrationResolver.cs | Refactors provider resolution to registry-first with miss refresh + one-time legacy reflection fallback. |
| src/Orleans.Core/Core/DefaultClientServices.cs | Switches client provider resolution to use ProviderRegistrationResolver.Default registry-first path. |
| src/Orleans.CodeGenerator/OrleansSourceGenerator.cs | Detects module-initializer support and passes the flag into metadata generation. |
| src/Orleans.CodeGenerator/MetadataSourceOutputGenerator.cs | Plumbs module-initializer support into MetadataGenerator. |
| src/Orleans.CodeGenerator/MetadataGenerator.cs | Emits a [ModuleInitializer] method to register generated provider metadata when supported and present. |
| src/api/Orleans.Serialization/Orleans.Serialization.cs | Updates the public API surface to include ProviderMetadataRegistry. |
Review details
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 12/12 changed files
- Comments generated: 0
- Review effort level: Lite
ReubenBond
deleted the
rb-issue-10504-avoid-runtime-provider-scanning-with-gen-0ddaa6
branch
August 12, 2026 18:18
This was referenced Aug 28, 2026
Merged
Merged
This was referenced Sep 4, 2026
This was referenced Sep 7, 2026
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
Configuration-driven client and silo providers still require eager assembly enumeration and reflection to find generated metadata, even when the current source generator has already resolved the complete provider set.
Solution
Emit a guarded module initializer whenever generated provider metadata exists and register that metadata in an AssemblyLoadContext-scoped runtime registry. Client and silo configuration now use the registry directly, refresh it on a miss for newly loaded generated assemblies, and only then initialize a shared one-time legacy reflection fallback.
The registry defines deterministic duplicate precedence, removes collectible-context registrations during unload, preserves generated-over-legacy precedence, and keeps custom type-manifest providers out of discovery.
Rationale
This keeps reflection and runtime assembly scanning off the successful generated path while retaining compatibility with assemblies built by older Orleans generators and preserving useful missing-provider diagnostics.
Fixes #10504
Microsoft Reviewers: Open in CodeFlow