Skip to content

Commit 7492972

Browse files
Add CommunityToolkit.Maui.SourceGenerator.Benchmarks, Reduce Memory Allocations on [BindableProperty] Source Generator (#2960)
1 parent a77c100 commit 7492972

File tree

8 files changed

+281
-135
lines changed

8 files changed

+281
-135
lines changed

.github/workflows/benchmarks.yml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ on:
1414

1515
env:
1616
LATEST_NET_VERSION: '10.0.x'
17-
PathToCommunityToolkitAnalyzersBenchmarkCsproj: 'src/CommunityToolkit.Maui.Analyzers.Benchmarks/CommunityToolkit.Maui.Analyzers.Benchmarks.csproj'
17+
PathToCommunityToolkitAnalyzersBenchmarksCsproj: 'src/CommunityToolkit.Maui.Analyzers.Benchmarks/CommunityToolkit.Maui.Analyzers.Benchmarks.csproj'
18+
PathToCommunityToolkitSourceGeneratorsBenchmarksCsproj: 'src/CommunityToolkit.Maui.SourceGenerators.Benchmarks/CommunityToolkit.Maui.SourceGenerators.Benchmarks.csproj'
1819

1920
concurrency:
2021
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
@@ -23,21 +24,11 @@ concurrency:
2324
jobs:
2425
run_benchmarks:
2526
name: Run Benchmarks
26-
runs-on: ${{ matrix.os }}
27-
strategy:
28-
fail-fast: false
29-
matrix:
30-
os: [ windows-latest, macos-26 ]
31-
27+
runs-on: windows-latest
3228
steps:
3329
- name: Checkout code
3430
uses: actions/checkout@main
3531

36-
- uses: maxim-lobanov/setup-xcode@v1
37-
if: runner.os == 'macOS'
38-
with:
39-
xcode-version: latest-stable
40-
4132
- name: Install Latest Version of .NET, v${{ env.LATEST_NET_VERSION }}
4233
uses: actions/setup-dotnet@v5
4334
with:
@@ -58,11 +49,13 @@ jobs:
5849
- name: Display dotnet info
5950
run: dotnet --info
6051

61-
- name: Run Benchmarks
62-
run: dotnet run --project ${{ env.PathToCommunityToolkitAnalyzersBenchmarkCsproj }} -c Release -- -a ${{ runner.temp }}
52+
- name: Run Analyzer Benchmarks
53+
run: dotnet run --project ${{ env.PathToCommunityToolkitAnalyzersBenchmarksCsproj }} -c Release -- -a "${{ runner.temp }}"
54+
55+
- name: Run Source Generator Benchmarks
56+
run: dotnet run --project ${{ env.PathToCommunityToolkitSourceGeneratorsBenchmarksCsproj }} -c Release -- -a "${{ runner.temp }}"
6357

6458
- name: Publish Benchmarks
65-
if: runner.os == 'Windows'
6659
uses: actions/upload-artifact@v5
6760
with:
6861
name: Benchmarks

samples/CommunityToolkit.Maui.Sample.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</Folder>
1212
<Folder Name="/Benchmarks/">
1313
<Project Path="../src/CommunityToolkit.Maui.Analyzers.Benchmarks/CommunityToolkit.Maui.Analyzers.Benchmarks.csproj" />
14+
<Project Path="../src/CommunityToolkit.Maui.SourceGenerators.Benchmarks/CommunityToolkit.Maui.SourceGenerators.Benchmarks.csproj" Id="cc195bb1-9cac-410e-81fe-23665f99f294" />
1415
</Folder>
1516
<Folder Name="/Samples/">
1617
<Project Path="CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj">
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using BenchmarkDotNet.Attributes;
2+
using CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.BindablePropertyAttributeSourceGeneratorTests;
3+
4+
namespace CommunityToolkit.Maui.SourceGenerators.Benchmarks;
5+
6+
[MemoryDiagnoser]
7+
public class BindablePropertyAttributeSourceGeneratorBenchmarks
8+
{
9+
static readonly CommonUsageTests commonUsageTests = new();
10+
static readonly EdgeCaseTests edgeCaseTests = new();
11+
static readonly IntegrationTests integrationTests = new();
12+
13+
[Benchmark]
14+
public Task GenerateBindableProperty_SimpleExample_GeneratesCorrectCode()
15+
=> commonUsageTests.GenerateBindableProperty_SimpleExample_GeneratesCorrectCode();
16+
17+
[Benchmark]
18+
public Task GenerateBindableProperty_MultipleProperties_GeneratesCorrectCode()
19+
=> commonUsageTests.GenerateBindableProperty_MultipleProperties_GeneratesCorrectCode();
20+
21+
[Benchmark]
22+
public Task GenerateBindableProperty_WithAllParameters_GeneratesCorrectCode()
23+
=> commonUsageTests.GenerateBindableProperty_WithAllParameters_GeneratesCorrectCode();
24+
25+
[Benchmark]
26+
public Task GenerateBindableProperty_InternalClass_GeneratesCorrectCode()
27+
=> commonUsageTests.GenerateBindableProperty_InternalClass_GeneratesCorrectCode();
28+
29+
[Benchmark]
30+
public Task GenerateBindableProperty_ComplexInheritanceScenario_GeneratesCorrectCode()
31+
=> integrationTests.GenerateBindableProperty_ComplexInheritanceScenario_GeneratesCorrectCode();
32+
33+
[Benchmark]
34+
public Task GenerateBindableProperty_NestedClass_GeneratesCorrectCode()
35+
=> integrationTests.GenerateBindableProperty_NestedClass_GeneratesCorrectCode();
36+
37+
[Benchmark]
38+
public Task GenerateBindableProperty_WithComplexDefaultValues_GeneratesCorrectCode()
39+
=> edgeCaseTests.GenerateBindableProperty_WithComplexDefaultValues_GeneratesCorrectCode();
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(NetVersion)</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<StartupObject>CommunityToolkit.Maui.SourceGenerators.Benchmarks.Program</StartupObject>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.15.7" />
11+
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests\CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.csproj" />
16+
<ProjectReference Include="..\CommunityToolkit.Maui.SourceGenerators\CommunityToolkit.Maui.SourceGenerators.csproj" />
17+
</ItemGroup>
18+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using BenchmarkDotNet.Configs;
2+
using BenchmarkDotNet.Running;
3+
4+
namespace CommunityToolkit.Maui.SourceGenerators.Benchmarks;
5+
6+
class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
var config = DefaultConfig.Instance;
11+
BenchmarkRunner.Run<BindablePropertyAttributeSourceGeneratorBenchmarks>(config, args);
12+
}
13+
}

src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/BindablePropertyAttributeSourceGeneratorTests.cs renamed to src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/CommonUsageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests.BindablePropertyAttributeSourceGeneratorTests;
44

5-
public class BindablePropertyAttributeSourceGeneratorTests : BaseBindablePropertyAttributeSourceGeneratorTest
5+
public class CommonUsageTests : BaseBindablePropertyAttributeSourceGeneratorTest
66
{
77
[Fact]
88
public async Task GenerateBindableProperty_SimpleExample_GeneratesCorrectCode()

0 commit comments

Comments
 (0)