Skip to content

Commit 5bbc6c6

Browse files
committed
feat: allow configuring number of threads that rayon uses
1 parent 396d34b commit 5bbc6c6

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

crates/c2pdf-gui/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn app_view() -> impl IntoView {
127127
12.0,
128128
None,
129129
&logger_for_thread,
130+
None
130131
);
131132
doc_subset.lock().unwrap().to_document(&mut doc);
132133
let f = File::create(path_for_thread.join("output.pdf")).unwrap();

crates/c2pdf/src/bin/c2pdf.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use c2pdf::logging::Logger;
66
use core::f32;
77
use printpdf::*;
88
use std::fs::File;
9+
use std::num::NonZeroU8;
910
use std::path::PathBuf;
1011
use std::time::Instant;
1112
// This makes `FromArgs` happy
@@ -65,6 +66,10 @@ struct Arguments {
6566
/// text to add to (the top of) every page
6667
#[argh(option)]
6768
page_text: Option<String>,
69+
70+
/// number of threads to use for processing
71+
#[argh(option)]
72+
threads: Option<NonZeroU8>,
6873
}
6974
fn main() {
7075
// Parse args
@@ -98,6 +103,7 @@ fn main() {
98103
args.font_size,
99104
args.page_text,
100105
&logger,
106+
args.threads,
101107
);
102108
doc_subset.lock().unwrap().to_document(&mut doc);
103109
let num_pages = doc.pages.len();

crates/c2pdf/src/lib/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use std::{
1010
cmp::Ordering,
11+
num::NonZeroU8,
1112
path::PathBuf,
1213
sync::{Arc, Mutex},
1314
};
@@ -43,6 +44,7 @@ impl CodeToPdf {
4344
font_size: f32,
4445
page_text: Option<String>,
4546
logger: &Logger,
47+
threads: Option<NonZeroU8>,
4648
) -> (Arc<Mutex<DocumentSubset>>, usize) {
4749
let doc_subset = DocumentSubset::default();
4850
let ss = two_face::syntax::extra_newlines();
@@ -75,7 +77,13 @@ impl CodeToPdf {
7577
let local_highlighter_config = ThreadLocal::<Arc<Mutex<HighlighterConfig>>>::new();
7678

7779
let doc_subset = Arc::new(Mutex::new(doc_subset));
78-
80+
if let Some(threads) = threads {
81+
// Build the global threadpool with the correct number of threads
82+
rayon::ThreadPoolBuilder::new()
83+
.num_threads(u8::from(threads) as usize)
84+
.build_global()
85+
.unwrap();
86+
}
7987
walker.enumerate().par_bridge().for_each(|(i, result)| {
8088
// let mut doc = PdfDocument::new(&args.name);
8189
let c2pdf_mutex = local_c2pdf.get_or(|| {

0 commit comments

Comments
 (0)