Repository navigation
Conversation
|
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. |
|
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. |
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)
9df3263 to
0c6280f
Compare
|
So you were very liberal in 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. |
|
@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. |
|
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. |
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.memoryin alocal 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):