Skip to content

testing: implement AllocsPerRun - #5772

Open
jakebailey wants to merge 1 commit into
tinygo-org:devfrom
jakebailey:testing-allocs-per-run
Open

jakebailey wants to merge 1 commit into
tinygo-org:devfrom
jakebailey:testing-allocs-per-run

Conversation

@jakebailey

Copy link
Copy Markdown
Member

This implements testing.AllocsPerRun. The current code returned 0, which causes a bunch of tests to pass even though they shouldn't, so explicitly skip those.

All GC providers already had the code for this, save for Boehm which just needs a counter under the existing GC lock.

Custom GC needs to implement this via ReadMemStats. (Though, I am of the opinion that custom GC should go away, because it was only ever added so someone could add Boehm themselves!)

The makefile is getting nasty; I have a change I want to send in the future to try and make it less repetitive, but for now it keeps doing the same copypasta as before.

@jakebailey

Copy link
Copy Markdown
Member Author

Huh, not how it behaved locally.... Will fix :)

@jakebailey
jakebailey marked this pull request as draft September 28, 2026 04:49
@jakebailey
jakebailey force-pushed the testing-allocs-per-run branch from 39df6ae to 29ab91c Compare September 28, 2026 05:34
@jakebailey
jakebailey marked this pull request as ready for review September 28, 2026 05:38
Comment thread src/runtime/gc_boehm.go
}

gcLock.Lock()
gcMallocs++

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we allowed to use an atomic here; istm that there are two calls, add(1) to increment, and return add(0) to get the current mallocs count.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, but its a double word :( ok, lock it is

@jakebailey

Copy link
Copy Markdown
Member Author
fatal error: could not start thread
FAIL	internal/synctest	0.449s

When I'm at my mac again I'll have to look into this; not the first time it's flaked

@jakebailey
jakebailey force-pushed the testing-allocs-per-run branch from 29ab91c to c094eb5 Compare September 28, 2026 15:46
@jakebailey

Copy link
Copy Markdown
Member Author

Foiled by my own netip unskip 😄

Allocation-sensitive standard library tests currently receive a
constant zero, hiding regressions and optimization opportunities. Real
measurements expose existing differences from upstream, so keep those
tests excluded until TinyGo meets their allocation budgets.
@dgryski

dgryski commented Sep 29, 2026

Copy link
Copy Markdown
Member

Because our escape analysis is not as good as Big Go's, this causes the following packages in the test corpus to fail:

cespare/xxhash
julienschmidt/httprouter
soypat/piudf

These all have tests expecting zero allocations and getting 1. There are a few other packages that use AllocsPerRun (golang/text, golang/image) but they have enough wiggle room that we're able to get by.

This is not to say we shouldn't merge this, but it does mean it quickly exposes other places we need to beef up the escape analysis.

@davecheney davecheney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants