Skip to content

Instantly share code, notes, and snippets.

@Vtec234
Created September 4, 2017 12:07
Show Gist options
  • Save Vtec234/44690ee4b20780ebfe310eccc33d3828 to your computer and use it in GitHub Desktop.
Save Vtec234/44690ee4b20780ebfe310eccc33d3828 to your computer and use it in GitHub Desktop.
#![feature(test)]
extern crate crossbeam_epoch;
extern crate test;
#[cfg(test)]
mod bench {
use crossbeam_epoch::{Owned, Scope, pin};
use std::thread;
use test::{Bencher, black_box};
const THREADS: usize = 4;
const ITERS: usize = 512;
#[bench]
fn bench_free(b: &mut Bencher) {
b.iter(|| {
let a = Owned::new(1);
pin(|scope: &Scope| {
unsafe {
scope.defer_free(a.into_ptr(scope));
}
scope.flush();
} );
} );
}
struct DropMe {
val: u32,
}
impl Drop for DropMe {
fn drop(&mut self) {
black_box(self.val);
}
}
#[bench]
fn bench_drop(b: &mut Bencher) {
b.iter(|| {
let a = Owned::new(DropMe { val: 3 } );
pin(|scope: &Scope| {
unsafe {
scope.defer_drop(a.into_ptr(scope));
}
scope.flush();
} );
} );
}
#[bench]
fn bench_defer(b: &mut Bencher) {
b.iter(|| {
pin(|scope: &Scope| {
unsafe {
scope.defer(move || {
3 == 7;
} );
}
scope.flush();
} );
} );
}
#[bench]
fn bench_free_n(b: &mut Bencher) {
b.iter(|| {
let mut v: Vec<Owned<u32>> = Vec::new();
for _ in 0..ITERS {
v.push(Owned::new(4));
}
pin(|scope: &Scope| {
unsafe {
for a in v {
scope.defer_free(a.into_ptr(scope));
}
}
scope.flush();
} );
} );
}
#[bench]
fn bench_drop_n(b: &mut Bencher) {
b.iter(|| {
let mut v: Vec<Owned<DropMe>> = Vec::new();
for _ in 0..ITERS {
v.push(Owned::new(DropMe { val: 3 } ));
}
pin(|scope: &Scope| {
unsafe {
for a in v {
scope.defer_drop(a.into_ptr(scope));
}
}
scope.flush();
} );
} );
}
#[bench]
fn bench_defer_n(b: &mut Bencher) {
b.iter(|| {
pin(|scope: &Scope| {
unsafe {
for _ in 0..ITERS {
scope.defer(move || {
3 == 7;
} );
}
}
scope.flush();
} );
} );
}
#[bench]
fn bench_free_multi(b: &mut Bencher) {
b.iter(|| {
for _ in 0..THREADS {
thread::spawn(|| {
let mut v: Vec<Owned<u32>> = Vec::new();
for _ in 0..ITERS/THREADS {
v.push(Owned::new(4));
}
pin(|scope: &Scope| {
unsafe {
for a in v {
scope.defer_free(a.into_ptr(scope));
}
}
} );
} );
}
pin(|scope: &Scope| { scope.flush(); } );
} );
}
#[bench]
fn bench_drop_multi(b: &mut Bencher) {
b.iter(|| {
for _ in 0..THREADS {
thread::spawn(|| {
let mut v: Vec<Owned<DropMe>> = Vec::new();
for _ in 0..ITERS/THREADS {
v.push(Owned::new(DropMe { val: 3 } ));
}
pin(|scope: &Scope| {
unsafe {
for a in v {
scope.defer_drop(a.into_ptr(scope));
}
}
} );
} );
}
pin(|scope: &Scope| { scope.flush(); } );
} );
}
#[bench]
fn bench_defer_multi(b: &mut Bencher) {
b.iter(|| {
for _ in 0..THREADS {
thread::spawn(|| {
let mut v: Vec<Owned<DropMe>> = Vec::new();
for _ in 0..ITERS/THREADS {
v.push(Owned::new(DropMe { val: 3 } ));
}
pin(|scope: &Scope| {
unsafe {
for a in v {
scope.defer(move || {
drop(a);
} );
}
}
} );
} );
}
pin(|scope: &Scope| { scope.flush(); } );
} );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment