r/golang • • 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.

cd benchmarks/compare
GOWORK=off CGO_ENABLED=0 GOTOOLCHAIN=go1.26.11 go test -mod=mod -run '^$' -bench '^BenchmarkUp100_' -benchmem -count=6 .

Benchmark source · golang-migrate buffer implementation

9 Upvotes

5 comments sorted by

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/Fabulous-Cookie-6457 10d ago

Yes. I reran the same 100 small migrations from actual .sql files using os.DirFS. SQLite was on disk in both runs. The only change was where the runner read the SQL from.

With golang-migrate v4.20.1, the default 100,000-byte buffer used 10.69 MiB per batch with MapFS and 13.91 MiB with DirFS. With a 4 KiB buffer, those figures were 0.91 and 4.14 MiB. So reading real files did increase total allocations, but switching to the smaller buffer saved almost exactly the same amount in both cases, about 9.8 MiB per batch. These are medians from three fresh runs per setting.

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/Fabulous-Cookie-6457 10d ago

Yes, the smaller buffer still saved about 9.75 MiB per batch with larger files. I tested 100 migrations from disk, each with a 128 KiB string literal in an INSERT. golang-migrate v4.20.1 allocated 56.76 MiB with its default buffer and 47.01 MiB with a 4 KiB buffer. The absolute saving was about the same as with small files, but it accounted for a smaller share of the total.

What surprised me was our own result. Our v1.0.1 allocated 107.08 MiB on that workload. Profiling showed we were making extra copies of SQL that already had normalized line endings. Our transaction check also allocated space for a whole literal just to replace it with two quote characters.

We fixed those paths in v1.1.1 and measured 41.02 MiB per batch. The 4 KiB golang-migrate configuration still had the lower median runtime in those three runs, 63.7 ms versus our 76.3 ms. I only tested fixed buffer sizes, so I don’t have measurements for Stat().Size() or sync.Pool. Code change: https://github.com/Go-Gen-Ecosystem/go-migrate/commit/6bae73ca46e1ec56eaa215d22fd4306072960cdf

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?