Skip to content

Widen CompiledMetric tag-cache key from 32 to 57 bits - #425

Merged
gmalette merged 2 commits into
Shopify:mainfrom
gmalette:gm/wider-compiled-metric-cache-key
Aug 4, 2026
Merged

Widen CompiledMetric tag-cache key from 32 to 57 bits#425
gmalette merged 2 commits into
Shopify:mainfrom
gmalette:gm/wider-compiled-metric-cache-key

Conversation

@gmalette

@gmalette gmalette commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Situation

CompiledMetric keys its tag-combination cache by a 32-bit rotate-left-5 + XOR hash of the tag values. Collisions are detected by comparing full tag values but never resolved: the losing tag combination is never cached, so every emit of it for the rest of the process lifetime allocates a fresh PrecompiledDatagram (the allocation CompiledMetric exists to avoid) and increments statsd_instrument.compiled_metric.hash_collision_detected. Once a colliding pair lands among hot tag combinations, the counter fires continuously until the process restarts. Because String#hash is seeded per process, which pair collides differs per process, so the signal shows up as intermittent, fleet-wide noise.

At 32 bits this is inevitable at real volumes. A process that has cached D distinct tag combinations sees expected collisions of roughly D² / 2³³, and every new distinct combination looked up against a full 5000-entry cache collides with probability ~1.2e-6. A busy fleet produces a steady stream of collision events every day.

Execution

Widen the key to 57 bits at zero cost. The only operation that can escape Fixnum range is the intermediate (__cache_key__ << 5), and 57-bit keys keep it within 2**62 - 1 on 64-bit CRuby:

k <= 2**57 - 1
k << 5 <= 2**62 - 32
(k << 5) | (k >> 52) <= 2**62 - 1

String#hash exposes ~60 bits of SipHash entropy, so the extra 25 bits are real. Expected collisions per process drop by 2^25 (~33 million-fold), turning a daily occurrence into a never-in-practice one, with the same op count and no new allocations on either the hit or miss path. The collision-detection branch stays as a correctness guard. The new test derives a low-32-bit-colliding pair at runtime (hash seeds are process-local) and asserts both combinations cache independently with no collision metric.

This touches the same lines as #413 but is orthogonal: that changes the mixing function for speed, this widens the key space. Either can land first; the conflict is trivial to resolve.

@gmalette
gmalette force-pushed the gm/wider-compiled-metric-cache-key branch 2 times, most recently from fb6b4ce to 70c18f8 Compare August 3, 2026 18:02
The tag-combination cache keys precompiled datagrams by a 32-bit
rotate-left-5 + XOR hash of the tag values. Collisions are detected by
comparing full tag values, but never resolved: the losing tag combination
is never cached, so every emit of it for the rest of the process lifetime
allocates a fresh PrecompiledDatagram (the allocation CompiledMetric exists
to avoid) and increments
statsd_instrument.compiled_metric.hash_collision_detected. Once a colliding
pair lands among hot tag combinations, the counter fires continuously until
the process restarts, and because String#hash is seeded per process, which
pair collides differs per process, making the signal intermittent and
fleet-wide.

At 32 bits this is inevitable at real volumes. A process that has cached D
distinct tag combinations sees expected collisions of roughly D^2 / 2^33;
with the cache holding up to 5000 entries and hot metrics seeing far more
distinct combinations looked up against it, a busy fleet produces a steady
stream of collision events every day.

## Fix

Widen the key to 57 bits at zero cost. The only operation that can escape
Fixnum range is the intermediate (__cache_key__ << 5), and 57-bit keys keep
it within 2**62 - 1 on 64-bit CRuby:

  k <= 2**57 - 1
  k << 5 <= 2**62 - 32
  (k << 5) | (k >> 52) <= 2**62 - 1

String#hash exposes ~60 bits of SipHash entropy, so the extra 25 bits are
real. Expected collisions per process drop by 2^25 (~33 million-fold),
turning a daily occurrence into a never-in-practice one, with the same op
count and no new allocations on either the hit or miss path. The collision
detection branch stays as a correctness guard.

This touches the same lines as Shopify#413 but is orthogonal: that changes the
mixing function for speed, this widens the key space. Either can land
first; the conflict is trivial to resolve.

Assisted-By: devx/07082c33-4cad-46be-b311-20f734743ed7
Assisted-By: devx/07082c33-4cad-46be-b311-20f734743ed7
@gmalette
gmalette force-pushed the gm/wider-compiled-metric-cache-key branch from 70c18f8 to f64dfbc Compare August 3, 2026 18:02
@gmalette
gmalette marked this pull request as ready for review August 3, 2026 18:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Not ready to approve

The newly added tests can hang or fail across the repository’s supported Ruby engines (CI includes JRuby/TruffleRuby) due to unbounded collision-search logic and non-portable GC allocation stats usage.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR widens CompiledMetric’s tag-combination cache key from a 32-bit masked rotate-left+XOR hash to a 57-bit masked variant to drastically reduce practical collision rates while keeping integer operations within the immediate/small-integer range on 64-bit CRuby.

Changes:

  • Expand the cache-key mask to 57 bits and adjust rotation to match the wider key space.
  • Add tests intended to validate wider-than-32-bit behavior and guard against allocation regressions in cache-key computation.
  • Bump library version to 3.11.2 and document the change in the changelog.
File summaries
File Description
lib/statsd/instrument/compiled_metric.rb Widens tag-cache key mask to 57 bits and updates the rotation shift accordingly.
test/compiled_metric_test.rb Updates collision test mask and adds new tests for 57-bit key behavior and allocation characteristics.
lib/statsd/instrument/version.rb Bumps gem version to 3.11.2.
CHANGELOG.md Adds a 3.11.2 entry documenting the widened cache key.
Review details

Suppressed comments (1)

test/compiled_metric_test.rb:333

  • This collision-search loop is unbounded and currently uses i.hash, which can make the test extremely slow or non-terminating on some Rubies. Use String#hash (seeded per process) and add a max-iteration guard so the test fails fast instead of hanging CI.
    until pair
      key = i.hash & 0xFFFFFFFF
      other = seen[key]
      if other && other != i && (other.hash & 0x01FFFFFFFFFFFFFF) != (i.hash & 0x01FFFFFFFFFFFFFF)
        pair = [other, i]
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

metric = Class.new(StatsD::Instrument::CompiledMetric::Counter) do
define(
name: "foo.bar",
tags: { shop_id: Integer },
Comment thread test/compiled_metric_test.rb
@gmalette
gmalette merged commit 7e8ec49 into Shopify:main Aug 4, 2026
1 check passed
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.

4 participants