mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-16 04:55:22 +03:00
Merge pull request #21355 from Veykril/push-zkmupnykkvln
perf: Reduce channel lock contention for drop-threads
This commit is contained in:
@@ -163,7 +163,22 @@ unsafe impl Send for SendPtr {}
|
||||
let (sender, receiver) = std::sync::mpsc::channel::<(SendPtr, fn(SendPtr))>();
|
||||
std::thread::Builder::new()
|
||||
.name("SpanMapDropper".to_owned())
|
||||
.spawn(move || receiver.iter().for_each(|(b, drop)| drop(b)))
|
||||
.spawn(move || {
|
||||
loop {
|
||||
// block on a receive
|
||||
if let Ok((b, drop)) = receiver.recv() {
|
||||
drop(b);
|
||||
}
|
||||
// then drain the entire channel
|
||||
while let Ok((b, drop)) = receiver.try_recv() {
|
||||
drop(b);
|
||||
}
|
||||
// and sleep for a bit
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
}
|
||||
// why do this over just a `receiver.iter().for_each(drop)`? To reduce contention on the channel lock.
|
||||
// otherwise this thread will constantly wake up and sleep again.
|
||||
})
|
||||
.unwrap();
|
||||
sender
|
||||
})
|
||||
|
||||
@@ -218,7 +218,18 @@ fn drop(&mut self) {
|
||||
let (sender, receiver) = std::sync::mpsc::channel::<GreenNode>();
|
||||
std::thread::Builder::new()
|
||||
.name("ParseNodeDropper".to_owned())
|
||||
.spawn(move || receiver.iter().for_each(drop))
|
||||
.spawn(move || {
|
||||
loop {
|
||||
// block on a receive
|
||||
_ = receiver.recv();
|
||||
// then drain the entire channel
|
||||
while let Ok(_) = receiver.try_recv() {}
|
||||
// and sleep for a bit
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
}
|
||||
// why do this over just a `receiver.iter().for_each(drop)`? To reduce contention on the channel lock.
|
||||
// otherwise this thread will constantly wake up and sleep again.
|
||||
})
|
||||
.unwrap();
|
||||
sender
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user