Skip to content

Commit ca84423

Browse files
committed
Extract helper functions to get number of threads
1 parent c80031c commit ca84423

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎rust/src/graph/builder/mod.rs‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ pub fn build_graph(
6161
thread_handles.push(handle);
6262

6363
// Thread pool: Parse imports
64-
let num_workers = thread::available_parallelism()
65-
.map(|n| max(n.get() / 2, 1))
66-
.unwrap_or(4);
67-
for _ in 0..num_workers {
64+
let num_threads = num_threads_for_module_parsing();
65+
for _ in 0..num_threads {
6866
let receiver = found_module_receiver.clone();
6967
let sender = parsed_module_sender.clone();
7068
let error_sender = error_sender.clone();
@@ -141,10 +139,7 @@ struct ParsedModule {
141139
}
142140

143141
fn discover_python_modules(package: &PackageSpec, sender: channel::Sender<FoundModule>) {
144-
let num_threads = thread::available_parallelism()
145-
.map(|n| max(n.get() / 2, 1))
146-
.unwrap_or(4);
147-
142+
let num_threads = num_threads_for_module_discovery();
148143
let package_clone = package.clone();
149144

150145
WalkBuilder::new(&package.directory)
@@ -324,3 +319,19 @@ fn assemble_graph(
324319

325320
graph
326321
}
322+
323+
/// Calculate the number of threads to use for module discovery.
324+
/// Uses half of available parallelism, with a minimum of 1 and default of 4.
325+
fn num_threads_for_module_discovery() -> usize {
326+
thread::available_parallelism()
327+
.map(|n| max(n.get() / 2, 1))
328+
.unwrap_or(4)
329+
}
330+
331+
/// Calculate the number of threads to use for module parsing.
332+
/// Uses half of available parallelism, with a minimum of 1 and default of 4.
333+
fn num_threads_for_module_parsing() -> usize {
334+
thread::available_parallelism()
335+
.map(|n| max(n.get() / 2, 1))
336+
.unwrap_or(4)
337+
}

0 commit comments

Comments
 (0)