Skip to content

perf: RateLimiter::check allocates String on every request even for existing keys #252

Closed

Description

@ZhuchkaTriplesix

Where: src/rate_limit.rs:191-194 (RateLimiter::check).

Currently:

let bucket = shard.entry(key.to_string()).or_insert_with(|| BucketState {
    tokens: limit_f64,
    last_update: now,
});

Because std::collections::hash_map::Entry requires ownership of the key, key.to_string() is evaluated unconditionally on every single request, allocating a String on the heap even when the client's bucket already exists in the shard (the 99.9% common case).

Fix direction:
Look up using shard.get_mut(key) first without allocating. Only allocate key.to_string() when inserting a new bucket:

if let Some(bucket) = shard.get_mut(key) {
    // update bucket tokens
} else {
    shard.insert(key.to_string(), BucketState { ... });
}

Activity

  1. added
    enhancement
    on Oct 18, 2026
  2. added a commit that references this issue on Oct 18, 2026
  3. ZhuchkaTriplesix commented on Oct 18, 2026

    @ZhuchkaTriplesix
    MemberAuthor

    Resolved in PR #259

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions