Rust Library 'ratelock' Outperforms 'governor' on M3 Pro
New benchmarks show that a lock-free token bucket implementation in Rust, called `ratelock`, is 2.3 times faster than the `governor` rate-limiting library. On a single thread of an Apple M3 Pro, the library achieved an execution time of approximately 1.89 nanoseconds. The library is designed for high-performance workloads and supports `no_std` and `no_alloc` environments.
- The `ratelock` library achieves its performance through a lock-free implementation that relies on `AtomicU64` for state management, avoiding the use of heavier synchronization primitives like `Mutex` or `RwLock`. This design minimizes contention in highly concurrent scenarios. - In contrast, `governor` implements the Generic Cell Rate Algorithm (GCRA), which is functionally equivalent to a leaky bucket. It also uses a single `AtomicU64` to maintain its state, which is a significant performance advantage over mutex-based approaches. - The benchmark showing `ratelock` as 2.25 times faster than `governor` specifically measures the "check-path overhead" by using a very high quota. This test focuses on the raw speed of checking the rate limit without being influenced by the throttling behavior itself. - Support for `no_std` and `no_alloc` in both libraries is a key feature for systems-level programming, such as in operating systems, embedded devices, or other environments where the standard library and dynamic memory allocation are unavailable or undesirable. This is particularly relevant for performance-critical code on Apple's platforms. - The creator of `ratelock` designed it to have zero heap allocations in its steady-state operations and to contain no `unsafe` code, making it a candidate for applications where auditability and deterministic performance are critical. - `governor` is designed to be a highly efficient and ergonomic rate-limiting library and offers features like keyed rate limiters, where each key can have its own rate-limiting state, which is useful for scenarios like per-customer API rate limiting.