Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use spin lock instead of standard lock #121

Closed
siddontang opened this issue Oct 25, 2017 · 1 comment
Closed

Use spin lock instead of standard lock #121

siddontang opened this issue Oct 25, 2017 · 1 comment
Assignees

Comments

@siddontang
Copy link
Contributor

I use crate spin 0.4.6 and find that the spin lock has a better performance than the standard lock.

In our case, we can't hold the lock for a long time, so using spin has a great benefit.

#[bench]
fn bench_lock_write_map(b: &mut Bencher) {
    let m = HashMap::with_capacity(100);
    let l = Arc::new(RwLock::new(m));

    b.iter(||{
        let mut a = l.write().unwrap();
        a.insert(1, 1);
    })
}

#[bench]
fn bench_spinlock_write_map(b: &mut Bencher) {
    let m = HashMap::with_capacity(100);
    let l = Arc::new(spin::RwLock::new(m));

    b.iter(||{
        let mut a = l.write();
        a.insert(1, 1);
    })
}


#[bench]
fn bench_lock_read_map(b: &mut Bencher) {
    let mut m = HashMap::with_capacity(100);
    m.insert(1, 1);
    let l = Arc::new(RwLock::new(m));

    b.iter(||{
        let a = l.read().unwrap();
        a.get(&1).unwrap();
    })
}

#[bench]
fn bench_spinlock_read_map(b: &mut Bencher) {
    let mut m = HashMap::with_capacity(100);
    m.insert(1, 1);
    let l = Arc::new(spin::RwLock::new(m));

    b.iter(||{
        let mut a = l.read();
        a.get(&1).unwrap();
    })
}
test tests::bench_lock_read_map      ... bench:          47 ns/iter (+/- 4)
test tests::bench_lock_write_map     ... bench:          45 ns/iter (+/- 4)
test tests::bench_spinlock_read_map  ... bench:          17 ns/iter (+/- 3)
test tests::bench_spinlock_write_map ... bench:          15 ns/iter (+/- 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants