Skip to content

Commit abf2027

Browse files
committed
Revert "Switch to rayon instead of scoped_threadpool"
This reverts commit bbe2600. Switching to `rayon` caused a pretty significant performance drop.
1 parent 26fa749 commit abf2027

File tree

3 files changed

+49
-105
lines changed

3 files changed

+49
-105
lines changed

Cargo.lock

Lines changed: 4 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ git2 = "0.13.8"
2323
globwalk = "0.8.0"
2424
handlebars = "3.4.0"
2525
home = "0.5.3"
26-
indexmap = { version = "1.5.1", features = ["serde-1", "rayon"] }
26+
indexmap = { version = "1.5.1", features = ["serde-1"] }
2727
itertools = "0.9.0"
2828
maplit = "1.0.2"
2929
once_cell = "1.4.1"
30-
rayon = "1.3.1"
3130
regex = "1.3.9"
3231
regex-macro = "0.1.0"
3332
reqwest = { version = "0.10.7", features = ["blocking"] }
33+
scoped_threadpool = "0.1.9"
3434
serde = { version = "1.0.115", features = ["derive"] }
3535
structopt = "0.3.16"
3636
thiserror = "1.0.20"

src/lock.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
//! This module handles the downloading of `Source`s and figuring out which
44
//! files to use for `Plugins`.
55
6+
use std::cmp;
67
use std::collections::HashSet;
8+
use std::convert::TryInto;
79
use std::fmt;
810
use std::fs;
911
use std::path::{Path, PathBuf};
1012
use std::result;
13+
use std::sync;
1114

1215
use anyhow::{anyhow, bail, Context as ResultExt, Error, Result};
1316
use indexmap::{indexmap, IndexMap};
1417
use itertools::{Either, Itertools};
1518
use maplit::hashmap;
1619
use once_cell::sync::Lazy;
17-
use rayon::prelude::*;
1820
use serde::{Deserialize, Serialize};
1921
use url::Url;
2022
use walkdir::WalkDir;
@@ -547,26 +549,46 @@ impl Config {
547549
.map(|(_, locked)| locked)
548550
.collect::<Vec<_>>()
549551
} else {
550-
// Install the sources in parallel.
551-
map.into_par_iter()
552-
.map(|(source, plugins)| {
553-
let source_name = source.to_string();
554-
let source = source
555-
.lock(ctx)
556-
.with_context(s!("failed to install source `{}`", source_name))?;
557-
558-
let mut locked = Vec::with_capacity(plugins.len());
559-
for (index, plugin) in plugins {
560-
let name = plugin.name.clone();
561-
locked.push((
562-
index,
563-
plugin
564-
.lock(ctx, &templates, source.clone(), matches, apply)
565-
.with_context(s!("failed to install plugin `{}`", name)),
566-
));
567-
}
568-
Ok(locked)
569-
})
552+
/// The maximmum number of threads to use while downloading sources.
553+
const MAX_THREADS: u32 = 8;
554+
555+
// Create a thread pool and install the sources in parallel.
556+
let thread_count = cmp::min(count.try_into().unwrap_or(MAX_THREADS), MAX_THREADS);
557+
let mut pool = scoped_threadpool::Pool::new(thread_count);
558+
let (tx, rx) = sync::mpsc::channel();
559+
let templates_ref = &templates;
560+
561+
pool.scoped(move |scoped| {
562+
for (source, plugins) in map {
563+
let tx = tx.clone();
564+
scoped.execute(move || {
565+
tx.send((|| {
566+
let source_name = source.to_string();
567+
let source = source
568+
.lock(ctx)
569+
.with_context(s!("failed to install source `{}`", source_name))?;
570+
571+
let mut locked = Vec::with_capacity(plugins.len());
572+
for (index, plugin) in plugins {
573+
let name = plugin.name.clone();
574+
locked.push((
575+
index,
576+
plugin
577+
.lock(ctx, templates_ref, source.clone(), matches, apply)
578+
.with_context(s!("failed to install plugin `{}`", name)),
579+
));
580+
}
581+
Ok(locked)
582+
})())
583+
.expect("oops! did main thread die?");
584+
})
585+
}
586+
scoped.join_all();
587+
});
588+
589+
rx.iter()
590+
// all threads must send a response
591+
.take(count)
570592
// The result of this is basically an `Iter<Result<Vec<(usize, Result)>, _>>`
571593
// The first thing we need to do is to filter out the failures and record the
572594
// errors that occurred while installing the source in our `errors` list.

0 commit comments

Comments
 (0)