Skip to content

Store linear memory in a local variable across non-growing spans. - #68

Closed
msuozzo wants to merge 2 commits into
ncruces:mainfrom
msuozzo:push-osnpzwuknutl
Closed

msuozzo wants to merge 2 commits into
ncruces:mainfrom
msuozzo:push-osnpzwuknutl

Conversation

@msuozzo

@msuozzo msuozzo commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Generated functions address linear memory as m.memory (or *m.memory)
on every access, so Go reloads the slice header after every store and
cannot reuse bounds checks across adjacent loads and stores.

Instead of loading the slice on every read, store mem := m.memory in a
local variable (an overhead of one load) and re-assign it it only after
calls that may grow the underlying memory slice. Apply this when it
reduces slice loads outright, or when a call-free function has a loop.

Benchmarks on go-sqlite3 (wasm2go -unsafe):

goos: linux
goarch: amd64
cpu: AMD EPYC 7B13
pkg: github.com/ncruces/go-sqlite3/driver
      │   before    │               after               │
      │   sec/op    │   sec/op     vs base              │
_loop   582.1m ± 0%   537.1m ± 1%  -7.72% (p=0.000 n=8)

pkg: github.com/ncruces/go-sqlite3/ext/stats
          │    before    │               after               │
          │    sec/op    │   sec/op     vs base              │
_average     92.19n ± 0%   84.03n ± 0%  -8.85% (p=0.000 n=8)
_variance   108.65n ± 1%   99.46n ± 2%  -8.45% (p=0.000 n=8)
geomean      100.1n        91.42n       -8.65%

pkg: github.com/ncruces/go-sqlite3/vfs/tests/speedtest1
            │   before    │               after               │
            │   sec/op    │   sec/op     vs base              │
_speedtest1   72.40m ± 1%   70.13m ± 1%  -3.15% (p=0.000 n=8)

@msuozzo

msuozzo commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

meta comment: not at all picky about how this is implemented but I think it's a worthwhile optimization. it's relatively simple and I don't think there's any way to avoid the overhead it addresses: len() can't be elided/trusted across potential slice grows so the go compiler isn't able to optimize this case.

@ncruces

ncruces commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

I think I had tried doing this in the "front end" (not as an "optimization pass") only for the case where the memory is a pointer (shared), and didn't see such gains.

I guess I'll have to take a deeper look.

msuozzo and others added 2 commits November 11, 2026 15:07
Generated functions address linear memory as `m.memory` (or `*m.memory`)
on every access, so Go reloads the slice header after every store and
cannot reuse bounds checks across adjacent loads and stores.

Instead of loading the slice on every read, store `mem := m.memory` in a
local variable (an overhead of one load) and re-assign it it only after
calls that may grow the underlying memory slice. Apply this when it
reduces slice loads outright, or when a call-free function has a loop.

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

goos: linux
goarch: amd64
cpu: AMD EPYC 7B13
pkg: github.com/ncruces/go-sqlite3/driver
      │   before    │               after               │
      │   sec/op    │   sec/op     vs base              │
_loop   582.1m ± 0%   537.1m ± 1%  -7.72% (p=0.000 n=8)

pkg: github.com/ncruces/go-sqlite3/ext/stats
          │    before    │               after               │
          │    sec/op    │   sec/op     vs base              │
_average     92.19n ± 0%   84.03n ± 0%  -8.85% (p=0.000 n=8)
_variance   108.65n ± 1%   99.46n ± 2%  -8.45% (p=0.000 n=8)
geomean      100.1n        91.42n       -8.65%

pkg: github.com/ncruces/go-sqlite3/vfs/tests/speedtest1
            │   before    │               after               │
            │   sec/op    │   sec/op     vs base              │
_speedtest1   72.40m ± 1%   70.13m ± 1%  -3.15% (p=0.000 n=8)
@ncruces

ncruces commented Nov 11, 2026

Copy link
Copy Markdown
Owner

So you were very liberal in mayGrowMemory. AFAICT, any host call can grow memory, especially for imported memories. Same for provided functions. And if I become conservative, it seems the benefit mostly goes away.

I'm a bit unsure what to do here.

@msuozzo

msuozzo commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

So you were very liberal in mayGrowMemory. AFAICT, any host call can grow memory, especially for imported memories. Same for provided functions. And if I become conservative, it seems the benefit mostly goes away.

I'm a bit unsure what to do here.

Hmm I tested a precise version as well which was more code for the same speedup. Let me try to see what's going on.

@msuozzo

msuozzo commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@ncruces how are you measuring? on your commit, the sqlite3 benchmarks still show 5-10% improvement both on 1.26 and 1.27 for me.

@msuozzo msuozzo closed this Nov 11, 2026
@msuozzo msuozzo reopened this Nov 11, 2026
@ncruces

ncruces commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Well for whatever it's worth, I also noticed more modest speedups on my Xeon W-2135 compared to your EPYC 7B13 to begin with.

@ncruces

ncruces commented Nov 12, 2026

Copy link
Copy Markdown
Owner

I find #74 much easier to reason about, what do you think @msuozzo?

@msuozzo

msuozzo commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

I find #74 much easier to reason about, what do you think @msuozzo?

#74 (comment) 👍

@ncruces ncruces closed this Nov 12, 2026
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