Async twins: goroutines on a small pool of threads

By default, each goroutine in a converted program runs on its own operating-system thread. A goroutine that waits, on a channel, a lock or a timer, keeps that thread while it waits. Go itself runs goroutines on a small pool of threads instead: a goroutine that waits gives its thread back, and another goroutine runs on it.

Async twins let a converted program work the same way. They are experimental and opt-in: a program gets them only when it is built with twins and run with the scheduler switched on, as How to opt in describes. A program that does not opt in runs exactly as it does without them.

What an async twin is

An async twin is a second copy of a function that can wait without holding a thread. The source generator in every converted project writes it beside the function, with the same name followed by ꞏtwin. Where the function blocks its thread, its twin parks: it returns to the scheduler, which runs another goroutine on the same thread, and the goroutine resumes, on whichever thread is free, when what it waited for arrives.

The twin of a function returns a task, a GoTask (or a GoTask<T> for a result), and its body awaits the twins of the functions it calls. So a goroutine started on a twin runs as a chain of twins, each awaiting the next, down to the operation that actually waits: a channel send or receive, a select, or a time.Sleep. A sync.Mutex.Lock that has to wait parks as well, but only inside a function that can also wait on one of those (Which code gets twins).

A public function that can never wait still gets a twin, so every caller can await it the same way. That twin is a forwarder: it calls the plain function and returns a task that has already completed.

You do not call twins, and you do not see them in your converted code. The generator writes them into the build, and the converted program’s entry point starts main’s twin when the program is built with twins.

Which code gets twins

How to opt in

Opting in takes two switches: one when you build, one when you run.

The build. Build the projects with twins. The properties that do this are read by go.lib’s build files, so they work for any converted program: one built against a go2cs repository clone, against a deploy-core root, or against the go.* NuGet packages. The published 1.26.11.1 packages carry no twins at all; twins arrive with the packages of the next release (.5).

Upgrading a program converted before this release: re-run the conversion, so the output root’s Directory.Build.targets imports go.lib’s twin file. A NuGet consumer needs nothing more than the new packages.

Setting GoTwins on the command line is not the same thing: a command-line property reaches every project the build references, standard-library packages included. Use GoTwinsFor on the command line instead.

The run. Set the environment variable GO2CS_MN=1 when you start the program. How many goroutines run at once follows GOMAXPROCS, as in Go: the GOMAXPROCS environment variable at start, and runtime.GOMAXPROCS(n) while the program runs.

Without GO2CS_MN=1, a program built with twins runs each goroutine on its own thread, as it would without them.

How to tell it is on. Run with GO2CS_MN_READING=1 as well. The scheduler then writes one line to standard error when the program exits, starting READING go-scheduler, with what it counted: parks= is how many times a goroutine parked, and peak-os-threads= is the most threads the program ran at once. No line means the scheduler never started: GO2CS_MN was not 1, or no goroutine started on a twin, for example because the executable was not named in GoTwinsFor. The line is for reading, and its fields may change between releases.

An example. A converted program whose executable project is App.csproj and whose library project is Lib.csproj:

dotnet build -c Release -p:GoTwinsFor=App%3BLib App.csproj
GO2CS_MN=1 GO2CS_MN_READING=1 dotnet run -c Release --no-build --project App.csproj

What you get, and what it costs

What you get. A program with many goroutines that spend most of their time waiting no longer needs a thread for each of them. Goroutines that wait in twinned code park and give their thread back, so the program runs on a pool of threads near GOMAXPROCS in size instead of one per waiting goroutine.

What stays on a thread. A goroutine still holds its thread while it is inside a plain call that waits: a call into code built without twins , a function whose only wait is a sync.Mutex.Lock, a hand-written body, a native call, or a call the generator could not make awaitable (the diagnostics below name each such site). The scheduler adds a thread when all of its threads are held this way, so the program keeps running, but those goroutines cost a thread each, as they do by default.

Build cost. A project built with twins compiles a twin for each public member, so its build takes a little longer and its assembly grows by a little. A project built without twins is unchanged.

Run cost. A call through a twin that does not park costs a few nanoseconds more than the plain call, and a park and resume costs under a microsecond (measured on Windows x64). Code that waits rarely and computes a lot gains nothing from twins.

Limits

Diagnostics

These come only from a build with twins. Each has a row in Diagnostic codes.

With WarningsAsErrors, the warnings stop the build.

Future

A future release may enable async twins by default. Until then they are opt-in, and a program that does not opt in runs each goroutine on its own thread.