Skip to content

Commit 0ebfb79

Browse files
committed
and more :(
1 parent 865757a commit 0ebfb79

4 files changed

Lines changed: 90 additions & 57 deletions

File tree

crates/hash-sorted-map/benchmarks/performance.rs

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ fn bench_insert(c: &mut Criterion) {
8080

8181
group.bench_function("std::HashMap+FNV", |b| {
8282
b.iter_batched(
83-
|| std::collections::HashMap::with_capacity_and_hasher(trigrams.len(), fnv::FnvBuildHasher::default()),
83+
|| {
84+
std::collections::HashMap::with_capacity_and_hasher(
85+
trigrams.len(),
86+
fnv::FnvBuildHasher::default(),
87+
)
88+
},
8489
|mut map| {
8590
for (i, &key) in trigrams.iter().enumerate() {
8691
map.insert(key, i);
@@ -93,10 +98,12 @@ fn bench_insert(c: &mut Criterion) {
9398

9499
group.bench_function("hashbrown+Identity", |b| {
95100
b.iter_batched(
96-
|| hashbrown::HashMap::<u32, usize, IdentityBuildHasher>::with_capacity_and_hasher(
97-
trigrams.len(),
98-
Default::default(),
99-
),
101+
|| {
102+
hashbrown::HashMap::<u32, usize, IdentityBuildHasher>::with_capacity_and_hasher(
103+
trigrams.len(),
104+
Default::default(),
105+
)
106+
},
100107
|mut map| {
101108
for (i, &key) in trigrams.iter().enumerate() {
102109
map.insert(key, i);
@@ -109,10 +116,12 @@ fn bench_insert(c: &mut Criterion) {
109116

110117
group.bench_function("HashSortedMap", |b| {
111118
b.iter_batched(
112-
|| HashSortedMap::with_capacity_and_hasher(
113-
trigrams.len(),
114-
IdentityBuildHasher::default(),
115-
),
119+
|| {
120+
HashSortedMap::with_capacity_and_hasher(
121+
trigrams.len(),
122+
IdentityBuildHasher::default(),
123+
)
124+
},
116125
|mut map| {
117126
for (i, &key) in trigrams.iter().enumerate() {
118127
map.insert(key, i);
@@ -133,10 +142,11 @@ fn bench_reinsert(c: &mut Criterion) {
133142
group.bench_function("hashbrown+Identity", |b| {
134143
b.iter_batched(
135144
|| {
136-
let mut map = hashbrown::HashMap::<u32, usize, IdentityBuildHasher>::with_capacity_and_hasher(
137-
trigrams.len(),
138-
Default::default(),
139-
);
145+
let mut map =
146+
hashbrown::HashMap::<u32, usize, IdentityBuildHasher>::with_capacity_and_hasher(
147+
trigrams.len(),
148+
Default::default(),
149+
);
140150
for (i, &key) in trigrams.iter().enumerate() {
141151
map.insert(key, i);
142152
}
@@ -183,10 +193,12 @@ fn bench_grow(c: &mut Criterion) {
183193

184194
group.bench_function("hashbrown+Identity", |b| {
185195
b.iter_batched(
186-
|| hashbrown::HashMap::<u32, usize, IdentityBuildHasher>::with_capacity_and_hasher(
187-
128,
188-
Default::default(),
189-
),
196+
|| {
197+
hashbrown::HashMap::<u32, usize, IdentityBuildHasher>::with_capacity_and_hasher(
198+
128,
199+
Default::default(),
200+
)
201+
},
190202
|mut map| {
191203
for (i, &key) in trigrams.iter().enumerate() {
192204
map.insert(key, i);
@@ -199,10 +211,7 @@ fn bench_grow(c: &mut Criterion) {
199211

200212
group.bench_function("HashSortedMap", |b| {
201213
b.iter_batched(
202-
|| HashSortedMap::with_capacity_and_hasher(
203-
128,
204-
IdentityBuildHasher::default(),
205-
),
214+
|| HashSortedMap::with_capacity_and_hasher(128, IdentityBuildHasher::default()),
206215
|mut map| {
207216
for (i, &key) in trigrams.iter().enumerate() {
208217
map.insert(key, i);
@@ -227,10 +236,12 @@ fn bench_count(c: &mut Criterion) {
227236

228237
group.bench_function("hashbrown+Identity entry()", |b| {
229238
b.iter_batched(
230-
|| hashbrown::HashMap::<u32, u32, IdentityBuildHasher>::with_capacity_and_hasher(
231-
trigrams.len(),
232-
Default::default(),
233-
),
239+
|| {
240+
hashbrown::HashMap::<u32, u32, IdentityBuildHasher>::with_capacity_and_hasher(
241+
trigrams.len(),
242+
Default::default(),
243+
)
244+
},
234245
|mut map| {
235246
for &key in &counted_trigrams {
236247
*map.entry(key).or_insert(0) += 1;
@@ -243,10 +254,12 @@ fn bench_count(c: &mut Criterion) {
243254

244255
group.bench_function("HashSortedMap get_or_default", |b| {
245256
b.iter_batched(
246-
|| HashSortedMap::<u32, u32, _>::with_capacity_and_hasher(
247-
trigrams.len(),
248-
IdentityBuildHasher::default(),
249-
),
257+
|| {
258+
HashSortedMap::<u32, u32, _>::with_capacity_and_hasher(
259+
trigrams.len(),
260+
IdentityBuildHasher::default(),
261+
)
262+
},
250263
|mut map| {
251264
for &key in &counted_trigrams {
252265
*map.get_or_default(key) += 1;
@@ -259,10 +272,12 @@ fn bench_count(c: &mut Criterion) {
259272

260273
group.bench_function("HashSortedMap entry().or_default()", |b| {
261274
b.iter_batched(
262-
|| HashSortedMap::<u32, u32, _>::with_capacity_and_hasher(
263-
trigrams.len(),
264-
IdentityBuildHasher::default(),
265-
),
275+
|| {
276+
HashSortedMap::<u32, u32, _>::with_capacity_and_hasher(
277+
trigrams.len(),
278+
IdentityBuildHasher::default(),
279+
)
280+
},
266281
|mut map| {
267282
for &key in &counted_trigrams {
268283
*map.entry(key).or_default() += 1;
@@ -276,5 +291,11 @@ fn bench_count(c: &mut Criterion) {
276291
group.finish();
277292
}
278293

279-
criterion_group!(benches, bench_insert, bench_reinsert, bench_grow, bench_count);
294+
criterion_group!(
295+
benches,
296+
bench_insert,
297+
bench_reinsert,
298+
bench_grow,
299+
bench_count
300+
);
280301
criterion_main!(benches);

crates/hash-sorted-map/src/group_ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub type Mask = u64;
2323
#[cfg(target_arch = "x86_64")]
2424
mod arch {
2525
#[cfg(target_arch = "x86")]
26-
use core::arch::x86 as x86;
26+
use core::arch::x86;
2727
#[cfg(target_arch = "x86_64")]
2828
use core::arch::x86_64 as x86;
2929

crates/hash-sorted-map/src/hash_sorted_map.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,16 @@ impl<K: Hash + Eq, V, S: BuildHasher> HashSortedMap<K, V, S> {
174174
return None;
175175
}
176176
if c == tag && unsafe { group.keys[hint].assume_init_ref() } == &key {
177-
let old = std::mem::replace(
178-
unsafe { group.values[hint].assume_init_mut() },
179-
value,
180-
);
177+
let old = std::mem::replace(unsafe { group.values[hint].assume_init_mut() }, value);
181178
return Some(old);
182179
}
183180
// Slow path: SIMD scan group for tag match.
184181
let mut tag_mask = group_ops::match_tag(&group.ctrl, tag);
185182
tag_mask = group_ops::clear_slot(tag_mask, hint);
186183
while let Some(i) = group_ops::next_match(&mut tag_mask) {
187184
if unsafe { group.keys[i].assume_init_ref() } == &key {
188-
let old = std::mem::replace(
189-
unsafe { group.values[i].assume_init_mut() },
190-
value,
191-
);
185+
let old =
186+
std::mem::replace(unsafe { group.values[i].assume_init_mut() }, value);
192187
return Some(old);
193188
}
194189
}
@@ -240,9 +235,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> HashSortedMap<K, V, S> {
240235

241236
// Fast path: preferred slot.
242237
let c = group.ctrl[hint];
243-
if c == tag
244-
&& unsafe { group.keys[hint].assume_init_ref() }.borrow() == key
245-
{
238+
if c == tag && unsafe { group.keys[hint].assume_init_ref() }.borrow() == key {
246239
return Some(unsafe { group.values[hint].assume_init_ref() });
247240
}
248241

@@ -338,9 +331,9 @@ impl<K: Hash + Eq, V, S: BuildHasher> HashSortedMap<K, V, S> {
338331
for group in &old_groups[..old_num_groups] {
339332
let mut full_mask = group_ops::match_full(&group.ctrl);
340333
while let Some(i) = group_ops::next_match(&mut full_mask) {
341-
let hash = self.hash_builder.hash_one(unsafe {
342-
group.keys[i].assume_init_ref()
343-
});
334+
let hash = self
335+
.hash_builder
336+
.hash_one(unsafe { group.keys[i].assume_init_ref() });
344337
self.insert_for_grow(hash, group.keys[i].as_ptr(), group.values[i].as_ptr());
345338
}
346339
}
@@ -378,8 +371,16 @@ impl<K: Hash + Eq, V, S: BuildHasher> HashSortedMap<K, V, S> {
378371
}
379372
}
380373
group.ctrl[hint] = tag;
381-
unsafe { group.keys[hint].as_mut_ptr().copy_from_nonoverlapping(key_src, 1) };
382-
unsafe { group.values[hint].as_mut_ptr().copy_from_nonoverlapping(value_src, 1) };
374+
unsafe {
375+
group.keys[hint]
376+
.as_mut_ptr()
377+
.copy_from_nonoverlapping(key_src, 1)
378+
};
379+
unsafe {
380+
group.values[hint]
381+
.as_mut_ptr()
382+
.copy_from_nonoverlapping(value_src, 1)
383+
};
383384
self.len += 1;
384385
}
385386
}
@@ -402,7 +403,10 @@ enum FindResult<K, V> {
402403
/// long as no reallocation occurs (the grow path re-walks via the slow path).
403404
enum Insertion<K, V> {
404405
/// An empty slot is waiting at `(group, slot)`.
405-
Empty { group: *mut Group<K, V>, slot: usize },
406+
Empty {
407+
group: *mut Group<K, V>,
408+
slot: usize,
409+
},
406410
/// The chain is full; allocate a new overflow group and link via `tail`.
407411
NeedsOverflow { tail: *mut Group<K, V> },
408412
}
@@ -489,7 +493,6 @@ impl<'a, V> OccupiedEntry<'a, V> {
489493
}
490494

491495
impl<'a, K: Hash + Eq, V, S: BuildHasher> VacantEntry<'a, K, V, S> {
492-
493496
/// Insert `value` and return a mutable reference to it.
494497
/// Writes directly to the slot pre-computed during `entry()`; only re-walks
495498
/// the chain on the rare grow path (where the pre-computed pointers become
@@ -699,11 +702,20 @@ mod tests {
699702
s.to_string()
700703
};
701704
// First call: f runs, inserts "first".
702-
assert_eq!(map.get_or_insert_with(1, || make("first")), &mut "first".to_string());
705+
assert_eq!(
706+
map.get_or_insert_with(1, || make("first")),
707+
&mut "first".to_string()
708+
);
703709
// Second call with same key: f does NOT run; returns existing.
704-
assert_eq!(map.get_or_insert_with(1, || make("second")), &mut "first".to_string());
710+
assert_eq!(
711+
map.get_or_insert_with(1, || make("second")),
712+
&mut "first".to_string()
713+
);
705714
// New key: f runs.
706-
assert_eq!(map.get_or_insert_with(2, || make("third")), &mut "third".to_string());
715+
assert_eq!(
716+
map.get_or_insert_with(2, || make("third")),
717+
&mut "third".to_string()
718+
);
707719
assert_eq!(call_count, 2);
708720
assert_eq!(map.len(), 2);
709721
}

crates/string-offsets/benchmarks/performance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::hint::black_box;
21
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
32
use rand::{rng, RngExt};
3+
use std::hint::black_box;
44
use string_offsets::{AllConfig, OnlyLines, StringOffsets};
55

66
fn only_lines_construction_benchmark(c: &mut Criterion) {

0 commit comments

Comments
 (0)