Skip to content

perf: Optimize is_owner rate limit checks with zero allocation tracking#971

Open
ppraneth wants to merge 10 commits intomainfrom
ppraneth/perf2
Open

perf: Optimize is_owner rate limit checks with zero allocation tracking#971
ppraneth wants to merge 10 commits intomainfrom
ppraneth/perf2

Conversation

@ppraneth
Copy link
Copy Markdown
Collaborator

@ppraneth ppraneth commented Mar 30, 2026

Description

Problem

The is_owner method in the consistent hash ring was previously delegating to get_owners, which heavily allocated memory just to return a boolean check. To verify ownership, the original code had to allocate a new Vec, allocate a HashSet, clone strings repeatedly, and perform an extra string allocation for every check. This created unnecessary memory and CPU overhead during high throughput scenarios.

Solution

This PR eliminates all temporary vectors, dynamic HashSets, and string clones from the is_owner execution path. It introduces a fast, short circuiting iterator that tracks seen unique nodes using a small stack allocated array. Because the number of replica owners is very small, a linear scan over a fixed array is noticeably faster than hashing, allowing us to hit true zero memory allocations.

Changes

  • Detached is_owner from the allocating get_owners pipeline in the mesh crate.
  • Replaced the heap allocated tracking HashSet with a tiny stack bounded array.
  • Added a loop boundary based on the total ring length to mathematically guarantee loop termination in the rare event of a desynchronized state.
  • Made the consistent_hash module public to allow for direct isolated benchmarking.
  • Added a new criterion benchmark inside model_gateway to measure execution times over thousands of keys.

Test Plan

A new benchmark consistent_hash_bench was added to track cache hits and misses against the ring. You can reproduce the metrics by running:

cargo bench -p smg --bench consistent_hash_bench

Before changes:
Average execution time was approximately 173.00 µs.

After changes:
Average execution time dropped to approximately 29.90 µs.

This is an 82.7 percent performance improvement per block. also verified that all tests continue to pass and there are no remaining clippy warnings.

Checklist
  • cargo +nightly fmt passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • (Optional) Documentation updated
  • (Optional) Please join us on Slack #sig-smg to discuss, review, and merge PRs

Summary by CodeRabbit

  • Performance

    • Improved consistent-hash ownership checks for faster, more predictable lookups and bounded scanning to avoid pathological loops.
  • API Changes

    • Consistent-hash module made publicly accessible (kept hidden from generated docs).
  • Tests / Benchmarks

    • Added a benchmark that measures consistent-hash ownership performance.

Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
@github-actions github-actions bot added dependencies Dependency updates benchmarks Benchmark changes model-gateway Model gateway crate changes labels Mar 30, 2026
Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1a206971-b251-4e67-8333-17b63e2270ea

📥 Commits

Reviewing files that changed from the base of the PR and between 1a16f41 and f465f18.

📒 Files selected for processing (1)
  • crates/mesh/src/lib.rs

📝 Walkthrough

Walkthrough

ConsistentHashRing::is_owner was rewritten to compute ownership by hashing and scanning the ring with explicit termination and fixed-size stack tracking; the consistent_hash module was made public (doc-hidden); a Criterion benchmark for is_owner was added to model_gateway.

Changes

Cohort / File(s) Summary
Core Logic
crates/mesh/src/consistent_hash.rs
Rewrote ConsistentHashRing::is_owner to hash the key and scan clockwise through the ring, with early exit on empty ring, a fixed-size stack-allocated seen_nodes for uniqueness instead of a HashSet, limits by NUM_OWNERS and ring.len(), and removed delegation to get_owners.
Module Visibility
crates/mesh/src/lib.rs
Changed consistent_hash module from private (mod consistent_hash;) to publicly exported (#[doc(hidden)] pub mod consistent_hash;).
Benchmarking
model_gateway/Cargo.toml, model_gateway/benches/consistent_hash_bench.rs
Added a Cargo bench target and a Criterion benchmark is_owner_baseline that builds a 50-node ring, generates 500 keys, and measures ConsistentHashRing::is_owner for an existing and a non-existent node.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • CatherineSue
  • key4ng
  • slin1237

Poem

🐰 I hopped the ring and chased the hash,
I counted nodes in one quick dash,
No heaps, just stack — I scanned with glee,
Benchmarks hum, come watch me flee! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'perf: Optimize is_owner rate limit checks with zero allocation tracking' directly and specifically summarizes the main change: optimizing the is_owner method by eliminating allocations through stack-bounded array tracking.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ppraneth/perf2

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes the is_owner method in the ConsistentHashRing to be zero-allocation and introduces a benchmark suite to track its performance. Additionally, the consistent_hash module is now publicly accessible. Feedback was provided to simplify the is_owner logic by using Iterator::chain for a more idiomatic approach to handling ring wrap-around, which would enhance readability and maintainability without impacting performance.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 17b7cda58b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
@CatherineSue CatherineSue added the mesh Mesh crate changes label Apr 1, 2026
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Apr 3, 2026

Hi @ppraneth, the DCO sign-off check has failed. All commits must include a Signed-off-by line.

To fix existing commits:

# Sign off the last N commits (replace N with the number of unsigned commits)
git rebase HEAD~N --signoff
git push --force-with-lease

To sign off future commits automatically:

  • Use git commit -s every time, or
  • VSCode: enable Git: Always Sign Off in Settings
  • PyCharm: enable Sign-off commit in the Commit tool window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmarks Benchmark changes dependencies Dependency updates mesh Mesh crate changes model-gateway Model gateway crate changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants