Repository navigation
Conversation
MeterId's constructor ran std::regex_replace on the name and on every tag key and value. Callers that create meters per measurement, such as NDrive, paid that on every record: building a MeterId with six tags drops from about 4.9 us to 0.8 us with a byte loop over the same character class. The spectatord id is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
andersvw
marked this pull request as ready for review
October 15, 2026 19:56
Review feedback: building the id by appending ReplaceInvalidChars results grew the string and made a temporary string per key and value. Each byte maps to one byte, so the size is known up front: reserve it once and append the replaced bytes directly. ToSpectatorId now allocates once regardless of tag count (was 4 with 2 tags, 10 with 6), and a six-tag MeterId drops from about 713 ns to 648 ns. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
brharrington
approved these changes
Oct 15, 2026
Contributor
|
@andersvw @brharrington Can we pause on merging this until I review? I have a branch I wrote 5 months ago that had these exact same changes. I'd like to compare the two before merging. |
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.
MeterId's constructor runsstd::regex_replaceon the name and on every tag key and value to build the spectatord id. This replaces the regex with a byte loop over the same character class ([-._A-Za-z0-9~^]), and builds the id with string appends instead of anostringstream. The spectatord id is unchanged byte for byte: non-ASCII input is still replaced one byte at a time.Registry::Create*(name, tags)builds twoMeterIds per call, one from the tags and one fromWithTagsfor the configured extra tags. Callers that create meters per measurement pay that cost on every record.Benchmarks
clang 19,
-O2, an 8-vCPU Linux container,mainat 656bf58.performance_tests/performance_test.cpp, unmodified (CreateCounter(name, tags).Increment()with 2 tags), with the UDS writer sending to a socket that discards what it receives. One 2-minute run each:mainConstructing a
MeterIdtakes, in ns per id (median of 3 runs of 200K):mainPer-thread times with 4 and 8 threads were within 5% of these, so the cost is the regex work itself, not contention.
In NDrive, which creates several meters per FUSE read, this change cut CPU per cached 128 KiB read from 261 µs to 208 µs, even with its meters already cached.
Testing
New tests cover invalid characters in tag keys and values, every valid character, and multi-byte UTF-8. Built standalone with clang 19 against libstdc++, the new tests pass both with and without this change. In that environment,
HashSameTags,LookupTagsandSpectatordIdalso fail onmain, because they depend onunordered_mapiteration order, and they fail the same way with this change. I have not run the gcc-15/conan build.Originally written by Colin Howes.
🤖 Generated with Claude Code