Skip to content

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

Draft
th0114nd wants to merge 1 commit into
zero-alloc-tags-02-serializefrom
zero-alloc-tags-03-hash
Draft

internal/tags: add HashTags/TagSet.Hash for zero-alloc tag hashing#187
th0114nd wants to merge 1 commit into
zero-alloc-tags-02-serializefrom
zero-alloc-tags-03-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.

Reuses the gatherValidTags/numValidTags helpers and stack-tier
sizing introduced for SerializeTags: pairs are gathered into a small
stack array (or a single exact-size heap allocation past 16 tags)
and sorted via TagSet.Sort(), then the serialized name+tags bytes
are built into a similarly-tiered stack buffer (128/256/512 bytes,
heap beyond that) and hashed directly, without ever needing a heap
allocation for the common case.

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.

Reuses the gatherValidTags/numValidTags helpers and stack-tier
sizing introduced for SerializeTags: pairs are gathered into a small
stack array (or a single exact-size heap allocation past 16 tags)
and sorted via TagSet.Sort(), then the serialized name+tags bytes
are built into a similarly-tiered stack buffer (128/256/512 bytes,
heap beyond that) and hashed directly, without ever needing a heap
allocation for the common case.

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>
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