pool size guess

serial
Avril 4 years ago
parent 9271879298
commit 8b996f5698
Signed by: flanchan
GPG Key ID: 284488987C31F630

32
Cargo.lock generated

@ -21,6 +21,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"memmap", "memmap",
"num_cpus", "num_cpus",
"rustc_version",
"smallvec",
] ]
[[package]] [[package]]
@ -43,6 +45,36 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "smallvec"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae524f056d7d770e174287294f562e95044c68e88dec909a00d2094805db9d75"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

@ -10,3 +10,8 @@ edition = "2018"
[dependencies] [dependencies]
memmap = "0.7.0" memmap = "0.7.0"
num_cpus = "1.13.0" num_cpus = "1.13.0"
smallvec = "1.5.1"
[build-dependencies]
rustc_version = "0.2"
num_cpus = "1.13.0"

@ -0,0 +1,46 @@
extern crate rustc_version;
extern crate num_cpus;
use rustc_version::{version, version_meta, Channel};
use std::io::{self, Write};
use std::fs::OpenOptions;
pub const DEFAULT_CPU_FILE: &str = "./src/consts.rs";
fn write_numcpus() -> io::Result<()>
{
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(DEFAULT_CPU_FILE)?;
writeln!(&mut file, "pub const CPUS: usize = {};", num_cpus::get())?;
writeln!(&mut file, "pub type PoolContainer<T> = [T; CPUS+1];")?;
file.flush()?;
Ok(())
}
fn main() {
// Assert we haven't travelled back in time
assert!(version().unwrap().major >= 1);
// Set cfg flags depending on release channel
match version_meta().unwrap().channel {
Channel::Stable => {
println!("cargo:rustc-cfg=stable");
}
Channel::Beta => {
println!("cargo:rustc-cfg=beta");
}
Channel::Nightly => {
println!("cargo:rustc-cfg=nightly");
}
Channel::Dev => {
println!("cargo:rustc-cfg=dev");
}
}
write_numcpus().expect("Failed to write comptime cpu info to source")
}

@ -0,0 +1,2 @@
pub const CPUS: usize = 8;
pub type PoolContainer<T> = [T; CPUS+1];

@ -6,6 +6,8 @@ mod map;
mod job; mod job;
mod work; mod work;
mod consts;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
} }

@ -66,9 +66,15 @@ pub fn join_all<T>(i: impl IntoIterator<Item = JoinHandle<T>>)
} }
} }
/// Vector type used for thread pool waiting.
///
/// This container is compiled will an estimate of enough stack storage for a reasonably sized thread pool.
/// The number of processors of the CPU this binary was built on is used as this estimate, and will only heap allocate when spawning more threads that number.
pub type PoolContainer = smallvec::SmallVec<[JoinHandle<()>; crate::consts::CPUS]>;
/// A pool container that ensures worker threads are waited on before termination. /// A pool container that ensures worker threads are waited on before termination.
#[derive(Debug)] #[derive(Debug)]
pub struct Pool(Vec<JoinHandle<()>>); pub struct Pool(PoolContainer);
impl Pool impl Pool
{ {
@ -81,7 +87,7 @@ impl Pool
/// Detach the pool but do not join its worker threads. /// Detach the pool but do not join its worker threads.
#[inline] pub fn detach(mut self) #[inline] pub fn detach(mut self)
{ {
self.0 = Vec::default(); self.0 = Default::default();
} }
/// The number of threads in the pool /// The number of threads in the pool
@ -102,11 +108,11 @@ impl FromIterator<JoinHandle<()>> for Pool
impl IntoIterator for Pool impl IntoIterator for Pool
{ {
type Item= JoinHandle<()>; type Item= JoinHandle<()>;
type IntoIter = std::vec::IntoIter<JoinHandle<()>>; type IntoIter = smallvec::IntoIter<[JoinHandle<()>; crate::consts::CPUS]>;
fn into_iter(mut self) -> Self::IntoIter fn into_iter(mut self) -> Self::IntoIter
{ {
std::mem::replace(&mut self.0, Vec::default()).into_iter() std::mem::replace(&mut self.0, Default::default()).into_iter()
} }
} }
@ -115,7 +121,7 @@ impl Drop for Pool
fn drop(&mut self) fn drop(&mut self)
{ {
if self.0.len() > 0 { if self.0.len() > 0 {
join_all(std::mem::replace(&mut self.0, Vec::default())); join_all(std::mem::replace(&mut self.0, Default::default()));
} }
} }
} }

Loading…
Cancel
Save