Skip to content

Commit 1b3c757

Browse files
committed
Merge /home/droundy/src/internment into stepancheg-sixteen
2 parents a176408 + fdc7c6c commit 1b3c757

File tree

3 files changed

+183
-89
lines changed

3 files changed

+183
-89
lines changed

benches/bench.rs

Lines changed: 164 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,106 +12,184 @@ fn main() {
1212
}
1313

1414
#[cfg(feature = "bench")]
15-
fn main() {
16-
println!(
17-
"Contended Intern {}",
18-
bench(|| {
19-
let mut handles: Vec<std::thread::JoinHandle<()>> = Vec::new();
20-
for i in 0..50 {
21-
handles.push(std::thread::spawn(move || {
22-
let mut interned_strings = Vec::new();
23-
let mut interned_ints = Vec::new();
24-
let mut interned_pairs = Vec::new();
25-
let mut interned_triples = Vec::new();
26-
for n in 0..100000 {
27-
match (i + n) % 4 {
28-
0 => {
29-
interned_strings.push(Intern::new(format!("i {} and n {}", i, n)));
30-
}
31-
1 => {
32-
interned_ints.push(Intern::new(i * n * 137));
33-
}
34-
2 => {
35-
interned_pairs.push(Intern::new((i, n)));
36-
}
37-
_ => {
38-
interned_triples.push(Intern::new((i, n, i + n)));
39-
}
40-
}
41-
}
42-
}));
15+
macro_rules! four_types {
16+
($intern:ident, $i:expr) => {
17+
$intern::<String>::benchmarking_only_clear_interns();
18+
$intern::<usize>::benchmarking_only_clear_interns();
19+
$intern::<(usize, usize)>::benchmarking_only_clear_interns();
20+
$intern::<(usize, usize, usize)>::benchmarking_only_clear_interns();
21+
22+
let mut interned_strings = Vec::new();
23+
let mut interned_ints = Vec::new();
24+
let mut interned_pairs = Vec::new();
25+
let mut interned_triples = Vec::new();
26+
for n in 0..100000 {
27+
match ($i + n) % 4 {
28+
0 => {
29+
interned_strings.push($intern::new(format!("i {} and n {}", $i, n)));
30+
}
31+
1 => {
32+
interned_ints.push($intern::new($i * n * 137));
33+
}
34+
2 => {
35+
interned_pairs.push($intern::new(($i, n)));
36+
}
37+
_ => {
38+
interned_triples.push($intern::new(($i, n, $i + n)));
39+
}
4340
}
44-
for h in handles {
45-
h.join().unwrap();
41+
}
42+
};
43+
}
44+
45+
#[cfg(feature = "bench")]
46+
macro_rules! same_arrays {
47+
($intern:ident, $i:expr) => {
48+
$intern::<[usize; 1000]>::benchmarking_only_clear_interns();
49+
$intern::<[usize; 20]>::benchmarking_only_clear_interns();
50+
$intern::<[usize; 30]>::benchmarking_only_clear_interns();
51+
$intern::<[usize; 40]>::benchmarking_only_clear_interns();
52+
$intern::<[usize; 50]>::benchmarking_only_clear_interns();
53+
$intern::<[usize; 60]>::benchmarking_only_clear_interns();
54+
$intern::<[usize; 70]>::benchmarking_only_clear_interns();
55+
$intern::<[usize; 80]>::benchmarking_only_clear_interns();
56+
$intern::<[usize; 90]>::benchmarking_only_clear_interns();
57+
$intern::<[usize; 100]>::benchmarking_only_clear_interns();
58+
59+
let mut size = $i;
60+
for _ in 0..100000 {
61+
size = (size * 75 + 74) % ((1 << 16) + 1);
62+
match size % 10 {
63+
0 => {
64+
$intern::new([0; 1000]);
65+
}
66+
1 => {
67+
$intern::new([0; 20]);
68+
}
69+
2 => {
70+
$intern::new([0; 30]);
71+
}
72+
3 => {
73+
$intern::new([0; 40]);
74+
}
75+
4 => {
76+
$intern::new([0; 50]);
77+
}
78+
5 => {
79+
$intern::new([0; 60]);
80+
}
81+
6 => {
82+
$intern::new([0; 70]);
83+
}
84+
7 => {
85+
$intern::new([0; 80]);
86+
}
87+
8 => {
88+
$intern::new([0; 90]);
89+
}
90+
_ => {
91+
$intern::new([0; 100]);
92+
}
4693
}
47-
})
48-
);
49-
println!(
50-
"Contended ArcIntern {}",
94+
}
95+
};
96+
}
97+
98+
#[cfg(feature = "bench")]
99+
macro_rules! different_arrays {
100+
($intern:ident, $i:expr) => {
101+
$intern::<[usize; 1000]>::benchmarking_only_clear_interns();
102+
$intern::<[usize; 20]>::benchmarking_only_clear_interns();
103+
$intern::<[usize; 30]>::benchmarking_only_clear_interns();
104+
$intern::<[usize; 40]>::benchmarking_only_clear_interns();
105+
$intern::<[usize; 50]>::benchmarking_only_clear_interns();
106+
$intern::<[usize; 60]>::benchmarking_only_clear_interns();
107+
$intern::<[usize; 70]>::benchmarking_only_clear_interns();
108+
$intern::<[usize; 80]>::benchmarking_only_clear_interns();
109+
$intern::<[usize; 90]>::benchmarking_only_clear_interns();
110+
$intern::<[usize; 100]>::benchmarking_only_clear_interns();
111+
112+
let mut size = $i;
113+
for _ in 0..100000 {
114+
size = (size * 75 + 74) % ((1 << 16) + 1);
115+
match size % 10 {
116+
0 => {
117+
$intern::new([size+$i; 1000]);
118+
}
119+
1 => {
120+
$intern::new([size+$i; 20]);
121+
}
122+
2 => {
123+
$intern::new([size+$i; 30]);
124+
}
125+
3 => {
126+
$intern::new([size+$i; 40]);
127+
}
128+
4 => {
129+
$intern::new([size+$i; 50]);
130+
}
131+
5 => {
132+
$intern::new([size+$i; 60]);
133+
}
134+
6 => {
135+
$intern::new([size+$i; 70]);
136+
}
137+
7 => {
138+
$intern::new([size+$i; 80]);
139+
}
140+
8 => {
141+
$intern::new([size+$i; 90]);
142+
}
143+
_ => {
144+
$intern::new([size+$i; 100]);
145+
}
146+
}
147+
}
148+
};
149+
}
150+
151+
#[cfg(feature = "bench")]
152+
macro_rules! contended {
153+
($intern:ident,$mac:ident) => {
51154
bench(|| {
52155
let mut handles: Vec<std::thread::JoinHandle<()>> = Vec::new();
53156
for i in 0..50 {
54-
handles.push(std::thread::spawn(move || {
55-
let mut interned_strings = Vec::new();
56-
let mut interned_ints = Vec::new();
57-
let mut interned_pairs = Vec::new();
58-
let mut interned_triples = Vec::new();
59-
for n in 0..100000 {
60-
match (i + n) % 4 {
61-
0 => {
62-
interned_strings.push(ArcIntern::new(format!("i {} and n {}", i, n)));
63-
}
64-
1 => {
65-
interned_ints.push(ArcIntern::new(i * n * 137));
66-
}
67-
2 => {
68-
interned_pairs.push(ArcIntern::new((i, n)));
69-
}
70-
_ => {
71-
interned_triples.push(ArcIntern::new((i, n, i + n)));
72-
}
73-
}
74-
}
75-
}));
157+
handles.push(std::thread::spawn(move || { $mac!($intern, i); }));
76158
}
77159
for h in handles {
78160
h.join().unwrap();
79161
}
80162
})
81-
);
82-
println!(
83-
"Contended LocalIntern {} (not really contended)",
163+
};
164+
}
165+
166+
#[cfg(feature = "bench")]
167+
macro_rules! uncontended {
168+
($intern:ident,$mac:ident) => {
84169
bench(|| {
85-
let mut handles: Vec<std::thread::JoinHandle<()>> = Vec::new();
86170
for i in 0..50 {
87-
handles.push(std::thread::spawn(move || {
88-
let mut interned_strings = Vec::new();
89-
let mut interned_ints = Vec::new();
90-
let mut interned_pairs = Vec::new();
91-
let mut interned_triples = Vec::new();
92-
for n in 0..100000 {
93-
match (i + n) % 4 {
94-
0 => {
95-
interned_strings.push(LocalIntern::new(format!("i {} and n {}", i, n)));
96-
}
97-
1 => {
98-
interned_ints.push(LocalIntern::new(i * n * 137));
99-
}
100-
2 => {
101-
interned_pairs.push(LocalIntern::new((i, n)));
102-
}
103-
_ => {
104-
interned_triples.push(LocalIntern::new((i, n, i + n)));
105-
}
106-
}
107-
}
108-
}));
109-
}
110-
for h in handles {
111-
h.join().unwrap();
171+
$mac!($intern, i);
112172
}
113173
})
114-
);
174+
};
175+
}
176+
177+
178+
#[cfg(feature = "bench")]
179+
fn main() {
180+
println!("Contended Intern {} / {}", contended!(Intern, four_types), uncontended!(Intern, four_types));
181+
println!("Contended ArcIntern {} / {}", contended!(ArcIntern, four_types), uncontended!(ArcIntern, four_types));
182+
println!("Contended LocalIntern {} / {}", contended!(LocalIntern, four_types), uncontended!(LocalIntern, four_types));
183+
println!("\n");
184+
185+
println!("Contended arrays Intern {} / {}", contended!(Intern, same_arrays), uncontended!(Intern, same_arrays));
186+
println!("Contended arrays ArcIntern {} / {}", contended!(ArcIntern, same_arrays), uncontended!(ArcIntern, same_arrays));
187+
println!("Contended arrays LocalIntern {} / {}", contended!(LocalIntern, same_arrays), uncontended!(LocalIntern, same_arrays));
188+
println!("\n");
189+
190+
println!("Contended different arrays Intern {} / {}", contended!(Intern, different_arrays), uncontended!(Intern, different_arrays));
191+
println!("Contended different arrays ArcIntern {} / {}", contended!(ArcIntern, different_arrays), uncontended!(ArcIntern, different_arrays));
192+
println!("Contended different arrays LocalIntern {} / {}", contended!(LocalIntern, different_arrays), uncontended!(LocalIntern, different_arrays));
115193
println!("\n");
116194

117195
Intern::new(0i64);

src/boxedset.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ impl<P: Deref + Eq + Hash> HashSet<P> {
4949
pub fn len(&self) -> usize {
5050
self.0.len()
5151
}
52+
#[cfg(feature = "bench")]
53+
pub fn clear(&mut self) {
54+
self.0.clear()
55+
}
5256
}

src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ impl<T: Eq + Hash + Send + Sync + 'static> Intern<T> {
318318
pub fn num_objects_interned() -> usize {
319319
with_global(|m: &mut HashSet<&'static T>| -> usize { m.len() })
320320
}
321+
322+
/// Only for benchmarking, this will cause problems
323+
#[cfg(feature = "bench")]
324+
pub fn benchmarking_only_clear_interns() {
325+
with_global(|m: &mut HashSet<&'static T>| -> () { m.clear() })
326+
}
321327
}
322328

323329
#[cfg(feature = "tinyset")]
@@ -340,9 +346,7 @@ fn heap_location() -> u64 {
340346
std::sync::atomic::Ordering::Relaxed,
341347
std::sync::atomic::Ordering::Relaxed,
342348
) {
343-
Ok(_) => {
344-
ptr as u64
345-
},
349+
Ok(_) => ptr as u64,
346350
Err(ptr) => {
347351
println!("race, ptr is {:p}", ptr);
348352
ptr as u64
@@ -606,6 +610,10 @@ impl<T: Eq + Hash + Send + Sync + 'static> ArcIntern<T> {
606610
pub fn refcount(&self) -> usize {
607611
unsafe { self.pointer.as_ref().count.load(Ordering::Acquire) }
608612
}
613+
614+
/// Only for benchmarking, this will cause problems
615+
#[cfg(feature = "bench")]
616+
pub fn benchmarking_only_clear_interns() {}
609617
}
610618

611619
#[cfg(feature = "arc")]
@@ -1075,6 +1083,10 @@ impl<T: Eq + Hash + 'static> LocalIntern<T> {
10751083
pub fn num_objects_interned() -> usize {
10761084
with_local(|m: &mut HashSet<Box<T>>| -> usize { m.len() })
10771085
}
1086+
1087+
/// Only for benchmarking, this will cause problems
1088+
#[cfg(feature = "bench")]
1089+
pub fn benchmarking_only_clear_interns() {}
10781090
}
10791091

10801092
#[test]

0 commit comments

Comments
 (0)