Skip to content

Commit 03536cc

Browse files
committed
bench: compare .NET 10 JIT and NativeAOT compilation and execution
1 parent e330ef8 commit 03536cc

10 files changed

Lines changed: 296 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
obj/
3+
BenchmarkDotNet.Artifacts/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
10+
<ProjectReference Include="../../src/Lua/Lua.csproj" />
11+
<Compile Include="../Benchmark/ComparisonWorkloads.cs" Link="ComparisonWorkloads.cs" />
12+
<Compile Include="../Benchmark/LuaCSharpComparison.cs" Link="LuaCSharpComparison.cs" />
13+
<EmbeddedResource Include="../Benchmark/n-body.lua" LogicalName="Comparison.n-body.lua" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using BenchmarkDotNet.Running;
2+
3+
if (args.Contains("--verify"))
4+
{
5+
foreach (
6+
var workload in ComparisonWorkloads
7+
.Cases.Concat(ComparisonWorkloads.CompilationCases)
8+
.DistinctBy(c => c.Name)
9+
)
10+
RuntimeVerification.Verify(workload);
11+
return;
12+
}
13+
14+
BenchmarkSwitcher
15+
.FromTypes([typeof(RuntimeExecutionComparison), typeof(RuntimeCompilationComparison)])
16+
.Run(args);
17+
18+
static class RuntimeVerification
19+
{
20+
public static void Verify(ComparisonCase workload)
21+
{
22+
foreach (var bytes in new[] { false, true })
23+
{
24+
using var runner = new LuaCSharpComparison(workload.Source, bytes);
25+
for (var i = 0; i < 2; i++)
26+
ComparisonWorkloads.Check(
27+
workload,
28+
bytes ? "Lua-CSharp bytes" : "Lua-CSharp",
29+
runner.Run()
30+
);
31+
}
32+
Console.WriteLine($"PASS {workload.Name}: {workload.Expected:R}");
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# .NET 10 JIT and NativeAOT benchmarks
2+
3+
Compare Lua-CSharp source compilation and bytecode execution under .NET 10 JIT
4+
and NativeAOT, using the sources and checksum checks from `../Benchmark`.
5+
NativeAOT compiles the C# interpreter; Lua source is still compiled to Lua
6+
bytecode and interpreted.
7+
8+
A [selected numeric-loop comparison](results/2026-10-27/README.md) records
9+
compilation/execution results, settings and raw CSV summaries.
10+
11+
Run from this directory:
12+
13+
```powershell
14+
dotnet restore Benchmark.NativeAot.csproj
15+
dotnet build Benchmark.NativeAot.csproj -c Release --no-restore
16+
dotnet bin/Release/net10.0/Benchmark.NativeAot.dll --verify
17+
dotnet bin/Release/net10.0/Benchmark.NativeAot.dll --filter '*RuntimeExecutionComparison*numeric-loop*' --exporters json
18+
dotnet bin/Release/net10.0/Benchmark.NativeAot.dll --filter '*RuntimeCompilationComparison*numeric-loop*' --exporters json
19+
```
20+
21+
Use a case filter before selecting a whole suite. `--list flat` lists the
22+
benchmarks; no arguments opens BenchmarkDotNet's selector. `--artifacts <path>`
23+
keeps outputs separate and `--keepFiles` retains generated projects/executables.
24+
25+
The BenchmarkDotNet NativeAOT toolchain publishes a native benchmark process.
26+
Install the platform's NativeAOT build prerequisites (Visual C++ build tools
27+
and a Windows SDK on Windows). The native job pins compiler/runtime **10.0.14**,
28+
uses speed optimization and the current CPU's native instruction set. Record
29+
the resolved JIT runtime version too. These native executables target the build
30+
machine's CPU capabilities.
31+
32+
`--verify` checks all 15 distinct workloads in the managed host, twice in each
33+
string mode. Each benchmark's setup also runs its checksum checks inside the
34+
actual JIT or NativeAOT benchmark process before timing. Timed execution uses
35+
UTF-16 mode with ASCII inputs, retained state and a compiled chunk. Compilation
36+
has six source cases, a fresh state outside each batch of 64 loads, and no Lua
37+
execution during measurement. Both jobs use three launches. Compilation warms
38+
for two seconds, then measures 20 batches after 20 warmups; execution measures
39+
10 iterations after five warmups with a 250 ms target iteration time.
40+
41+
Run timing jobs serially. JIT uses runtime defaults; NativeAOT has no tiered JIT
42+
or dynamic PGO. Compare the same sources and keep these profiles distinct from
43+
cross-engine results. MemoryDiagnoser reports managed allocation; generated
44+
executable sizes also include BenchmarkDotNet and metadata, so neither is a
45+
minimal embedding application size or total native-Lua memory measurement.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System.Diagnostics;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Configs;
4+
using BenchmarkDotNet.Diagnosers;
5+
using BenchmarkDotNet.Environments;
6+
using BenchmarkDotNet.Jobs;
7+
using BenchmarkDotNet.Toolchains.NativeAot;
8+
using Lua;
9+
using Lua.Runtime;
10+
using Lua.Standard;
11+
using Perfolizer.Horology;
12+
13+
static class RuntimeJobs
14+
{
15+
public static Job NativeAot(Job job) =>
16+
job.WithRuntime(NativeAotRuntime.Net10_0)
17+
.WithArguments([
18+
new MsBuildArgument("/p:RestoreSources=https://api.nuget.org/v3/index.json"),
19+
])
20+
.WithToolchain(
21+
NativeAotToolchain
22+
.CreateBuilder()
23+
.UseNuGet("10.0.14", "https://api.nuget.org/v3/index.json")
24+
.IlcInstructionSet("native")
25+
.TargetFrameworkMoniker("net10.0")
26+
.RuntimeFrameworkVersion("10.0.14")
27+
.UseNuGetClearTag(true)
28+
.DisplayName("NativeAOT 10.0.14")
29+
.ToToolchain()
30+
)
31+
.WithId("NET10-NativeAOT");
32+
}
33+
34+
public sealed class RuntimeExecutionConfig : ManualConfig
35+
{
36+
public RuntimeExecutionConfig()
37+
{
38+
AddDiagnoser(MemoryDiagnoser.Default);
39+
var job = Job
40+
.ShortRun.WithLaunchCount(3)
41+
.WithWarmupCount(5)
42+
.WithIterationCount(10)
43+
.WithIterationTime(TimeInterval.FromMilliseconds(250));
44+
AddJob(job.WithRuntime(CoreRuntime.Core10_0).WithId("NET10-JIT").AsBaseline());
45+
AddJob(RuntimeJobs.NativeAot(job));
46+
}
47+
}
48+
49+
public sealed class RuntimeCompilationConfig : ManualConfig
50+
{
51+
public RuntimeCompilationConfig()
52+
{
53+
AddDiagnoser(MemoryDiagnoser.Default);
54+
var job = Job
55+
.ShortRun.WithLaunchCount(3)
56+
.WithWarmupCount(20)
57+
.WithIterationCount(20)
58+
.WithInvocationCount(1)
59+
.WithUnrollFactor(1);
60+
AddJob(job.WithRuntime(CoreRuntime.Core10_0).WithId("NET10-JIT").AsBaseline());
61+
AddJob(RuntimeJobs.NativeAot(job));
62+
}
63+
}
64+
65+
[Config(typeof(RuntimeExecutionConfig))]
66+
[BenchmarkCategory("Execution")]
67+
public class RuntimeExecutionComparison
68+
{
69+
[ParamsSource(nameof(Cases))]
70+
public string Case { get; set; } = "numeric-loop";
71+
public static IEnumerable<string> Cases => ComparisonWorkloads.Cases.Select(c => c.Name);
72+
LuaCSharpComparison runner = default!;
73+
74+
[GlobalSetup]
75+
public void Setup()
76+
{
77+
var workload = ComparisonWorkloads.Find(Case);
78+
RuntimeVerification.Verify(workload);
79+
runner = new(workload.Source);
80+
}
81+
82+
[Benchmark]
83+
public double Execute() => runner.Run();
84+
85+
[GlobalCleanup]
86+
public void Cleanup() => runner.Dispose();
87+
}
88+
89+
[Config(typeof(RuntimeCompilationConfig))]
90+
[BenchmarkCategory("Compilation")]
91+
public class RuntimeCompilationComparison
92+
{
93+
[ParamsSource(nameof(Cases))]
94+
public string Case { get; set; } = "numeric-loop";
95+
public static IEnumerable<string> Cases =>
96+
ComparisonWorkloads.CompilationCases.Select(c => c.Name);
97+
string source = default!;
98+
LuaState state = default!;
99+
const int BatchSize = 64;
100+
101+
[GlobalSetup]
102+
public void Setup()
103+
{
104+
var workload = ComparisonWorkloads.FindCompilation(Case);
105+
RuntimeVerification.Verify(workload);
106+
source = workload.Source;
107+
var timer = Stopwatch.StartNew();
108+
do
109+
{
110+
ResetState();
111+
try
112+
{
113+
Compile();
114+
}
115+
finally
116+
{
117+
Cleanup();
118+
}
119+
} while (timer.ElapsedMilliseconds < 2000);
120+
}
121+
122+
[IterationSetup]
123+
public void ResetState()
124+
{
125+
state = LuaState.Create();
126+
state.OpenStandardLibraries();
127+
}
128+
129+
[Benchmark(OperationsPerInvoke = BatchSize)]
130+
public LuaClosure Compile()
131+
{
132+
LuaClosure result = default!;
133+
for (var i = 0; i < BatchSize; i++)
134+
result = state.Load(source, "comparison");
135+
return result;
136+
}
137+
138+
[IterationCleanup]
139+
public void Cleanup() => state.Dispose();
140+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sdk": {
3+
"version": "10.0.100",
4+
"rollForward": "latestFeature"
5+
}
6+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Selected .NET 10 JIT / NativeAOT comparison
2+
3+
2026-10-27. Lua-CSharp engine source `ad530ca`; benchmark sources alongside
4+
this report. BenchmarkDotNet 0.15.8, SDK 10.0.403, JIT and NativeAOT runtime
5+
10.0.14. Windows 11 10.0.26300.9711, Intel Core i7-13700F,
6+
24 logical / 16 physical cores. BenchmarkDotNet selected High Performance
7+
power. JIT uses default tiering/PGO; NativeAOT targets the host's native ISA.
8+
9+
Only `numeric-loop` was timed: compile a small numeric-loop chunk, or execute
10+
its retained compiled function summing 1..20,000. Each actual native benchmark
11+
process passed its setup checksum checks. The managed verification command
12+
also passed all 15 distinct shared workloads in both string modes.
13+
14+
## Compilation
15+
16+
Three launches, two seconds of bounded warmup per launch, 20 warmup and 20
17+
measured batches of 64 compilations. A fresh state is created outside each
18+
batch. The table is per compilation; small batches remain below the recommended
19+
iteration duration, so inspect variance rather than assuming exact precision.
20+
21+
| Method | Job | Runtime | Arguments | Case | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
22+
|-------- |---------------- |--------------- |------------------------------------------------------ |------------- |---------:|----------:|----------:|------:|--------:|----------:|------------:|
23+
| Compile | NET10-JIT | .NET 10.0 | Default | numeric-loop | 1.269 μs | 0.0190 μs | 0.0389 μs | 1.00 | 0.04 | 696 B | 1.00 |
24+
| Compile | NET10-NativeAOT | NativeAOT 10.0 | /p:RestoreSources=https://api.nuget.org/v3/index.json | numeric-loop | 1.429 μs | 0.0206 μs | 0.0452 μs | 1.13 | 0.05 | 712 B | 1.02 |
25+
26+
## Retained-chunk execution
27+
28+
Three launches, five warmups and ten measurements per launch, 250 ms target
29+
iteration time. State/source compilation are outside timing; one numeric
30+
result is read on each invocation.
31+
32+
| Method | Job | Runtime | Arguments | Case | Mean | Error | StdDev | Ratio | Allocated | Alloc Ratio |
33+
|-------- |---------------- |--------------- |------------------------------------------------------ |------------- |---------:|---------:|---------:|------:|----------:|------------:|
34+
| Execute | NET10-JIT | .NET 10.0 | Default | numeric-loop | 95.59 μs | 0.570 μs | 0.835 μs | 1.00 | - | NA |
35+
| Execute | NET10-NativeAOT | NativeAOT 10.0 | /p:RestoreSources=https://api.nuget.org/v3/index.json | numeric-loop | 98.64 μs | 0.258 μs | 0.386 μs | 1.03 | 1 B | NA |
36+
37+
Error is half the 99.9% confidence interval. Memory figures are managed
38+
allocation per operation and include small harness/background noise. This
39+
single workload does not establish overall JIT/NativeAOT performance or
40+
minimal application size. Keep these profiles separate from native Lua and
41+
MoonSharp results.
42+
43+
Raw summaries: [compilation.csv](compilation.csv), [execution.csv](execution.csv).
44+
The benchmark README has the exact case-filter commands used for this run.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Method,Job,AnalyzeLaunchVariance,EvaluateOverhead,MaxAbsoluteError,MaxRelativeError,MinInvokeCount,MinIterationTime,OutlierMode,Affinity,EnvironmentVariables,Jit,LargeAddressAware,Platform,PowerPlanMode,Runtime,AllowVeryLargeObjects,Concurrent,CpuGroups,Force,HeapAffinitizeMask,HeapCount,NoAffinitize,RetainVm,Server,Arguments,BuildConfiguration,Clock,EngineFactory,NuGetReferences,IsMutator,InvocationCount,IterationCount,IterationTime,LaunchCount,MaxIterationCount,MaxWarmupIterationCount,MemoryRandomization,MinIterationCount,MinWarmupIterationCount,RunStrategy,UnrollFactor,WarmupCount,Case,Mean,Error,StdDev,Ratio,RatioSD,Allocated,Alloc Ratio
2+
Compile,NET10-JIT,False,Default,Default,Default,Default,Default,Default,111111111111111111111111,Empty,RyuJit,Default,X64,8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c,.NET 10.0,False,True,False,True,Default,Default,False,False,False,Default,Default,Default,Default,Default,Default,1,20,Default,3,Default,Default,Default,Default,Default,Default,1,20,numeric-loop,1.269 μs,0.0190 μs,0.0389 μs,1.00,0.04,696 B,1.00
3+
Compile,NET10-NativeAOT,False,Default,Default,Default,Default,Default,Default,111111111111111111111111,Empty,RyuJit,Default,X64,8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c,NativeAOT 10.0,False,True,False,True,Default,Default,False,False,False,/p:RestoreSources=https://api.nuget.org/v3/index.json,Default,Default,Default,Default,Default,1,20,Default,3,Default,Default,Default,Default,Default,Default,1,20,numeric-loop,1.429 μs,0.0206 μs,0.0452 μs,1.13,0.05,712 B,1.02
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Method,Job,AnalyzeLaunchVariance,EvaluateOverhead,MaxAbsoluteError,MaxRelativeError,MinInvokeCount,MinIterationTime,OutlierMode,Affinity,EnvironmentVariables,Jit,LargeAddressAware,Platform,PowerPlanMode,Runtime,AllowVeryLargeObjects,Concurrent,CpuGroups,Force,HeapAffinitizeMask,HeapCount,NoAffinitize,RetainVm,Server,Arguments,BuildConfiguration,Clock,EngineFactory,NuGetReferences,IsMutator,InvocationCount,IterationCount,IterationTime,LaunchCount,MaxIterationCount,MaxWarmupIterationCount,MemoryRandomization,MinIterationCount,MinWarmupIterationCount,RunStrategy,UnrollFactor,WarmupCount,Case,Mean,Error,StdDev,Ratio,Allocated,Alloc Ratio
2+
Execute,NET10-JIT,False,Default,Default,Default,Default,Default,Default,111111111111111111111111,Empty,RyuJit,Default,X64,8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c,.NET 10.0,False,True,False,True,Default,Default,False,False,False,Default,Default,Default,Default,Default,Default,Default,10,250ms,3,Default,Default,Default,Default,Default,Default,16,5,numeric-loop,95.59 μs,0.570 μs,0.835 μs,1.00,0 B,NA
3+
Execute,NET10-NativeAOT,False,Default,Default,Default,Default,Default,Default,111111111111111111111111,Empty,RyuJit,Default,X64,8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c,NativeAOT 10.0,False,True,False,True,Default,Default,False,False,False,/p:RestoreSources=https://api.nuget.org/v3/index.json,Default,Default,Default,Default,Default,Default,10,250ms,3,Default,Default,Default,Default,Default,Default,16,5,numeric-loop,98.64 μs,0.258 μs,0.386 μs,1.03,1 B,NA

‎sandbox/Benchmark/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,6 @@ single overall ranking without application-specific workload weights.
7272

7373
The legacy suites include file I/O, source execution and focused API tests with
7474
different boundaries. Their results should not be mixed with this suite.
75+
76+
For Lua-CSharp JIT versus NativeAOT compilation and execution, use the
77+
[NativeAOT benchmarks](../Benchmark.NativeAot/README.md), which share these sources.

0 commit comments

Comments
 (0)