Skip to content

Widen 32-bit linear memory addresses to uint64 in popAddr under -unsafe. - #70

Merged
ncruces merged 2 commits into
ncruces:mainfrom
msuozzo:push-opquuxvmutrt
Nov 9, 2026
Merged

ncruces merged 2 commits into
ncruces:mainfrom
msuozzo:push-opquuxvmutrt

Conversation

@msuozzo

@msuozzo msuozzo commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Generated 32-bit memory accesses use uint32(addr) at offset 0 and
uint64(uint32(addr))+offset otherwise, so Go's type-keyed SSA cannot
share zero-extensions or bounds-check proofs across uses of addr.

Under -unsafe, where load and store helpers inline into large callers,
always widen 32-bit addresses to uint64(uint32(addr)). Safe mode keeps
uint32(addr) at offset 0: its helpers are not inlined into large
callers, so widening shares nothing across the call and only adds
zero-extensions and spills in the caller. The safe-mode carve-out can be
removed once the safe helpers inline, see #69.

Benchmarks on go-sqlite3 (wasm2go -unsafe):

goos: linux
goarch: amd64
pkg: github.com/ncruces/go-sqlite3/driver
cpu: AMD EPYC 7B13
      │   before    │               after               │
      │   sec/op    │   sec/op     vs base              │
_loop   543.5m ± 2%   537.3m ± 2%  -1.14% (p=0.001 n=8)

pkg: github.com/ncruces/go-sqlite3/ext/stats
          │   before    │               after               │
          │   sec/op    │   sec/op     vs base              │
_average    84.39n ± 1%   81.96n ± 1%  -2.88% (p=0.003 n=8)
_variance   99.65n ± 1%   98.34n ± 8%       ~ (p=0.129 n=8)
geomean     91.70n        89.78n       -2.10%

pkg: github.com/ncruces/go-sqlite3/vfs/tests/speedtest1
            │   before    │               after               │
            │   sec/op    │   sec/op     vs base              │
_speedtest1   69.11m ± 1%   68.58m ± 2%  -0.77% (p=0.050 n=8)

Generated 32-bit memory accesses use `uint32(addr)` at offset 0 and
`uint64(uint32(addr))+offset` otherwise, so Go's type-keyed SSA cannot
share zero-extensions or bounds-check proofs across uses of `addr`.

Under `-unsafe`, where load and store helpers inline into large callers,
always widen 32-bit addresses to `uint64(uint32(addr))`. Safe mode keeps
`uint32(addr)` at offset 0: its helpers are not inlined into large
callers, so widening shares nothing across the call and only adds
zero-extensions and spills in the caller. The safe-mode carve-out can be
removed once the safe helpers inline, see ncruces#69.

Benchmarks on `go-sqlite3` (`wasm2go -unsafe`):

goos: linux
goarch: amd64
pkg: github.com/ncruces/go-sqlite3/driver
cpu: AMD EPYC 7B13
      │   before    │               after               │
      │   sec/op    │   sec/op     vs base              │
_loop   543.5m ± 2%   537.3m ± 2%  -1.14% (p=0.001 n=8)

pkg: github.com/ncruces/go-sqlite3/ext/stats
          │   before    │               after               │
          │   sec/op    │   sec/op     vs base              │
_average    84.39n ± 1%   81.96n ± 1%  -2.88% (p=0.003 n=8)
_variance   99.65n ± 1%   98.34n ± 8%       ~ (p=0.129 n=8)
geomean     91.70n        89.78n       -2.10%

pkg: github.com/ncruces/go-sqlite3/vfs/tests/speedtest1
            │   before    │               after               │
            │   sec/op    │   sec/op     vs base              │
_speedtest1   69.11m ± 1%   68.58m ± 2%  -0.77% (p=0.050 n=8)
@ncruces

ncruces commented Nov 7, 2026

Copy link
Copy Markdown
Owner

At that point, do generics make sense?

@msuozzo

msuozzo commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

At that point, do generics make sense?

yeah so i think they're necessary to keep safe mode fast (until #69). safe will still use both generic variants until then so i think it's reasonable to keep them around.

to put a finer point on it: i found pretty rough regressions (~100% added cpu time) using uint64 everywhere. the go compiler change mentioned in #69 would unblock it if/when it's merged.

@ncruces ncruces left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I just inverted the condition as that reads better to me.

@ncruces
ncruces merged commit 96d01f8 into ncruces:main Nov 9, 2026
2 checks passed
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.

2 participants