Fix ReadMapStrIntf rejecting bin-typed map keys - #450
Open
mleklund wants to merge 2 commits into
Open
Conversation
ReadMapStrIntf reads map keys with a plain ReadString call, which only accepts the 'str' wire type. The rest of the library already accepts 'bin' as well - ReadMapKey and ReadMapKeyZC have supported it since 2014, and this function's own zero-copy sibling, ReadMapStrIntfBytes, already goes through ReadMapKeyZC. Swaps the ReadString call for ReadMapKey, matching what ReadMapStrIntfBytes already does. Fixes tinylib#449
philhofer
reviewed
Aug 27, 2026
| } | ||
| for i := uint32(0); i < sz; i++ { | ||
| var key string | ||
| var key []byte |
Member
There was a problem hiding this comment.
Hoist the declaration of key outside of the loop and pass key[:0] to ReadMapKey. That ought to reduce the number of allocations in this loop from roughly 2keys to 1keys.
Hoist the key declaration out of the loop and pass key[:0] to ReadMapKey so the previous key's storage gets reused. The buffer grows to the longest key in the map and stops allocating after that, which takes a 32-key decode from 128 to 97 allocs. Adds a test for what buffer reuse can break: a nested map, and a short key following a longer one, both read through a small reader buffer so the refill path is exercised.
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.
ReadMapStrIntf reads map keys with a plain ReadString call, which only accepts the 'str' wire type. The rest of the library already accepts 'bin' as well - ReadMapKey and ReadMapKeyZC have supported it since 2014, and this function's own zero-copy sibling, ReadMapStrIntfBytes, already goes through ReadMapKeyZC.
Swaps the ReadString call for ReadMapKey, matching what ReadMapStrIntfBytes already does.
Fixes #449