-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
221984c
commit 23e5a53
Showing
10 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "rayoff" | ||
version = "0.21.0" | ||
authors = ["Anatoly Yakovenko <anatoly@solana.com>"] | ||
|
||
[dependencies] | ||
sys-info = "0.5.8" | ||
|
||
[dev-dependencies] | ||
rayon = "1.1" | ||
|
||
[lib] | ||
crate-type = ["lib"] | ||
name = "rayoff" | ||
|
||
[[bench]] | ||
name = "rayoff" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
A Rust library that implements a fast threadpool. | ||
|
||
Raw bench performance: | ||
|
||
``` | ||
test bench_baseline ... bench: 90 ns/iter (+/- 5) | ||
test bench_pool ... bench: 10,489 ns/iter (+/- 2,053) | ||
test bench_rayon ... bench: 11,817 ns/iter (+/- 636) | ||
``` | ||
|
||
sigverify performance: | ||
``` | ||
running 3 tests | ||
test bench_sigverify ... bench: 3,973,128 ns/iter (+/- 306,527) | ||
test bench_sigverify_rayoff ... bench: 3,697,677 ns/iter (+/- 738,464) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#![feature(test)] | ||
extern crate rayoff; | ||
extern crate rayon; | ||
extern crate test; | ||
|
||
use rayoff::rayoff::Pool; | ||
use rayon::prelude::*; | ||
use test::Bencher; | ||
|
||
#[bench] | ||
fn bench_pool(bencher: &mut Bencher) { | ||
let pool = Pool::new(); | ||
bencher.iter(|| { | ||
let mut array = [0usize; 100]; | ||
pool.dispatch_mut(&mut array, |val: &mut usize| *val += 1); | ||
let expected = [1usize; 100]; | ||
for i in 0..100 { | ||
assert_eq!(array[i], expected[i]); | ||
} | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn bench_baseline(bencher: &mut Bencher) { | ||
bencher.iter(|| { | ||
let mut array = [0usize; 100]; | ||
for i in array.iter_mut() { | ||
*i += 1; | ||
} | ||
let expected = [1usize; 100]; | ||
for i in 0..100 { | ||
assert_eq!(array[i], expected[i]); | ||
} | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn bench_rayon(bencher: &mut Bencher) { | ||
bencher.iter(|| { | ||
let mut array = [0usize; 100]; | ||
array.par_iter_mut().for_each(|p| *p += 1); | ||
let expected = [1usize; 100]; | ||
for i in 0..100 { | ||
assert_eq!(array[i], expected[i]); | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod rayoff; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
extern crate sys_info; | ||
|
||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::sync::mpsc::{channel, Receiver, Sender}; | ||
use std::sync::{Arc}; | ||
use std::thread::{JoinHandle, yield_now, spawn}; | ||
|
||
struct Job { | ||
func: Box<dyn Fn(*mut u64, usize, usize)>, | ||
elems: *mut u64, | ||
num: usize, | ||
work_index: AtomicUsize, | ||
done_index: AtomicUsize, | ||
} | ||
unsafe impl Send for Job {} | ||
unsafe impl Sync for Job {} | ||
|
||
pub struct Pool { | ||
senders: Vec<Sender<Arc<Job>>>, | ||
threads: Vec<JoinHandle<()>>, | ||
} | ||
|
||
impl Job { | ||
fn execute(&self) { | ||
loop { | ||
let index = self.work_index.fetch_add(1, Ordering::Relaxed); | ||
if index >= self.num { | ||
self.done_index.fetch_add(1, Ordering::Relaxed); | ||
break; | ||
} | ||
(self.func)(self.elems, self.num, index); | ||
} | ||
} | ||
fn wait(&self, num: usize) { | ||
loop { | ||
let guard = self.done_index.load(Ordering::Relaxed); | ||
if guard >= num { | ||
break; | ||
} | ||
yield_now(); | ||
} | ||
} | ||
} | ||
|
||
impl Pool { | ||
pub fn new() -> Self { | ||
let num_threads = sys_info::cpu_num().unwrap_or(16) - 1; | ||
let mut pool = Self { | ||
senders: vec![], | ||
threads: vec![], | ||
}; | ||
(0..num_threads).for_each(|_| { | ||
let (sender, recvr): (Sender<Arc<Job>>, Receiver<Arc<Job>>) = channel(); | ||
let t = spawn(move || { | ||
for job in recvr.iter() { | ||
job.execute() | ||
} | ||
}); | ||
pool.senders.push(sender); | ||
pool.threads.push(t); | ||
}); | ||
pool | ||
} | ||
|
||
pub fn dispatch_mut<F, A>(&self, elems: &mut [A], func: F) | ||
where | ||
F: Fn(&mut A) + 'static, | ||
{ | ||
let job = Job { | ||
elems: elems.as_mut_ptr() as *mut u64, | ||
num: elems.len(), | ||
done_index: AtomicUsize::new(0), | ||
work_index: AtomicUsize::new(0), | ||
func: Box::new(move |ptr, num, index| { | ||
let ptr = ptr as *mut A; | ||
let slice = unsafe { std::slice::from_raw_parts_mut(ptr, num) }; | ||
func(&mut slice[index]) | ||
}), | ||
}; | ||
let job = Arc::new(job); | ||
for s in &self.senders { | ||
s.send(job.clone()).expect("send should never fail"); | ||
} | ||
job.execute(); | ||
job.wait(self.senders.len() + 1); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[test] | ||
fn test_pool() { | ||
let pool = Pool::new(); | ||
let mut array = [0usize; 100]; | ||
pool.dispatch_mut(&mut array, |val: &mut usize| *val += 1); | ||
let expected = [1usize; 100]; | ||
for i in 0..100 { | ||
assert_eq!(array[i], expected[i]); | ||
} | ||
} | ||
} |