Add benchmarks for AtomicRefCell borrows.

This commit is contained in:
Bobby Holley 2017-01-01 13:34:21 -08:00
parent 987b640c54
commit 79a552ea45

View file

@ -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,
@ -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);
@ -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]