pub struct DeferDrop<T: Send + 'static> { /* private fields */ }
Expand description
Wrapper type that, when dropped, sends the inner value to a global background thread to be dropped. Useful in cases where a value takes a long time to drop (for instance, a windows file that might block on close, or a large data structure that has to extensively recursively trawl itself).
DeferDrop
implements Deref
and DerefMut
, meaning it can be
dereferenced and freely used like a container around its inner type.
§Notes:
Carefully consider whether this pattern is necessary for your use case. Like all worker-thread abstractions, sending the value to a separate thread comes with its own costs, so it should only be done if performance profiling indicates that it’s a performance gain.
There is only one global worker thread. Dropped values are enqueued in an unbounded channel to be consumed by this thread; if you produce more garbage than the thread can handle, this will cause unbounded memory consumption. There is currently no way for the thread to signal or block if it is overwhelmed.
All of the standard non-determinism threading caveats apply here. The
objects are guaranteed to be destructed in the order received through a
channel, which means that objects sent from a single thread will be
destructed in order. However, there is no guarantee about the ordering of
interleaved values from different threads. Additionally, there are no
guarantees about how long the values will be queued before being dropped,
or even that they will be dropped at all. If your main
thread terminates
before all drops could be completed, they will be silently lost (as though
via a mem::forget
). This behavior is entirely up to your OS’s thread
scheduler. There is no way to receive a signal indicating when a particular
object was dropped.
§Example
use defer_drop::DeferDrop;
use std::time::{Instant, Duration};
use std::iter::repeat_with;
let massive_vec: Vec<Vec<i32>> = repeat_with(|| vec![1, 2, 3])
.take(1_000_000)
.collect();
let deferred = DeferDrop::new(massive_vec.clone());
fn timer(f: impl FnOnce()) -> Duration {
let start = Instant::now();
f();
Instant::now() - start
}
let drop1 = timer(move || drop(massive_vec));
let drop2 = timer(move || drop(deferred));
assert!(drop2 < drop1);
Implementations§
Source§impl<T: Send + 'static> DeferDrop<T>
impl<T: Send + 'static> DeferDrop<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Create a new DeferDrop
value.
Examples found in repository?
11fn main() {
12 println!("Allocating a ridiculously large vector");
13 let vec1: Vec<Vec<String>> = repeat_with(|| {
14 repeat_with(|| "Hello, World".to_string())
15 .take(1000)
16 .collect()
17 })
18 .take(1000)
19 .collect();
20
21 println!("Duplicating that vector");
22 let vec2 = vec1.clone();
23 let defer_vec1 = DeferDrop::new(vec1);
24
25 println!("Dropping the vectors");
26
27 let vec1_timer = timer(move || drop(defer_vec1));
28 let vec2_timer = timer(move || drop(vec2));
29
30 println!("Duration of deferred drop: {:?}", vec1_timer);
31 println!("Duration of foreground drop: {:?}", vec2_timer);
32}
Sourcepub fn into_inner(this: Self) -> T
pub fn into_inner(this: Self) -> T
Unwrap the DeferDrop
, returning the inner value. This has the effect
of cancelling the deferred drop behavior; ownership of the inner value
is transferred to the caller.