r/golang • u/Fabulous-Cookie-6457 • 11d ago
How a read buffer changes the result of a 100-migration SQLite benchmark in Go
I maintain Go-Gen-Ecosystem/go-migrate. I compared v1.0.1 with golang-migrate/migrate v4.20.1 and pressly/goose v3.30.1. Each library applied the same 100 SQL migrations to a fresh SQLite database. Each migration created a table and inserted one row.
On Linux/amd64 with Go 1.26.11, the median allocated per batch across six samples was 727.8 KiB for go-migrate, 10.66 MiB for golang-migrate, and 1.14 MiB for goose. Goose made the fewest allocations at default settings, with 8,547 per batch compared with 9,420 for golang-migrate and 10,873.5 for go-migrate.
I repeated the golang-migrate benchmark with one change. Setting gm.DefaultBufferSize = 4096 at the start of its benchmark, down from the 100,000-byte default, brought its median to 892.2 KiB on these small files. All three libraries used in-memory migration sources, the same pure-Go SQLite driver, WAL, and synchronous=OFF.
To reproduce the defaults from a disposable v1.0.1 checkout, run the following. The tagged benchmark module needs -mod=mod to refresh its dependency metadata.
GOWORK=off CGO_ENABLED=0 GOTOOLCHAIN=go1.26.11
1
u/radioom_app 11d ago
Nice that you pinned the driver, WAL and synchronous=OFF for all three. I'm on modernc.org/sqlite too (no cgo), and allocation numbers there depend a lot on how the migration runner reads files. Did you check what happens with a real on-disk source instead of in-memory? I'd expect the 100 KB default buffer to hurt more once file reads are involved.
1
u/Used_Criticism_4631 10d ago
Thanks for publishing the reproduction command, and for isolating a single knob rather than just ranking the libraries. It's a good reminder that a fixed-size read buffer can dominate allocations when inputs are small. Sizing it from the file's Stat().Size() (capped at a max), or reusing buffers via sync.Pool across migrations, would likely remove most of that fixed cost. Do you expect the gap to hold with larger migration files?
1
u/sayboom 10d ago
As far as I remember, Migration.Buffer() allocates a bufio.Reader of DefaultBufferSize per migration, as read-ahead so the next migration can be fetched while the current one runs. So, this gap is understandable. Do you checked with other sql drivers? And if yes, are there change on time of migrations?