Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TUnit.Core.SourceGenerator/Helpers/InterfaceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace TUnit.Core.SourceGenerator.Helpers;
/// <summary>
/// Caches interface implementation checks to avoid repeated AllInterfaces traversals
/// </summary>
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);

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion TUnit.SourceGenerator.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@
}
else
{
BenchmarkRunner.Run<TestMetadataGeneratorBenchmarks>();
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<TestMetadataGeneratorBenchmarks>();
// BenchmarkRunner.Run<AotConverterGeneratorBenchmarks>();
// BenchmarkRunner.Run<StaticPropertyInitializationGeneratorBenchmarks>();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
Expand Down