Skip to content

Commit

Permalink
Add benchmarks for AtomicRefCell borrows.
Browse files Browse the repository at this point in the history
  • Loading branch information
bholley committed Jan 2, 2017
1 parent 987b640 commit 79a552e
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion tests/unit/style/atomic_refcell.rs
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use style::atomic_refcell::{AtomicRef, AtomicRefCell};
use test::Bencher;

struct Foo {
u: u32,
Expand All @@ -12,9 +13,15 @@ struct Bar {
f: Foo,
}

impl Default for Bar {
fn default() -> Self {
Bar { f: Foo { u: 42 } }
}
}

#[test]
fn map() {
let a = AtomicRefCell::new(Bar { f: Foo { u: 42 } });
let a = AtomicRefCell::new(Bar::default());
let b = a.borrow();
assert_eq!(b.f.u, 42);
let c = AtomicRef::map(b, |x| &x.f);
Expand All @@ -23,6 +30,33 @@ fn map() {
assert_eq!(*d, 42);
}

#[bench]
fn immutable_borrow(b: &mut Bencher) {
let a = AtomicRefCell::new(Bar::default());
b.iter(|| a.borrow());
}

#[bench]
fn immutable_second_borrow(b: &mut Bencher) {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow();
b.iter(|| a.borrow());
}

#[bench]
fn immutable_third_borrow(b: &mut Bencher) {
let a = AtomicRefCell::new(Bar::default());
let _first = a.borrow();
let _second = a.borrow();
b.iter(|| a.borrow());
}

#[bench]
fn mutable_borrow(b: &mut Bencher) {
let a = AtomicRefCell::new(Bar::default());
b.iter(|| a.borrow_mut());
}

/* FIXME(bholley): Enable once we have AtomicRefMut::map(), which is blocked on
* https://github.com/Kimundi/owning-ref-rs/pull/16
#[test]
Expand Down

0 comments on commit 79a552e

Please sign in to comment.