Rust Library 'ratelock' Outperforms 'governor' on M3 Pro

Published by The Daily Scout

What happened

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.

Why it matters

- 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.

Key numbers

  • 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 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.
  • It also uses a single AtomicU64 to maintain its state, which is a significant performance advantage over mutex-based approaches.

Quick answers

What happened in 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.

Why does Rust Library 'ratelock' Outperforms 'governor' on M3 Pro matter?

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.

Get your own daily briefing

Scout delivers personalized news, insights, and conversations tailored to your role and industry.

Download on the App Store

Published by The Daily Scout - Be the smartest in the room.