lite-sync
Fast, lightweight async primitives: SPSC channel, oneshot, notify, and atomic waker.
π English | π δΈζζζ‘£
Overview
lite-sync provides a collection of optimized synchronization primitives designed for low latency and minimal allocations. These primitives are built from the ground up with performance in mind, offering alternatives to heavier standard library implementations.
Features
- Zero or minimal allocations: Most primitives avoid heap allocations entirely
- Lock-free algorithms: Using atomic operations for maximum concurrency
- Single-waiter optimization: Specialized for common SPSC (Single Producer Single Consumer) patterns
- Inline storage: Support for stack-allocated buffers to avoid heap allocations
- Type-safe: Leverages Rust's type system to enforce correctness at compile time
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Modules
oneshot
One-shot channel for sending a single value between tasks, with API behavior aligned with tokio::sync::oneshot.
Provides two variants:
oneshot::generic- For arbitrary typesT: Send, usesUnsafeCell<MaybeUninit<T>>for storageoneshot::lite- Ultra-lightweight variant forState-encodable types, uses onlyAtomicU8for storage
API (aligned with tokio oneshot):
channel<T>()- Create a sender/receiver pairSender::send(value) -> Result<(), T>- Send value, returnsErr(value)if receiver is closedSender::is_closed()- Check if receiver has been dropped or closedReceiver::recv().await/receiver.await- Async receive, returnsResult<T, RecvError>Receiver::try_recv()- Non-blocking receive, returnsResult<T, TryRecvError>Receiver::close()- Close the receiver, preventing future sendsReceiver::blocking_recv()- Blocking receive for synchronous code
Note: Unlike tokio's oneshot which uses CAS to guarantee
Errwhen receiver is already closed, our implementation usesArcrefcount check for simplicity. IfsendandReceiverdrop occur concurrently,sendmay returnOk(())even if the value will not be received. UseReceiver::close()for explicit cancellation when guaranteed detection is needed.
Key features:
- Zero Box allocation for waker storage
- Direct
Futureimplementation for ergonomic.await - Fast path for immediate completion
- Supports both sync (
blocking_recv) and async usage
spsc
High-performance async SPSC (Single Producer Single Consumer) channel.
Built on smallring for efficient ring buffer operations with inline storage support. Type-safe enforcement of single producer/consumer semantics eliminates synchronization overhead.
Key optimizations:
- Zero-cost interior mutability using
UnsafeCell - Inline buffer support for small channels
- Batch send/receive operations
- Single-waiter notification
notify
Lightweight single-waiter notification primitive.
Much lighter than tokio::sync::Notify when you only need to wake one task at a time. Ideal for internal synchronization in other primitives.
atomic_waker
Atomic waker storage with state machine synchronization.
Based on Tokio's AtomicWaker but simplified for specific use cases. Provides safe concurrent access to a waker without Box allocation.
Examples
Generic oneshot channel (like tokio::sync::oneshot)
use ;
async
Receiver close and try_recv
use channel;
async
Lite oneshot with custom state (ultra-lightweight)
use ;
async
SPSC channel with inline storage
use channel;
use NonZeroUsize;
async
Single-waiter notification
use SingleWaiterNotify;
use Arc;
async
Simple completion notification (unit type)
use Sender;
async
Blocking receive (for sync code)
use channel;
Benchmarks
Performance benchmarks are available in the benches/ directory. Run them with:
Key characteristics:
- Oneshot: Extremely fast for immediate completion, optimized async wait path
- SPSC: Low latency per-message overhead with efficient batch operations
- Notify: Minimal notification roundtrip time
Safety
All primitives use unsafe internally for performance but expose safe APIs. Safety is guaranteed through:
- Type system enforcement of single ownership (no
Cloneon SPSC endpoints) - Atomic state machines for synchronization
- Careful ordering of atomic operations
- Comprehensive test coverage including concurrent scenarios
Minimum Supported Rust Version (MSRV)
Rust 2024 edition (Rust 1.85.0 or later)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.