Skip to content

Commit ca49d7d

Browse files
committed
Upgrade to hashbrown 0.12
1 parent 0de46ec commit ca49d7d

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rayon = { version = "1.4.1", optional = true }
2222
rustc-rayon = { version = "0.3", optional = true }
2323

2424
[dependencies.hashbrown]
25-
version = "0.11"
25+
version = "0.12"
2626
default-features = false
2727
features = ["raw"]
2828

src/map/core.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::mem::replace;
1818
use core::ops::RangeBounds;
1919

2020
use crate::equivalent::Equivalent;
21-
use crate::util::{enumerate, simplify_range};
21+
use crate::util::simplify_range;
2222
use crate::{Bucket, Entries, HashValue};
2323

2424
/// Core of the map that does not depend on S
@@ -185,9 +185,7 @@ impl<K, V> IndexMapCore<K, V> {
185185
let entries = self.entries.split_off(at);
186186

187187
let mut indices = RawTable::with_capacity(entries.len());
188-
for (i, entry) in enumerate(&entries) {
189-
indices.insert_no_grow(entry.hash.get(), i);
190-
}
188+
raw::insert_bulk_no_grow(&mut indices, &entries);
191189
Self { indices, entries }
192190
}
193191

@@ -372,15 +370,9 @@ impl<K, V> IndexMapCore<K, V> {
372370
// Reinsert everything, as there are few kept indices
373371
self.indices.clear();
374372

375-
// Reinsert stable indices
376-
for (i, entry) in enumerate(start_entries) {
377-
self.indices.insert_no_grow(entry.hash.get(), i);
378-
}
379-
380-
// Reinsert shifted indices
381-
for (i, entry) in (start..).zip(shifted_entries) {
382-
self.indices.insert_no_grow(entry.hash.get(), i);
383-
}
373+
// Reinsert stable indices, then shifted indices
374+
raw::insert_bulk_no_grow(&mut self.indices, start_entries);
375+
raw::insert_bulk_no_grow(&mut self.indices, shifted_entries);
384376
} else if erased + shifted < half_capacity {
385377
// Find each affected index, as there are few to adjust
386378

@@ -429,11 +421,7 @@ impl<K, V> IndexMapCore<K, V> {
429421

430422
fn rebuild_hash_table(&mut self) {
431423
self.indices.clear();
432-
debug_assert!(self.indices.capacity() >= self.entries.len());
433-
for (i, entry) in enumerate(&self.entries) {
434-
// We should never have to reallocate, so there's no need for a real hasher.
435-
self.indices.insert_no_grow(entry.hash.get(), i);
436-
}
424+
raw::insert_bulk_no_grow(&mut self.indices, &self.entries);
437425
}
438426

439427
pub(crate) fn reverse(&mut self) {

src/map/core/raw.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22
//! This module encapsulates the `unsafe` access to `hashbrown::raw::RawTable`,
33
//! mostly in dealing with its bucket "pointers".
44
5-
use super::{equivalent, Entry, HashValue, IndexMapCore, VacantEntry};
5+
use super::{equivalent, Bucket, Entry, HashValue, IndexMapCore, VacantEntry};
66
use core::fmt;
77
use core::mem::replace;
88
use hashbrown::raw::RawTable;
99

1010
type RawBucket = hashbrown::raw::Bucket<usize>;
1111

12+
/// Inserts many entries into a raw table without reallocating.
13+
///
14+
/// ***Panics*** if there is not sufficient capacity already.
15+
pub(super) fn insert_bulk_no_grow<K, V>(indices: &mut RawTable<usize>, entries: &[Bucket<K, V>]) {
16+
assert!(indices.capacity() - indices.len() >= entries.len());
17+
for entry in entries {
18+
// SAFETY: we asserted that sufficient capacity exists for all entries.
19+
unsafe {
20+
indices.insert_no_grow(entry.hash.get(), indices.len());
21+
}
22+
}
23+
}
24+
1225
pub(super) struct DebugIndices<'a>(pub &'a RawTable<usize>);
1326
impl fmt::Debug for DebugIndices<'_> {
1427
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)