Skip to content

perf(rate_limit): eliminate String allocation for existing keys in RateLimiter::check (#252) - #259

Merged
ZhuchkaTriplesix merged 1 commit into
devfrom
issue-252-ratelimit-no-alloc
Oct 18, 2026
Merged

ZhuchkaTriplesix merged 1 commit into
devfrom
issue-252-ratelimit-no-alloc

Conversation

@ZhuchkaTriplesix

Copy link
Copy Markdown
Member

Closes #252

Summary of Changes

  • In RateLimiter::check (src/rate_limit.rs):
    • Previously, shard.entry(key.to_string()) was evaluated unconditionally on every request, allocating an owned String in the heap even when the client's bucket was already present in the shard.
    • Replaced with if let Some(bucket) = shard.get_mut(key) first, allowing zero-allocation lookups for existing client keys (the common case).
    • Kept eviction and key.to_string() insertion only for new keys not currently present in the shard.
  • Added unit test test_existing_key_tokens_drain_correctly in src/rate_limit.rs.
  • Verified formatting, clippy, unit tests, and pytest suite.

@ZhuchkaTriplesix
ZhuchkaTriplesix merged commit 27aec5c into dev Oct 18, 2026
17 checks 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.

1 participant