Bgdrop
A minimal background dropper for Rust. Free memory in background threads to reduce latency spikes by 100 times.
Test results by releasing 1000 trees of 1000 nodes each.
๐ฆ Installation
- Edit Cargo.toml
# Cargo.toml
[]
= "0.1"
- Or use Cargo
๐ง Usage
use Bgdrop;
โ ๏ธ Notice
- Creating a
Bgdropinstance will start at least one dedicated background thread for memory dropping. - Avoid creating
Bgdropinstances frequently. Use.clone()to share the same background thread. - For large or complex data structures (e.g.,
Vec<Vec<_>>,HashMap<_, _>, or trees), offloading the drop to background can improve performance by 10ร or more, especially in latency-critical code paths. - For small types like tiny
Vecs, primitive types, or types that do not implementClone,bgdropis unnecessary and adds overhead. - Use
bgdroponly when dropping time becomes a measurable bottleneck.
๐ก Motivation
In performance-sensitive applications like games, real-time systems, or low-latency services, releasing large memory allocations (e.g. Vec<u8>, HashMap, etc.) may cause noticeable spikes in frame time or latency.
By moving the drop operation to a background thread, bgdrop helps smooth the performance curve.
๐ Thread Safety
All types submitted to drop() must be:
Send'staticlifetime
Internally, values are wrapped in Box<dyn Send> and sent via a lock-free crossbeam::channel to a background thread that performs the drop.
๐ง Limitations
!Synctypes cannot be used.- Type erasure using
Box<dyn Send>incurs a minor allocation cost. - Dropped objects' destructors cannot observe ordering with respect to other code.
๐ License
MIT