Skip to content

internal/tags: add HashTags/TagSet.Hash for zero-alloc tag hashing - #183

Closed
th0114nd wants to merge 1 commit into
zero-alloc-tags-01-sortingfrom
zero-alloc-tags-02-hash
Closed

internal/tags: add HashTags/TagSet.Hash for zero-alloc tag hashing#183
th0114nd wants to merge 1 commit into
zero-alloc-tags-01-sortingfrom
zero-alloc-tags-02-hash

Conversation

@th0114nd

@th0114nd th0114nd commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Adds HashTags(name, tags) and TagSet.Hash(name), which compute a
128-bit hash of the same canonical serialized form SerializeTags/
TagSet.Serialize produce, without ever materializing that string.

The serialized name+tags bytes are built into a stack-resident buffer
sized to the smallest of three tiers (128/256/512 bytes) that fits,
falling back to a single exact-size heap allocation for larger tag
sets. Tag pairs are gathered into a small stack array and sorted with
slices.SortFunc before serializing, so this performs zero heap
allocations for the common case (up to 16 tags, serializing to 512
bytes or less).

This is intended to back a hash-keyed sync.Map lookup for counters/
gauges, so that repeat calls to NewCounterWithTags et al. (cache hits)
no longer allocate on every call the way building the serialized
string today does.

Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com


Stack created with GitHub Stacks CLIGive Feedback 💬

Adds HashTags(name, tags) and TagSet.Hash(name), which compute a
128-bit hash of the same canonical serialized form SerializeTags/
TagSet.Serialize produce, without ever materializing that string.

The serialized name+tags bytes are built into a stack-resident buffer
sized to the smallest of three tiers (128/256/512 bytes) that fits,
falling back to a single exact-size heap allocation for larger tag
sets. Tag pairs are gathered into a small stack array and sorted with
slices.SortFunc before serializing, so this performs zero heap
allocations for the common case (up to 16 tags, serializing to 512
bytes or less).

This is intended to back a hash-keyed sync.Map lookup for counters/
gauges, so that repeat calls to NewCounterWithTags et al. (cache hits)
no longer allocate on every call the way building the serialized
string today does.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@th0114nd

th0114nd commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #185 (reordered: unify SerializeTags before introducing HashTags, so HashTags doesn't need an immediate rewrite).

@th0114nd th0114nd closed this Aug 3, 2026
@th0114nd
th0114nd deleted the zero-alloc-tags-02-hash branch August 3, 2026 21:51
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.

1 participant