Skip to content

Conversation

@daboyuka
Copy link
Contributor

@daboyuka daboyuka commented Apr 9, 2025

internal/lz4block/blocks.go incurs an allocation on every call to Put, even when the pool is already expanded, because putting. a []byte into an interface{} requires a heap allocation of the slice header. We can avoid this by using array pointers without changing the Get and Put function signatures.

This change does require Go 1.17 minimum. If this is an issue, I can make another version of this PR with build tags to conditionally use this new technique.

My first time contributing to this repo; open to any feedback!

Before 2nd commit on this branch:

$ go test -test.run='^$' -bench '.*' -benchmem github.com/pierrec/lz4/v4/internal/lz4block
goos: darwin
goarch: arm64
pkg: github.com/pierrec/lz4/v4/internal/lz4block
cpu: Apple M1 Pro
BenchmarkGetPut-10    	49272153	        24.25 ns/op	      24 B/op	       1 allocs/op

After 2nd commit:

$ go test -test.run='^$' -bench '.*' -benchmem github.com/pierrec/lz4/v4/internal/lz4block
goos: darwin
goarch: arm64
pkg: github.com/pierrec/lz4/v4/internal/lz4block
cpu: Apple M1 Pro
BenchmarkGetPut-10    	135518984	         8.707 ns/op	       0 B/op	       0 allocs/op

@pierrec pierrec merged commit 71a9b63 into pierrec:v4 Apr 11, 2025
0 of 9 checks passed
@pierrec
Copy link
Owner

pierrec commented Apr 11, 2025

Thank you.

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