nakagami / firebirdsql

Firebird RDBMS sql driver for Go (golang)
MIT License
227 stars 60 forks source link

Avoid intermediate allocations for wire protocol conversions #70

Closed cheald closed 5 years ago

cheald commented 5 years ago

utils.go performs a lot of intermediate buffer allocations for converting byte sequences to int32, etc. This results in a lot of unnecessary allocations which can pretty substantially slow down reads.

I added a small benchmark in decode_speed_test.go. It shows off the difference pretty clearly:

(master) $ go test -bench=. -run=XXnoMatch -count 5 -benchmem
goos: linux
goarch: amd64
pkg: github.com/nakagami/firebirdsql
BenchmarkRead-12            3000            565649 ns/op           65462 B/op       1398 allocs/op
BenchmarkRead-12            3000            593914 ns/op           65441 B/op       1398 allocs/op
BenchmarkRead-12            2000            582589 ns/op           65445 B/op       1398 allocs/op
BenchmarkRead-12            3000            579606 ns/op           65460 B/op       1398 allocs/op
BenchmarkRead-12            2000            603802 ns/op           65432 B/op       1398 allocs/op
PASS
ok      github.com/nakagami/firebirdsql 20.087s

>> Average 585112ns/row read

(perf-fixes) $ go test -bench=. -run=XXnoMatch -count 5 -benchmem
goos: linux
goarch: amd64
pkg: github.com/nakagami/firebirdsql
BenchmarkRead-12            3000            397414 ns/op           12872 B/op        156 allocs/op
BenchmarkRead-12            3000            384127 ns/op           12855 B/op        156 allocs/op
BenchmarkRead-12            3000            406941 ns/op           12857 B/op        156 allocs/op
BenchmarkRead-12            5000            430075 ns/op           12850 B/op        156 allocs/op
BenchmarkRead-12            3000            417873 ns/op           12844 B/op        156 allocs/op
PASS
ok      github.com/nakagami/firebirdsql 19.745s

>> Average 407286ns/row read

In this case, my synthetic benchmark runs about 43% faster just by removing the intermediate allocations. I have production DBs with several hundred integer columns, which saw ~300% speedups with this change.

nakagami commented 5 years ago

It looks very nice, thanks.