diff --git a/TUnit.Core.SourceGenerator/Helpers/InterfaceCache.cs b/TUnit.Core.SourceGenerator/Helpers/InterfaceCache.cs index 30f6f5cf71..6717d29e63 100644 --- a/TUnit.Core.SourceGenerator/Helpers/InterfaceCache.cs +++ b/TUnit.Core.SourceGenerator/Helpers/InterfaceCache.cs @@ -7,9 +7,9 @@ namespace TUnit.Core.SourceGenerator.Helpers; /// /// Caches interface implementation checks to avoid repeated AllInterfaces traversals /// -internal static class InterfaceCache +public static class InterfaceCache { - private static readonly ConcurrentDictionary<(ITypeSymbol Type, string InterfaceName), bool> _implementsCache = new(TypeStringTupleComparer.Default); + public static readonly ConcurrentDictionary<(ITypeSymbol Type, string InterfaceName), bool> _implementsCache = new(TypeStringTupleComparer.Default); private static readonly ConcurrentDictionary<(ITypeSymbol Type, string GenericInterfacePattern), INamedTypeSymbol?> _genericInterfaceCache = new(TypeStringTupleComparer.Default); /// diff --git a/TUnit.SourceGenerator.Benchmarks/Program.cs b/TUnit.SourceGenerator.Benchmarks/Program.cs index 2a73e62c73..576ad408b6 100644 --- a/TUnit.SourceGenerator.Benchmarks/Program.cs +++ b/TUnit.SourceGenerator.Benchmarks/Program.cs @@ -10,7 +10,17 @@ } else { - BenchmarkRunner.Run(); + var bench = new TestMetadataGeneratorBenchmarks(); + bench.SetupRunGenerator(); + for (int i = 0; i < 100; i++) + { + bench.RunGenerator(); + var count = TUnit.Core.SourceGenerator.Helpers.InterfaceCache._implementsCache.Count; + Console.WriteLine($"{count} references to stale compilations. Total {GC.GetTotalMemory(true)/1_000_000} MB"); + } + + bench.Cleanup(); + // BenchmarkRunner.Run(); // BenchmarkRunner.Run(); // BenchmarkRunner.Run(); } diff --git a/TUnit.SourceGenerator.Benchmarks/TestMetadataGeneratorBenchmarks.cs b/TUnit.SourceGenerator.Benchmarks/TestMetadataGeneratorBenchmarks.cs index 17430eaa0e..c83b6a1d78 100644 --- a/TUnit.SourceGenerator.Benchmarks/TestMetadataGeneratorBenchmarks.cs +++ b/TUnit.SourceGenerator.Benchmarks/TestMetadataGeneratorBenchmarks.cs @@ -1,5 +1,6 @@ using BenchmarkDotNet.Attributes; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.MSBuild; using TUnit.Core.SourceGenerator.Generators; using TUnit.SourceGenerator.Benchmarks; @@ -23,8 +24,25 @@ public void SetupRunGenerator() => .GetAwaiter() .GetResult(); + // [IterationSetup(Target = nameof(RunGenerator))] + // public void Setup() + // { + // _sampleCompilation = _sampleCompilation!.AddSyntaxTrees([CSharpSyntaxTree.ParseText($"struct MyValue{Random.Shared.Next()} {{}}", options: (CSharpParseOptions)_sampleCompilation.SyntaxTrees.First().Options)]); + // + // } + + // [Benchmark] + // public GeneratorDriver RunGenerator() => _sampleDriver!.RunGeneratorsAndUpdateCompilation(_sampleCompilation!, out _, out _); + [Benchmark] - public GeneratorDriver RunGenerator() => _sampleDriver!.RunGeneratorsAndUpdateCompilation(_sampleCompilation!, out _, out _); + public GeneratorDriver RunGenerator() + { + var driver = _sampleDriver!.RunGeneratorsAndUpdateCompilation(_sampleCompilation!, out _, out _); + + // add a random type to create a new compilation. + _sampleCompilation = _sampleCompilation.AddSyntaxTrees([CSharpSyntaxTree.ParseText($"struct MyValue{Random.Shared.Next()} {{}}", options: (CSharpParseOptions)_sampleCompilation.SyntaxTrees.First().Options)]); + return driver; + } [GlobalCleanup] public void Cleanup()