Skip to content

Latest commit

 

History

History
563 lines (472 loc) · 43.6 KB

Tuning.md

File metadata and controls

563 lines (472 loc) · 43.6 KB

Tuning converted code

Converted Go runs on .NET, and .NET gives you choices: how the program is compiled and deployed, how the runtime compiles it while it runs, and a few converter options that change what it costs. This page says which choices matter, what each one trades, and which to leave alone. Every claim names the platform it was measured on. For the numbers, see the performance comparison, which is re-measured as the converter changes.

In short: keep the .NET defaults. Publish with ReadyToRun or Native AOT when start time matters, and prefer the JIT over Native AOT when tight loops dominate. Do not turn tiered compilation or dynamic PGO off for speed: on win-x64, the default runtime ran a pointer-heavy benchmark in roughly 40% less time than with either one off.

Start-up or steady state: the one setting to know. A converted program optimizes its hot code from its first moment. That keeps a service fast, and it costs a short-lived tool part of its start: on one CPU, about a quarter of a small program's run. Short-lived tools, CLIs and containers with one or two CPUs: set GoCallCountingDelayMs to runtime. Long-running services, servers and benchmarks: keep the default. The measurements and how to set it: start-up or steady state.

Choosing a deployment

A converted program can run three ways. All three run the same converted code; they differ in when it is compiled.

Deployment How you get it Compiled
JIT dotnet run, dotnet build, or a framework-dependent dotnet publish as each method first runs, then recompiled hotter as it keeps running
ReadyToRun dotnet publish -c Release of a converted program, which turns it on golib and the standard library ahead of time, then the JIT can still recompile hot methods; the program's own code as it runs
Native AOT dotnet publish -r <rid> -p:PublishAot=true entirely ahead of time, to a native executable with no JIT

In a folder that go2cs -recurse wrote, run that command in the program's own folder or name the program's project file: the converted libraries are compiled into the program and are not published on their own.

A Native AOT publish also needs the platform's native toolchain: on Linux, clang and zlib1g-dev (Debian and Ubuntu: sudo apt install clang zlib1g-dev; Fedora: clang zlib-devel); on Windows, the MSVC C++ build tools; on macOS, the Xcode command line tools. A publish that fails leaves its output folder as it was. A program from an earlier publish into the same folder, such as the small launcher a Release publish writes, is still there and still runs. Read the publish's own result, its error lines and its exit status, before you run what is in the folder.

Which wins depends on what the program spends its time on. The win-x64 comparisons below come from the performance comparison's current table, measured on one Windows machine (an AMD Ryzen 5 PRO 6650U) against the go.* 1.26.10.2 packages; a bullet that uses another source names it.

  • Start time. On win-x64, a Native AOT program started in about a third of the time the same program took under the JIT, though still slower than Go, and a ReadyToRun publish started in about three quarters of the JIT's time. On linux-x64, ReadyToRun cut a converted program's median start time by about a third against the JIT.
  • Tight loops. On win-x64, the JIT ran slice-indexing and floating-point loops faster than Native AOT, which took about 2.2 and 3.3 times as long. On a pointer-heavy loop, Native AOT took about a quarter longer than the JIT. Native AOT has no dynamic profile-guided optimization or on-stack replacement to tune a hot loop as it runs.
  • Maps. On win-x64, converted map code ran faster than Go under the JIT, ReadyToRun and Native AOT, and fastest under the JIT and ReadyToRun.
  • Interfaces. On win-x64, calling interface methods and asserting to concrete types took about 1.5 to 1.7 times as long under Native AOT as under the JIT. The one interface path with no compile-time answer, asserting a value to an interface it satisfies only structurally, took about twice as long under Native AOT as under the JIT.
  • Memory. On win-x64, in the performance comparison's Go 1.23.1 table under History, a Native AOT program's peak working set was lower than the JIT's on every benchmark. The current table reports time only.
  • Build cost. A Native AOT publish compiles every converted package the program references, so it is slow and the executable is large. Known issues has the measured cost and its platform.

A single-file publish compiles ReadyToRun one assembly at a time. For a self-contained single-file ReadyToRun publish, the .NET SDK's default is one composite image of the program and the framework. A converted program's project also compresses the bundle, so every start decompresses that image first. The project therefore sets <PublishReadyToRunComposite>false</PublishReadyToRunComposite> unless you set it yourself, as the publish profiles go2cs writes already do. Start-up wall time of a dotnet publish -p:PublishSingleFile=true on win-x64:

ReadyToRun Bundle Start-up Also
composite compressed 3,480 ms a 571 MB executable; a string-matching loop ran 27% slower
one assembly at a time compressed 470 ms
composite uncompressed 320 ms a 1.70 GB executable, and about 700 s to publish

To build the composite image anyway, pass -p:PublishReadyToRunComposite=true. On linux-x64 the composite image can also run the ReadyToRun compiler out of memory (Known issues).

Your own code is compiled as it runs. A ReadyToRun publish of a converted program precompiles golib and the standard library, which is where start time is won. It leaves the program's own assemblies, every project built from your module, to the JIT. ReadyToRun compiles each assembly on its own: precompiled code cannot inline a call into another assembly, and a loop that runs only once never leaves its precompiled form. On win-x64 (an AMD Ryzen 5 PRO 6650U), against a publish that precompiled everything:

  • a published loop calling strconv.FormatBool ran in about an eighth of the time;
  • a string-matching benchmark's hot phase took about two thirds of the time;
  • start time stayed within run-to-run variation, and the published folder came out a few tens of KB smaller.

To precompile your own code as well, set <GoReadyToRunUserCode>true</GoReadyToRunUserCode>. That is rarely worth it. Across the performance suite, on the same machine, precompiling the program's own code makes 11 of the 17 programs measured slower than the default ReadyToRun publish and none faster. FieldView, Fib, RefLower, Sort, Startup and TlsHandshake stay within measurement noise. Medians in ms; the ratio is precompiled-user-code against the default:

Benchmark default ReadyToRun GoReadyToRunUserCode ratio
StringView 21.8 605.2 27.792
IfaceCall 339.6 1458.1 4.294
String 653.5 1995.5 3.054
MatMul 213.8 589.4 2.757
Sieve 139.5 265.2 1.901
Iface 376.1 660.4 1.756
StringMatch 804.7 1215.2 1.510
Channel 79.9 117.1 1.466
IfaceShell 574.6 728.4 1.268
IfaceShellChurn 1071.7 1188.1 1.109
Map 248.9 268.9 1.080

A Native AOT build spends most of its time folding identical method bodies. After it compiles a program, the Native AOT compiler looks for methods whose machine code came out identical and keeps one copy of each. The .NET SDK does this by default in an optimized build, and the pass runs on a single core. On the go2cs programs measured, this one pass took three quarters or more of the compiler's time. Turning it off makes the build several times faster. The program runs the same; it is larger, and its symbol table is larger.

Program (win-x64) Compiler time, default Compiler time, folding off Executable Compiler memory Symbol names
PerfStartup 6 min 29 s 1 min 27 s 87.6 → 101.0 MB (+15%) +13% 115.7 → 131.8 MB (+14%)
TypeScript parsedemo 32 min 20 s 7 min 6 s 252.3 → 280.1 MB (+11%) +13% 616.2 → 657.3 MB (+7%)

Turn folding off where build time matters more than size: a development loop, or a CI job that publishes Native AOT to test it. Keep the default for what you ship when the executable's size matters, and for a very large program: the symbol names grow, and the native symbol table has a 2 GiB ceiling, so a program already close to it can stop fitting. Folding only removes symbols, so a program that builds with folding off also builds with it on. A program past the ceiling fails late in the publish, on Linux with System.OutOfMemoryException: Insufficient memory to continue the execution of the program. and this line in the stack: at ILCompiler.ObjectWriter.StringTableBuilder.CreateIndex(String). The message names memory, but the limit is the string table's size, not the machine's RAM.

The setting is a project property, IlcFoldIdenticalMethodBodies. Set it in one of three places:

Where How
The program's project file <IlcFoldIdenticalMethodBodies>false</IlcFoldIdenticalMethodBodies> in a <PropertyGroup>
One publish, on the command line dotnet publish -r <rid> -p:PublishAot=true -p:IlcFoldIdenticalMethodBodies=false
Every project under a folder the same property in a <PropertyGroup> of a Directory.Build.props there

go2cs's project templates do not set it, so a converted program builds with the SDK's default.

.NET runtime settings

Under the JIT and ReadyToRun, .NET compiles code in tiers. A method first gets a quick compile; one that keeps running is recompiled with full optimization, guided by a profile the runtime collected while it ran (dynamic PGO). Native AOT has no tiers and no dynamic PGO.

Leave tiered compilation and dynamic PGO on. On win-x64, a converted benchmark that takes field addresses in a hot loop took roughly 40% less time with the defaults than with tiered compilation off, and about as much less than with dynamic PGO off. Turning tiering off compiles every method once at full optimization, but without the profile that the tiers collect, and the profile is worth more.

Across the performance suite, on win-x64 (an AMD Ryzen 5 PRO 6650U), turning dynamic PGO off with DOTNET_TieredPGO=0 makes 13 of the 17 programs measured slower under the JIT and none faster; Startup, Fib, Sieve and Channel stay within measurement noise. Turning it off is not recommended. Medians in ms, in-program time; the ratio is PGO off against the default:

Benchmark default DOTNET_TieredPGO=0 ratio
String 339.0 1465.3 4.322
IfaceCall 358.3 807.0 2.252
MatMul 213.2 393.7 1.847
FieldView 1723.7 2917.6 1.693
TlsHandshake 1071.6 1513.4 1.412
Iface 374.7 528.0 1.409
StringView 20.9 29.4 1.405
StringMatch 791.2 1057.9 1.337
IfaceShellChurn 1076.8 1432.7 1.331
RefLower 511.3 645.7 1.263
IfaceShell 600.4 738.2 1.230
Sort 255.1 305.5 1.197
Map 253.6 264.6 1.044

The settings, if you need them, each in three equivalent forms: a property in the project file, a value in runtimeconfig.json, or an environment variable for one run.

Setting Project file runtimeconfig.json Environment
Tiered compilation <TieredCompilation>false</TieredCompilation> "System.Runtime.TieredCompilation": false DOTNET_TieredCompilation=0
Dynamic PGO <TieredPGO>false</TieredPGO> "System.Runtime.TieredPGO": false DOTNET_TieredPGO=0
Quick JIT for loops <TieredCompilationQuickJitForLoops>false</TieredCompilationQuickJitForLoops> "System.Runtime.TieredCompilation.QuickJitForLoops": false DOTNET_TC_QuickJitForLoops=0
Call-counting delay <GoCallCountingDelayMs>0</GoCallCountingDelayMs> (a converted program's own property) "System.Runtime.TieredCompilation.CallCountingDelayMs": 0 DOTNET_TC_CallCountingDelayMs=0
Server garbage collection <ServerGarbageCollection>true</ServerGarbageCollection> "System.GC.Server": true DOTNET_gcServer=1
Dynamic adaptation (DATAS) <GarbageCollectionAdaptationMode>0</GarbageCollectionAdaptationMode> "System.GC.DynamicAdaptationMode": 0 DOTNET_GCDynamicAdaptationMode=0

Microsoft documents every runtime compilation setting in Runtime configuration options for compilation. The garbage collector's settings are in Runtime configuration options for garbage collection.

Server garbage collection is an option for an allocation-heavy, long-running program. Server GC keeps one heap and one collector thread per core, and collects in parallel. DATAS, which .NET turns on by default with server GC, shrinks the heap count to fit the live data; turning it off keeps a heap on every core. On linux-x64, on a 4-vCPU cloud VM, the converted TypeScript type checker takes about a third less time over a slice of its compiler tests with both settings above, and its peak memory is about 15% higher. The cost is memory. It grows with the core count, because each core has its own heap, so measure on the machine you deploy to. The settings take effect only in an executable: in a library, the host application's own settings decide. A converted project keeps .NET's default, workstation GC, because most programs are small or short-lived and do not repay the memory.

Start-up or steady state: the call-counting delay

Choose it by how long your program runs. Measured on win-x64 (an AMD Ryzen 5 PRO 6650U, 6 cores and 12 threads): start times published as the converted project publishes by default, the TLS loop under the JIT build.

Your program Set GoCallCountingDelayMs to What that buys
A short-lived tool or CLI, or anything in a container with one or two CPUs runtime (or 10) it starts 30 to 50 ms sooner on 12 CPUs, and 100 to 170 ms sooner on one CPU: a quarter of a small program's run
A long-running service or server, and any benchmark 0, the default its hot code stays fast: with a delay of 10 ms, a loop of TLS handshakes runs 41% slower on 12 CPUs and 1.9 times slower on one

What the delay does. .NET first compiles a method quickly, then recompiles the methods that keep running with full optimization. It starts counting a method's calls only after a quiet spell with no new method compiled: 100 ms by default. A converted program sets that spell to 0, so it counts from its first moment and its hot code is optimized as soon as it is hot. The price is paid in the program's start: the optimizing compiler runs while the program is still starting, and on a machine with one CPU it competes with the program for that CPU. The delay applies wherever code is JIT-compiled: under dotnet run and dotnet build, and in a ReadyToRun publish, whose methods are still recompiled as they get hot. Native AOT has no tiers, so it does not apply there.

The default is 0 because a converted program that does real work runs for longer than its start, and there the delay costs more than it saves. Changing it trades one for the other. Medians on the same machine, start-up as process wall time and the TLS loop as in-program time:

Reading CPUs delay 0 (default) delay 10 delay 100 (.NET's own)
A one-line program starts (published) 12 346 ms 316 ms 313 ms
A one-line program starts (published) 1 426 ms 326 ms 323 ms
A JSON, regexp, URL and template program starts (published) 12 554 ms 506 ms 506 ms
A JSON, regexp, URL and template program starts (published) 1 678 ms 510 ms 510 ms
64 TLS handshakes (JIT build) 12 1,217 ms 1,713 ms
64 TLS handshakes (JIT build) 1 2,388 ms 4,541 ms

A delay of 10, 20 or 100 ms starts a program about equally fast, so the choice is between 0 and a delay, not between delays. The suite-wide cost of .NET's 100 ms follows below.

How to set it, in any of three forms:

  • in the converted project file, <GoCallCountingDelayMs>runtime</GoCallCountingDelayMs>, or for one build or publish, dotnet publish -c Release -p:GoCallCountingDelayMs=runtime. runtime keeps .NET's own default; a number is a delay in milliseconds; empty means 0. The property applies to an executable project;
  • in the program's runtimeconfig.json, "System.Runtime.TieredCompilation.CallCountingDelayMs": 100;
  • for one run, the environment variable DOTNET_TC_CallCountingDelayMs=100, which overrides the other two.

A C# application that references converted packages keeps .NET's default unless it sets the runtimeconfig.json value itself.

Check it on your machine. Pin a published program to one CPU (taskset -c 0 on Linux, start /affinity 1 /wait on Windows) and time a few runs with DOTNET_TC_CallCountingDelayMs=0, then with 100.

Before and after, across the performance suite. On win-x64 (an AMD Ryzen 5 PRO 6650U), the performance suite's programs ran with the template's delay of 0 and with .NET's own 100 ms. These programs run for about 0.1 to 2.5 s, so the delay decides how much of each run executes before the optimized code arrives. In a long-running service the delay is only a start-up transient, so the table overstates what the setting costs once a service is warm. With the delay at 100, 13 of the 17 programs measured are slower under the JIT and 11 under ReadyToRun; Startup is faster under both. Medians in ms, in-program time except Startup's process wall time; the ratio is delay 100 against delay 0:

Benchmark JIT, delay 0 JIT, delay 100 ratio ReadyToRun, delay 0 ReadyToRun, delay 100 ratio
Startup 502.0 483.5 0.963 371.5 345.5 0.930
Fib 97.6 213.4 2.186 101.4 213.2 2.102
String 339.0 734.3 2.166 within noise
TlsHandshake 1071.6 2053.3 1.916 1078.2 1688.7 1.566
Map 253.6 470.9 1.857 248.7 466.1 1.874
Sort 255.1 434.8 1.704 255.0 437.6 1.716
Channel 80.7 126.1 1.563 within noise
Iface 374.7 558.8 1.491 380.9 616.7 1.619
RefLower 511.3 752.4 1.472 536.8 761.2 1.418
IfaceShellChurn 1076.8 1574.1 1.462 1090.9 1269.0 1.163
StringMatch 791.2 1125.8 1.423 838.8 1096.0 1.307
IfaceCall 358.3 435.8 1.216 349.0 506.5 1.451
IfaceShell 600.4 723.0 1.204 575.0 770.4 1.340
FieldView 1723.7 1959.8 1.137 1936.5 2089.3 1.079

MatMul, Sieve and StringView stay within measurement noise on both. Native AOT has no tiers, so the delay does not apply to it.

One setting to change on one runtime. Quick JIT for loops is what lets the JIT replace a running loop with an optimized one. Ubuntu's packaged .NET 10, rather than Microsoft's build, can fail in that path on Linux; Known issues describes the fault and when it applies. Turning quick JIT for loops off avoids it. On linux-x64, under Canonical's .NET 10 build, that cost a converted program roughly a sixth more start time, and its loop speed stayed within measurement noise.

Two places in go2cs turn tiering off on purpose, and neither is advice.

  • Converted test hosts. A -tests host runs with tiered compilation off by default, so every method is compiled once at full optimization and a test verdict cannot depend on when the runtime promotes a method. -test-tiered turns it back on. That trades speed for determinism, which a test needs and a program does not; a production program should not copy it.
  • The performance runner's diagnostic arms. The performance suite can add a run with tiered compilation off, and measurements of individual changes also read runs with dynamic PGO off. They separate a cost the tiers recover from a cost the code always pays. They are measurement axes, not settings to deploy: a change that reads smaller with tiering off is still faster with the defaults.

Converter and runtime options

Most converter options change how the emitted C# reads, or which code is converted. They do not change what the program costs to run. These do.

String literals

By default a converted program keeps a string literal where Go has it: F("hello"). golib converts each literal from C#'s UTF-16 to Go's UTF-8 once and caches the result, so each later evaluation is a lookup that allocates nothing. A string constant is one field, as Go declares it.

-hoist-literals is the optimization option. It gives each literal whose words make a meaningful name a named field, F(helloˢ), and it reads every such field, and every string or other struct-typed constant, through a small accessor under the same name; every other literal ("%v", ", ") keeps the plain cached form, so no literal is copied on each evaluation. The JIT and Native AOT both inline that accessor into the code that reads it; they would not inline a function that reads the field directly. The standard library is converted this way already, so code that calls into it gets the benefit without the flag. A constant block shows the difference:

const (
	Version = "1.0"
	Name    = "server"
)
// without the flag
public static readonly @string Version = "1.0"u8;
public static readonly @string Name = "server"u8;

// with -hoist-literals
public static ref readonly @string Version => ref Versionᵇ;
private static readonly @string Versionᵇ = "1.0"u8;
public static ref readonly @string Name => ref Nameᵇ;
private static readonly @string Nameᵇ = "server"u8;

Every use still reads Version and Name, so the Go function bodies are unchanged.

When it helps: a small function that returns or passes a literal or a constant, called from a tight loop.

  • On win-x64 (an Intel Core i9-13900K), the standard library's FormatBool, which returns one of two literals, took 0.66 ns per call through the accessor under the JIT, against 1.41 ns when it read the field directly and 1.07 ns with a plain literal. Under Native AOT it took 0.75 to 1.02 ns through the accessor, against 1.13 to 1.29 ns reading the field.
  • On the same machine, a loop calling a small standard-library function that returns a literal took roughly twice as long with plain literals under the JIT, once the JIT had optimized the function, and roughly 1.7 to 1.8 times as long under Native AOT. A smaller such function took up to a quarter longer under Native AOT.
  • On win-x64 (an AMD Ryzen 5 PRO 6650U):
    • a literal returned from a small function called in a hot loop took roughly 1.7 to 1.8 times as long plain, under both the JIT and Native AOT;
    • a literal argument inside the loop took roughly 1.6 times as long under the JIT, and a sixth longer under Native AOT;
    • a local assigned from a literal took between a tenth and a seventh longer.
  • In whole programs, a literal-heavy string-matching benchmark runs faster hoisted under Native AOT on both machines measured on win-x64: about 9% faster on an AMD Ryzen 5 PRO 6650U and about 5% on an Intel Core i9-13900K. On the Ryzen it runs about a quarter faster hoisted under the JIT. Parsing URL queries took about the same time either way.

The second and third bullets, and the URL-query reading, were measured against the direct field read; the accessor reads faster than that field.

Before and after, across the performance suite. Of the 17 performance suite programs measured, only StringMatch moves beyond measurement noise with -hoist-literals. The other 16 stay within noise under both the JIT and ReadyToRun, including TlsHandshake and StringView, which also get hoisted fields. Native AOT was timed for StringMatch only. Medians in ms, in-program time; the ratio is hoisted against plain:

Machine (win-x64) Deployment plain -hoist-literals ratio
AMD Ryzen 5 PRO 6650U JIT 791.2 594.7 0.752
AMD Ryzen 5 PRO 6650U ReadyToRun 838.8 566.8 0.676
AMD Ryzen 5 PRO 6650U Native AOT 1288.5 1171.8 0.909
Intel i9-13900K Native AOT 582.1 550.0 0.945

When it does not: a literal evaluated rarely costs one lookup either way. On win-x64 (an AMD Ryzen 5 PRO 6650U), a literal passed as an any argument, fmt.Println("x"), measured the same both ways: both forms pass one pre-boxed value. Measure your own hot path.

What it costs: one more declaration line per literal and per constant. The literal's field is declared above the function that first uses it, and each constant becomes an accessor plus a private field. Go function bodies are unchanged. Plain literals describes both forms.

Goroutine stacks

By default, each goroutine other than main runs on its own thread, and the thread reserves a fixed stack, because a .NET stack cannot grow the way a Go stack does. The reserve is 256 MiB, or 16 MiB for a goroutine the converter proves cannot recurse (how). main runs on a thread that reserves 1 GiB, Go's own limit, and the settings below do not change it. It is address space, not memory, but it is what a thread start pays for: on win-x64, parking 100,000 goroutines at once took roughly five times as long with the 256 MiB reserve as with 16 MiB. On linux-x64, at 10,000 goroutines, the reserve made no measurable difference, and a kernel limit on memory mappings, not the reserve, stopped thread creation at around 16,000.

GO2CS_GOROUTINE_STACK sets the 256 MiB reserve and GO2CS_GOROUTINE_BOUNDED_STACK the 16 MiB one, as a byte count with an optional KiB, MiB or GiB suffix; 0 takes .NET's default thread stack. Lower the first only for a program that starts many goroutines at once and never recurses deeply in one: recursion past the reserve ends the process with no panic and no deferred calls (Limitations).

Options that do not change run-time cost

Option Why it is not a tuning option
-var, -uco, -indent, -comments, -provenance, -license, -tree Change how the emitted C# reads or what metadata it carries; the program does the same work.
-goroot, -gopath, -go2cspath, -stdlib, -recurse, -csproj, -tags, -platforms, -cgo Choose which Go code is converted and for which platforms, not how it runs.
-debug, -convert-timeout, -allow-stale-converter, -platform-census, -platform-stage, -ref-census, -version, -nuget-map, -nuget-map-*, -module-* Govern the conversion itself, or run an analysis that emits no C#.
-dual-recv, -dual-recv-params Experimental, for converter development, and off by default: eligible pointer-receiver methods also emit a form that takes the receiver by reference. Their output is not validated, and their run-time cost is not measured.
-tests, -test-* Converted test hosts only; -test-tiered is covered above.
GOGC, GOMEMLIMIT Read and reported, but .NET's collector does not follow them (Limitations).
GO2CS_GC_PAUSE_HISTORY=0 Changes what runtime reports about garbage collection, not how the program runs.
golib's FieldViewCache.Disabled A switch for one test, which checks that &s.f stays one pointer as it does in Go. Setting it breaks that.