| Crates.io | bgdrop |
| lib.rs | bgdrop |
| version | 0.1.2 |
| created_at | 2025-06-24 16:45:37.090163+00 |
| updated_at | 2025-06-24 17:27:23.745527+00 |
| description | A Rust crate that uses a dedicated thread and channel to reduce latency caused by memory deallocation. |
| homepage | |
| repository | https://github.com/daniel29348679/Bgdrop |
| max_upload_size | |
| id | 1724679 |
| size | 11,795 |
BgdropA minimal background dropper for Rust. Free memory in background threads to reduce latency spikes by 100 times.
---- tests::benchmark stdout ----
Duration without background drop: 27.3477ms
Duration with background drop: 108.5ยตs
Duration with background drop and threads: 105.2ยตs
Speedup with background drop: 252.05x
Speedup with background drop and threads: 259.96x
Test results by releasing 1000 trees of 1000 nodes each.
# Cargo.toml
[dependencies]
bgdrop = "0.1"
cargo add bgdrop
use bgdrop::Bgdrop;
fn main() {
// Create a background dropper with 1 thread
let dropper = Bgdrop::new();
// Drop a large Vec in the background
let large_vec = vec![0u8; 10_000_000];
dropper.drop(large_vec);
// Create a background dropper with multiple threads
let pool = Bgdrop::with_threads(4);
for _ in 0..10 {
pool.drop(vec![0u8; 1_000_000]);
}
}
Bgdrop instance will start at least one dedicated background thread for memory dropping.Bgdrop instances frequently. Use .clone() to share the same background thread.Vec<Vec<_>>, HashMap<_, _>, or trees), offloading the drop to background can improve performance by 10ร or more, especially in latency-critical code paths.Vecs, primitive types, or types that do not implement Clone, bgdrop is unnecessary and adds overhead.bgdrop only when dropping time becomes a measurable bottleneck.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.
All types submitted to drop() must be:
Send'static lifetimeInternally, values are wrapped in Box<dyn Send> and sent via a lock-free crossbeam::channel to a background thread that performs the drop.
!Sync types cannot be used.Box<dyn Send> incurs a minor allocation cost.MIT