Repository navigation
Preallocate WKB output buffer in AsBinary using exact byte count - #719
Merged
peterstace merged 2 commits intoNov 13, 2026
Conversation
peterstace
requested changes
Nov 13, 2026
peterstace
left a comment
Owner
There was a problem hiding this comment.
Thanks for the PR! I've got some requests for change.
peterstace
approved these changes
Nov 13, 2026
jay23606
deleted the
pr-go/368-preallocate-byte-slices-in-asbinary-meth
branch
November 13, 2026 21:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Geometry.AsBinaryand the per-typeAsBinarymethods calledAppendWKB(nil). A nil slice has zero capacity, so the output buffer was regrown several times while the WKB was written.Change
bytesInWKB()method that computes the exact WKB size.AsBinaryallocatesmake([]byte, 0, g.bytesInWKB())and appends into it, so the buffer is allocated once.capequalslenfor each geometry type and thatAsBinarymakes exactly one allocation. A second test checks thatbytesInWKBmatcheslen(AsBinary()).BenchmarkMarshalWKBnow callsb.ReportAllocs(), so the allocation count is reported alongside timing.Testing
go test ./geom/ -run TestBytesInWKBMatchesAsBinary -count=1passes on the fix. On the base commit it fails (go -C .pr-go/base test ./geom/ -run TestBytesInWKBMatchesAsBinary -count=1).go test ./geom/ -run TestBytesInWKB -count=1 -vpasses on the fix, covering both the length check and the allocation check.go vet ./geom/is clean.go test ./geom/ -run ^$ -bench BenchmarkMarshalWKB/polygon -benchtime=200x(one run each, so the timings are noisy): on the fix, the n=10000 polygon is 34,124 ns/op with 1 allocs/op. On base, it is 85,596 ns/op.go test ./geom/ -count=1has one failure,TestGeoJSONSyntaxError.Regression test: fails on base (
go -C .pr-go/base test ./geom/ -run TestBytesInWKBMatchesAsBinary -count=1), passes on fix (go test ./geom/ -run TestBytesInWKBMatchesAsBinary -count=1)Fixes #368