Repository navigation
perf: cache interface identities and serialization dependencies #11467
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
Merged
ReubenBond
merged 7 commits into
dotnet:main
from
ReubenBond:rb-interface-and-proxy-caching
Oct 11, 2026
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86f3584
perf(core): cache interface identities and proxy copier dependencies
ReubenBond 79ee871
perf(serialization): cache concrete copier dependencies
ReubenBond cc04ab8
refactor(serialization): generalize provider dependency caching
ReubenBond dfaa943
perf(serialization): avoid warm dependency resolution allocations
ReubenBond ebe6f5d
docs: clarify cached provider and registration contracts
ReubenBond dd4c09e
docs: remove caching implementation-page additions
ReubenBond 2c54bd8
fix(serialization): preserve pending interface dependency routing
ReubenBond 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
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
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
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
44 changes: 44 additions & 0 deletions
44
test/Orleans.CodeGenerator.Tests/ProxyCopierResolutionTests.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,44 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Xunit; | ||
|
|
||
| namespace Orleans.CodeGenerator.Tests; | ||
|
|
||
| public sealed class ProxyCopierResolutionTests | ||
| { | ||
| [Fact] | ||
| public async Task ProxyCopierFieldsPreserveConcreteTypesAndCallSites() | ||
| { | ||
| const string source = """ | ||
| using Orleans; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| namespace TestProject; | ||
| [GenerateSerializer] | ||
| public sealed class Payload | ||
| { | ||
| [Id(0)] public int Value { get; set; } | ||
| } | ||
| public interface IProxyGrain<T> : IGrainWithStringKey | ||
| { | ||
| ValueTask Send(Payload value, List<Payload> list, Payload[] array, T generic); | ||
| } | ||
| """; | ||
| var compilation = await TestCompilationHelper.CreateCompilation(source); | ||
| GeneratorDriver driver = CSharpGeneratorDriver.Create(new OrleansSerializationSourceGenerator().AsSourceGenerator()); | ||
| driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out var updated, out var diagnostics, cancellationToken: TestContext.Current.CancellationToken); | ||
| Assert.Empty(diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error)); | ||
| Assert.Empty(updated.GetDiagnostics(TestContext.Current.CancellationToken).Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error)); | ||
| var proxy = driver.GetRunResult().GeneratedTrees.SelectMany(tree => tree.GetRoot(TestContext.Current.CancellationToken).DescendantNodes()) | ||
| .OfType<ClassDeclarationSyntax>().Single(type => type.Identifier.ValueText == "Proxy_IProxyGrain"); | ||
| var text = proxy.NormalizeWhitespace().ToFullString(); | ||
| Assert.Contains("private readonly OrleansCodeGen.TestProject.Copier_Payload", text); | ||
| Assert.Contains("private readonly global::Orleans.Serialization.Codecs.ListCopier<global::TestProject.Payload>", text); | ||
| Assert.Contains("private readonly global::Orleans.Serialization.Codecs.ArrayCopier<global::TestProject.Payload>", text); | ||
| Assert.Contains("IDeepCopier<T>", text); | ||
| Assert.Contains("GetService<OrleansCodeGen.TestProject.Copier_Payload>(this, CodecProvider)", text); | ||
| Assert.Contains("GetService<global::Orleans.Serialization.Cloning.IDeepCopier<T>>(this, CodecProvider)", text); | ||
| Assert.DoesNotContain("CodecProvider.GetDeepCopier<", text); | ||
| } | ||
| } |
138 changes: 138 additions & 0 deletions
138
test/Orleans.Core.Tests/Manifest/GrainInterfaceTypeResolverTests.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,138 @@ | ||
| using Microsoft.Extensions.Options; | ||
| using Orleans.Metadata; | ||
| using Orleans.Runtime; | ||
| using Orleans.Serialization; | ||
| using Orleans.Serialization.Configuration; | ||
| using Orleans.Serialization.TypeSystem; | ||
| using Xunit; | ||
|
|
||
| namespace UnitTests.Manifest; | ||
|
|
||
| [TestSuite("BVT")] | ||
| [TestProvider("None")] | ||
| [TestArea("Runtime")] | ||
| public sealed class GrainInterfaceTypeResolverTests(ITestOutputHelper output) | ||
| { | ||
| [Fact] | ||
| public void WarmConventionLookupDoesNotAllocate() | ||
| { | ||
| var resolver = CreateResolver(); | ||
| var type = typeof(ITestInterface<List<int>>); | ||
| var expected = resolver.GetGrainInterfaceType(type); | ||
| for (var i = 0; i < 100; i++) | ||
| { | ||
| _ = resolver.GetGrainInterfaceType(type); | ||
| } | ||
|
|
||
| var before = GC.GetAllocatedBytesForCurrentThread(); | ||
| for (var i = 0; i < 1000; i++) | ||
| { | ||
| _ = resolver.GetGrainInterfaceType(type); | ||
| } | ||
|
|
||
| var cachedBytes = GC.GetAllocatedBytesForCurrentThread() - before; | ||
| before = GC.GetAllocatedBytesForCurrentThread(); | ||
| for (var i = 0; i < 1000; i++) | ||
| { | ||
| _ = resolver.GetGrainInterfaceTypeByConvention(type); | ||
| } | ||
|
|
||
| var conventionBytes = GC.GetAllocatedBytesForCurrentThread() - before; | ||
| output.WriteLine($"1000 lookups: cached={cachedBytes} bytes; uncached convention={conventionBytes} bytes"); | ||
| Assert.Equal(0, cachedBytes); | ||
| Assert.True(conventionBytes > 0); | ||
| Assert.Equal(expected, resolver.GetGrainInterfaceTypeByConvention(type)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConfiguredIdentityIsCachedWithinItsResolver() | ||
| { | ||
| var firstProvider = new IdentityProvider("first`1"); | ||
| var secondProvider = new IdentityProvider("second`1"); | ||
| var first = CreateResolver(firstProvider); | ||
| var second = CreateResolver(secondProvider); | ||
| var type = typeof(ITestInterface<int>); | ||
| var firstIdentity = first.GetGrainInterfaceType(type); | ||
| var secondIdentity = second.GetGrainInterfaceType(type); | ||
| Assert.NotEqual(firstIdentity, secondIdentity); | ||
| for (var i = 0; i < 1000; i++) | ||
| { | ||
| Assert.Equal(firstIdentity, first.GetGrainInterfaceType(type)); | ||
| Assert.Equal(secondIdentity, second.GetGrainInterfaceType(type)); | ||
| } | ||
|
|
||
| Assert.Equal(1, firstProvider.Calls); | ||
| Assert.Equal(1, secondProvider.Calls); | ||
| Assert.True(GenericGrainInterfaceType.TryParse(firstIdentity, out var generic)); | ||
| Assert.Equal([typeof(int)], generic.GetArguments(CreateConverter())); | ||
| var stringIdentity = first.GetGrainInterfaceType(typeof(ITestInterface<string>)); | ||
| Assert.NotEqual(firstIdentity, stringIdentity); | ||
| Assert.True(GenericGrainInterfaceType.TryParse(stringIdentity, out generic)); | ||
| Assert.Equal([typeof(string)], generic.GetArguments(CreateConverter())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ProviderPrecedenceDoesNotChangeConventionLookup() | ||
| { | ||
| var preferred = new IdentityProvider("preferred`1"); | ||
| var fallback = new IdentityProvider("fallback`1"); | ||
| var resolver = CreateResolver(preferred, fallback); | ||
| var type = typeof(ITestInterface<>); | ||
| Assert.Equal(GrainInterfaceType.Create("preferred`1"), resolver.GetGrainInterfaceType(type)); | ||
| Assert.NotEqual(resolver.GetGrainInterfaceType(type), resolver.GetGrainInterfaceTypeByConvention(type)); | ||
| Assert.Equal(1, preferred.Calls); | ||
| Assert.Equal(0, fallback.Calls); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FailedResolutionCanBeRetriedAndInvalidTypesAreNotCached() | ||
| { | ||
| var provider = new IdentityProvider("retry") { Fail = true }; | ||
| var resolver = CreateResolver(provider); | ||
| Assert.Throws<InvalidOperationException>(() => resolver.GetGrainInterfaceType(typeof(ITestInterface<int>))); | ||
| provider.Fail = false; | ||
| var expected = resolver.GetGrainInterfaceType(typeof(ITestInterface<int>)); | ||
| Assert.Equal(expected, resolver.GetGrainInterfaceType(typeof(ITestInterface<int>))); | ||
| Assert.Equal(2, provider.Calls); | ||
| Assert.Throws<ArgumentException>(() => resolver.GetGrainInterfaceType(typeof(int))); | ||
| Assert.Throws<ArgumentException>(() => resolver.GetGrainInterfaceType(typeof(int))); | ||
| Assert.Equal(2, provider.Calls); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ConcurrentLookupsPublishTheSameIdentity() | ||
| { | ||
| var resolver = CreateResolver(); | ||
| var results = await Task.WhenAll(Enumerable.Range(0, 32).Select(_ => Task.Run( | ||
| () => resolver.GetGrainInterfaceType(typeof(ITestInterface<List<int>>)), TestContext.Current.CancellationToken))); | ||
| Assert.All(results, identity => Assert.Equal(results[0], identity)); | ||
| Assert.True(GenericGrainInterfaceType.TryParse(results[0], out var generic)); | ||
| Assert.Equal([typeof(List<int>)], generic.GetArguments(CreateConverter())); | ||
| } | ||
|
|
||
| private static GrainInterfaceTypeResolver CreateResolver(params IGrainInterfaceTypeProvider[] providers) => new(providers, CreateConverter()); | ||
|
|
||
| private static TypeConverter CreateConverter() => new( | ||
| Array.Empty<ITypeConverter>(), Array.Empty<ITypeNameFilter>(), Array.Empty<ITypeFilter>(), | ||
| Options.Create(new TypeManifestOptions { AllowAllTypes = true }), new CachedTypeResolver()); | ||
|
|
||
| private interface ITestInterface<T> { } | ||
|
|
||
| private sealed class IdentityProvider(string identity) : IGrainInterfaceTypeProvider | ||
| { | ||
| public int Calls; | ||
| public bool Fail; | ||
|
|
||
| public bool TryGetGrainInterfaceType(Type type, out GrainInterfaceType result) | ||
| { | ||
| Interlocked.Increment(ref Calls); | ||
| if (Fail) | ||
| { | ||
| throw new InvalidOperationException("Injected identity resolution failure"); | ||
| } | ||
|
|
||
| result = GrainInterfaceType.Create(identity); | ||
| return true; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.