Skip to content

Commit

Permalink
Make chashmap, crossbeam and num_cpus deps optional
Browse files Browse the repository at this point in the history
Whenever those dependencies are used is now controlled with
the `multithreaded` feature flag.
  • Loading branch information
koute committed Sep 7, 2019
1 parent b554893 commit b4f0449
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ cirrus-ci = { repository = "jonhoo/inferno" }
codecov = { repository = "jonhoo/inferno", branch = "master", service = "github" }

[features]
default = ["cli"]
default = ["cli", "multithreaded"]
cli = ["structopt", "env_logger"]
multithreaded = ["chashmap", "crossbeam", "num_cpus"]

[dependencies]
chashmap = "2.2"
crossbeam = "0.7"
chashmap = { version = "2.2", optional = true }
crossbeam = { version = "0.7", optional = true }
env_logger = { version = "0.6.0", optional = true }
fnv = "1.0.3"
indexmap = "1.0"
itoa = "0.4.3"
lazy_static = "1.3.0"
log = "0.4"
num_cpus = "1.10"
num_cpus = { version = "1.10", optional = true }
num-format = { version = "0.4", default-features = false }
quick-xml = { version = "0.16", default-features = false }
rgb = "0.8.13"
Expand Down
60 changes: 52 additions & 8 deletions src/collapse/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::io;
use std::mem;
use std::sync::Arc;

#[cfg(feature = "multithreaded")]
use chashmap::CHashMap;
#[cfg(feature = "multithreaded")]
use crossbeam::channel;

use fnv::FnvHashMap;
use lazy_static::lazy_static;

Expand All @@ -26,11 +29,18 @@ const NBYTES_PER_STACK_GUESS: usize = 1024;

const RUST_HASH_LENGTH: usize = 17;

#[cfg(feature = "multithreaded")]
lazy_static! {
#[doc(hidden)]
pub static ref DEFAULT_NTHREADS: usize = num_cpus::get();
}

#[cfg(not(feature = "multithreaded"))]
lazy_static! {
#[doc(hidden)]
pub static ref DEFAULT_NTHREADS: usize = 1;
}

/// Private trait for internal library authors.
///
/// If you implement this trait, your type will implement the public-facing
Expand Down Expand Up @@ -154,6 +164,19 @@ pub trait CollapsePrivate: Send + Sized {
occurrences.write_and_clear(writer)
}

#[cfg(not(feature = "multithreaded"))]
fn collapse_multi_threaded<R>(
&mut self,
_: R,
_: &mut Occurrences,
) -> io::Result<()>
where
R: io::BufRead,
{
unimplemented!();
}

#[cfg(feature = "multithreaded")]
fn collapse_multi_threaded<R>(
&mut self,
mut reader: R,
Expand Down Expand Up @@ -332,32 +355,50 @@ pub trait CollapsePrivate: Send + Sized {
#[derive(Clone, Debug)]
pub enum Occurrences {
SingleThreaded(FnvHashMap<String, usize>),
#[cfg(feature = "multithreaded")]
MultiThreaded(Arc<CHashMap<String, usize>>),
}

impl Occurrences {
#[cfg(feature = "multithreaded")]
pub(crate) fn new(nthreads: usize) -> Self {
assert_ne!(nthreads, 0);
if nthreads == 1 {
let map = FnvHashMap::with_capacity_and_hasher(
CAPACITY_HASHMAP,
fnv::FnvBuildHasher::default(),
);
Occurrences::SingleThreaded(map)
Self::new_single_threaded()
} else {
let map = CHashMap::with_capacity(CAPACITY_HASHMAP);
let arc = Arc::new(map);
Occurrences::MultiThreaded(arc)
Self::new_multi_threaded()
}
}

#[cfg(not(feature = "multithreaded"))]
pub(crate) fn new(nthreads: usize) -> Self {
assert_ne!(nthreads, 0);
Self::new_single_threaded()
}

fn new_single_threaded() -> Self {
let map = FnvHashMap::with_capacity_and_hasher(
CAPACITY_HASHMAP,
fnv::FnvBuildHasher::default(),
);
Occurrences::SingleThreaded(map)
}

#[cfg(feature = "multithreaded")]
fn new_multi_threaded() -> Self {
let map = CHashMap::with_capacity(CAPACITY_HASHMAP);
let arc = Arc::new(map);
Occurrences::MultiThreaded(arc)
}

/// Inserts a key-count pair into the map. If the map did not have this key
/// present, `None` is returned. If the map did have this key present, the
/// value is updated, and the old value is returned.
pub(crate) fn insert(&mut self, key: String, count: usize) -> Option<usize> {
use self::Occurrences::*;
match self {
SingleThreaded(map) => map.insert(key, count),
#[cfg(feature = "multithreaded")]
MultiThreaded(arc) => arc.insert(key, count),
}
}
Expand All @@ -369,6 +410,7 @@ impl Occurrences {
use self::Occurrences::*;
match self {
SingleThreaded(map) => *map.entry(key).or_insert(0) += count,
#[cfg(feature = "multithreaded")]
MultiThreaded(arc) => arc.upsert(key, || count, |v| *v += count),
}
}
Expand All @@ -377,6 +419,7 @@ impl Occurrences {
use self::Occurrences::*;
match self {
SingleThreaded(_) => false,
#[cfg(feature = "multithreaded")]
MultiThreaded(_) => true,
}
}
Expand All @@ -394,6 +437,7 @@ impl Occurrences {
writeln!(writer, "{} {}", key, value)?;
}
}
#[cfg(feature = "multithreaded")]
MultiThreaded(ref mut arc) => {
let map = match Arc::get_mut(arc) {
Some(map) => map,
Expand Down

0 comments on commit b4f0449

Please sign in to comment.