-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_copy_map.rs
173 lines (160 loc) · 4.48 KB
/
local_copy_map.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![feature(test)]
extern crate test;
use std::fmt::Display;
use std::hash::RandomState;
use std::hint::black_box;
use rayon::prelude::*;
use concurrent_tools::local_copy_map::{LocalAnyMapType, LocalCopyMap, LocalMapType};
const N: i32 = 1024 << 4;
const SEARCH_LOOP: i32 = 1024;
const WRITE_MASK: i32 = 0x1;
#[bench]
fn bench_std_mutex_map_readonly(b: &mut test::Bencher) {
let mut map = std::collections::HashMap::new();
for i in 0..N {
map.insert(i, i);
}
let map = std::sync::Mutex::new(map);
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
black_box(map.lock().unwrap().get(&i).unwrap());
});
});
}
#[bench]
fn bench_std_rwlock_map_readonly(b: &mut test::Bencher) {
let mut map = std::collections::HashMap::new();
for i in 0..N {
map.insert(i, i);
}
let map = std::sync::RwLock::new(map);
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
black_box(map.read().unwrap().get(&i).unwrap());
});
});
}
thread_local! {
static LOCAL_RO: LocalMapType<i32, i32, RandomState> = LocalCopyMap::init_local(32);
static LOCAL_ANY_RO: LocalAnyMapType<RandomState> = LocalCopyMap::init_any_local(32);
}
#[bench]
fn bench_concurrent_map_readonly(b: &mut test::Bencher) {
let map = LocalCopyMap::new(32, &LOCAL_RO);
for i in 0..N {
map.set(i, i);
}
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
black_box(map.get(&i, |x| *x).unwrap());
});
});
}
#[bench]
fn bench_concurrent_any_map_readonly(b: &mut test::Bencher) {
let map = LocalCopyMap::new(32, &LOCAL_ANY_RO);
map.set_any(String::from("hello"));
map.set_any("hello");
map.set_any(10i8);
map.set_any(10u8);
map.set_any(10i16);
map.set_any(10u16);
map.set_any(10i32);
map.set_any(10u32);
map.set_any(10i64);
map.set_any(10u64);
map.set_any(10i128);
map.set_any(10u128);
map.set_any(10u64);
map.set_any(10f32);
map.set_any(10f64);
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
black_box(map.get_downcast(|x: &i64| {
*black_box(x)
}).unwrap());
});
});
}
thread_local! {
static LOCAL_RW: LocalMapType<i32, i32, RandomState> = LocalCopyMap::init_local(32);
static LOCAL_ANY_RW: LocalAnyMapType<RandomState> = LocalCopyMap::init_any_local(32);
}
#[bench]
fn bench_concurrent_map_readwrite(b: &mut test::Bencher) {
let map = LocalCopyMap::new(32, &LOCAL_RW);
for i in 0..N {
map.set(i, i);
}
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
if i & WRITE_MASK == WRITE_MASK {
black_box(map.set(i, i));
} else {
black_box(map.get(&i, |x| *x).unwrap());
}
});
});
}
#[bench]
fn bench_std_mutex_map_readwrite(b: &mut test::Bencher) {
let mut map = std::collections::HashMap::new();
for i in 0..N {
map.insert(i, i);
}
let map = std::sync::Mutex::new(map);
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
if i & WRITE_MASK == WRITE_MASK {
black_box(map.lock().unwrap().insert(i, i));
} else {
black_box(map.lock().unwrap().get(&i).unwrap());
}
});
});
}
#[bench]
fn bench_std_rwlock_map_readwrite(b: &mut test::Bencher) {
let mut map = std::collections::HashMap::new();
for i in 0..N {
map.insert(i, i);
}
let map = std::sync::RwLock::new(map);
b.iter(|| {
(0..SEARCH_LOOP)
.rev()
.collect::<Vec<_>>()
.into_par_iter()
.for_each(|i| {
if i & WRITE_MASK == WRITE_MASK {
black_box(map.write().unwrap().insert(i, i));
} else {
black_box(map.read().unwrap().get(&i).unwrap());
}
});
});
}