Benchmarks
These numbers measure the container alone: every service is a pointer that is already built, so what’s left is the cost of finding it. In every benchmark the container holds 32 services, about what an app wires at startup, and the one read is in the middle of them.
v2 against v1
Section titled “v2 against v1”Both versions ran on the same harness. v1’s Invoke calls are measured against the v2 calls that replace them:
where v1 handed a service to a func through reflection, v2’s Get returns it typed.
With 16 goroutines at once, the time is the wall time divided by all of their reads, as go test reports it:
it measures throughput, not how long one read takes.
Show as a table
| Scenario | v1 | v2 |
|---|---|---|
| Get | 5.8 ns0 allocs | 3.6 ns0 allocs |
| Get, not registered | 4.8 ns0 allocs | 3.3 ns0 allocs |
| MustGet | 6.4 ns0 allocs | 3.5 ns0 allocs |
| Has | 6.2 ns0 allocs | 3.4 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Show as a table
| Scenario | v1 | v2 |
|---|---|---|
| Invoke in v1, Get in v2 | 80.9 ns0 allocs | 3.6 ns0 allocs |
| MustInvoke in v1, MustGet in v2 | 71.0 ns0 allocs | 3.5 ns0 allocs |
| Three services: MustInvokeMany in v1, three MustGets in v2 | 162.7 ns5 allocs | 10.2 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Show as a table
| Scenario | v1 | v2 |
|---|---|---|
| Get from 16 goroutines at once | 128.1 ns0 allocs | 0.27 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Show as a table
| Scenario | v1 | v2 |
|---|---|---|
| Register, replacing a service | 9.5 ns0 allocs | 224.6 ns5 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
v1 read under a read-write lock. Every reader writes to the lock’s shared counter, so readers on different cores queue for it, and reading from 16 goroutines at once cost more per read than reading from one. v2 keeps its services in a map it never changes once it’s published: a read is an atomic load and a lookup, and readers don’t touch anything another core writes.
The price is on the other side. To change the map, Register and Deregister copy it and swap the copy in,
so they are slower than v1’s and grow with the number of services. Services are registered once, at startup,
and read for as long as the program runs, so v2 makes that trade.
v2’s own suite
Section titled “v2’s own suite”| Benchmark | Time | Memory | Allocations |
|---|---|---|---|
GetGet a registered service | 3.5 ns | 0 B | 0 |
GetNotRegisteredGet under a key with nothing registered | 3.4 ns | 0 B | 0 |
MustGetMustGet a registered service | 3.4 ns | 0 B | 0 |
HasCheck for a registered service | 3.3 ns | 0 B | 0 |
GetParallelGet from 16 goroutines at once | 0.26 ns | 0 B | 0 |
Register/1Replace the only service | 52.7 ns | 264 B | 3 |
Register/32Replace one of 32 services | 216.8 ns | 1888 B | 5 |
Register/1024Replace one of 1024 services | 4.71 µs | 54664 B | 7 |
Run them yourself
Section titled “Run them yourself”From a clone of the repository:
go test -run '^$' -bench . -benchmemTo compare two versions, run each a few times and compare with benchstat:
go test -run '^$' -bench . -count 10 > new.txtbenchstat old.txt new.txtThe allocation counts are also enforced by tests: TestAllocations fails if a call allocates more than it should.