Skip to content

Commit 3d1f5f7

Browse files
authored
Merge pull request #28 from projectsyn/feat/parallel-inventory
Parallelize inventory rendering
2 parents 1780735 + a9f36e2 commit 3d1f5f7

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ chrono = "0.4.30"
1616
indexmap = "2.0.0"
1717
nom = "7.1.3"
1818
pyo3 = { version = "0.19.2", features = ["chrono"] }
19+
rayon = "1.7.0"
1920
serde = { version = "1.0.188", features = ["derive"] }
2021
serde_json = "1.0.106"
2122
serde_yaml = "0.9.25"

src/inventory.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
22
use chrono::Local;
33
use pyo3::prelude::*;
44
use pyo3::types::PyDict;
5+
use rayon::prelude::*;
56
use std::collections::HashMap;
67

78
use super::{NodeInfo, Reclass};
@@ -28,8 +29,8 @@ impl Inventory {
2829
// Render all nodes
2930
let infos: Vec<_> = r
3031
.nodes
31-
.keys()
32-
.map(|name| (name, { r.render_node(name) }))
32+
.par_iter()
33+
.map(|(name, _)| (name, { r.render_node(name) }))
3334
.collect();
3435

3536
// Generate `Inventory` from the rendered nodes

src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub mod types;
1616
use anyhow::{anyhow, Result};
1717
use pyo3::exceptions::PyValueError;
1818
use pyo3::prelude::*;
19+
use pyo3::types::PyType;
20+
use rayon::ThreadPoolBuilder;
1921
use std::collections::HashMap;
2022
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
2123
use walkdir::WalkDir;
@@ -193,6 +195,19 @@ impl Reclass {
193195
Inventory::render(self)
194196
.map_err(|e| PyValueError::new_err(format!("Error while rendering inventory: {e}")))
195197
}
198+
199+
/// Configures the number of threads to use when rendering the full inventory. Calling the
200+
/// method with `count=0` will configure the thread pool to have one thread per logical core of
201+
/// the system.
202+
///
203+
/// Note that this method should only be called once and will print a diagnostic message if
204+
/// called again.
205+
#[classmethod]
206+
pub fn set_thread_count(_cls: &PyType, count: usize) {
207+
if let Err(e) = ThreadPoolBuilder::new().num_threads(count).build_global() {
208+
eprintln!("While initializing global thread pool: {e}");
209+
}
210+
}
196211
}
197212

198213
impl Default for Reclass {

0 commit comments

Comments
 (0)