Skip to content

Commit

Permalink
Reduce thread pool memory usage
Browse files Browse the repository at this point in the history
The array-based channel that was created for a capacity of 1 would
allocate many more kilobytes than waiting for the auxiliary thread to
accept the task.
  • Loading branch information
nvzqz committed Dec 5, 2024
1 parent 823b160 commit 5499bc3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Reduced thread pool memory usage by many kilobytes by using rendezvous
channels instead of array-based channels.

## [0.1.16] - 2024-11-25

### Added
Expand Down
21 changes: 14 additions & 7 deletions src/thread_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,13 @@ fn spawn(additional: NonZeroUsize, threads: &mut Vec<mpsc::SyncSender<Task>>) {
let next_thread_id = threads.len() + 1;

threads.extend((next_thread_id..(next_thread_id + additional.get())).map(|thread_id| {
// Create single-task channel.
// Create single-task channel. Unless another benchmark is running, the
// current thread will be immediately unblocked after the auxiliary
// thread accepts the task.
//
// If we used 0 (rendezvous channel), then the main thread would block
// while sending a task until the auxiliary thread accepts the task.
// Using a capacity of 1 allows the main thread to immediately progress
// onto finishing its local work, including sending the task to all
// remaining threads.
let (sender, receiver) = mpsc::sync_channel::<Task>(1);
// This uses a rendezvous channel (capacity 0) instead of other standard
// library channels because it reduces memory usage by many kilobytes.
let (sender, receiver) = mpsc::sync_channel::<Task>(0);

let work = move || {
// Abort the process if the caught panic error itself panics when
Expand Down Expand Up @@ -379,4 +378,12 @@ mod benches {

bencher.bench(benched);
}

/// Benchmarks using `ThreadPool` once.
#[crate::bench(crate = crate, args = aux_thread_counts(), sample_size = 1)]
fn broadcast_once(bencher: crate::Bencher, aux_threads: usize) {
bencher
.with_inputs(ThreadPool::new)
.bench_refs(|pool| pool.broadcast(aux_threads, crate::black_box_drop));
}
}

0 comments on commit 5499bc3

Please sign in to comment.