Repository navigation
[Go] Size struct-codec groups from decoded entries, not the wire count - #1143
Open
slachiewicz wants to merge 1 commit into
Open
slachiewicz wants to merge 1 commit into
slachiewicz wants to merge 1 commit into
Conversation
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.
When decoding from an
io.Reader, the struct codecs allocatedmake([]T, numInGroup)from the count on the wire, so a 4-byte group header claiming 65534 entries madeCar.Decodeallocate 2 MiB before reading any entry. A new slice now starts at no more than 1024 entries and grows as entries decode; a slice the caller passes in with enough capacity is reused as before. If decoding fails partway through a group, the slice now holds the entries decoded so far rather than the full count.1024 keeps a single allocation for typical group sizes while bounding what a header alone can claim (about 32 KiB for
Car's fuel figures).Verified: the new
GroupCapacity_test.gofails on master (2097200 bytes allocated) and passes with this change;make testingocodepasses. On arm64 Linux, decoding a freshCarallocates the same (464 B, 11 allocs) and shows no time difference beyond run-to-run noise.