Skip to content

Commit

Permalink
Upgrade to hashbrown 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 29, 2022
1 parent ffd875b commit 8eeea2c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rayon = { version = "1.4.1", optional = true }
rustc-rayon = { version = "0.3", optional = true }

[dependencies.hashbrown]
version = "0.11"
version = "0.12"
default-features = false
features = ["raw"]

Expand Down
24 changes: 6 additions & 18 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use core::mem::replace;
use core::ops::RangeBounds;

use crate::equivalent::Equivalent;
use crate::util::{enumerate, simplify_range};
use crate::util::simplify_range;
use crate::{Bucket, Entries, HashValue};

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

let mut indices = RawTable::with_capacity(entries.len());
for (i, entry) in enumerate(&entries) {
indices.insert_no_grow(entry.hash.get(), i);
}
raw::insert_bulk_no_grow(&mut indices, &entries);
Self { indices, entries }
}

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

// Reinsert stable indices
for (i, entry) in enumerate(start_entries) {
self.indices.insert_no_grow(entry.hash.get(), i);
}

// Reinsert shifted indices
for (i, entry) in (start..).zip(shifted_entries) {
self.indices.insert_no_grow(entry.hash.get(), i);
}
// Reinsert stable indices, then shifted indices
raw::insert_bulk_no_grow(&mut self.indices, start_entries);
raw::insert_bulk_no_grow(&mut self.indices, shifted_entries);
} else if erased + shifted < half_capacity {
// Find each affected index, as there are few to adjust

Expand Down Expand Up @@ -429,11 +421,7 @@ impl<K, V> IndexMapCore<K, V> {

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

pub(crate) fn reverse(&mut self) {
Expand Down
15 changes: 14 additions & 1 deletion src/map/core/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@
//! This module encapsulates the `unsafe` access to `hashbrown::raw::RawTable`,
//! mostly in dealing with its bucket "pointers".

use super::{equivalent, Entry, HashValue, IndexMapCore, VacantEntry};
use super::{equivalent, Bucket, Entry, HashValue, IndexMapCore, VacantEntry};
use core::fmt;
use core::mem::replace;
use hashbrown::raw::RawTable;

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

/// Inserts many entries into a raw table without reallocating.
///
/// ***Panics*** if there is not sufficient capacity already.
pub(super) fn insert_bulk_no_grow<K, V>(indices: &mut RawTable<usize>, entries: &[Bucket<K, V>]) {
assert!(indices.capacity() - indices.len() >= entries.len());
for entry in entries {
// SAFETY: we asserted that sufficient capacity exists for all entries.
unsafe {
indices.insert_no_grow(entry.hash.get(), indices.len());
}
}
}

pub(super) struct DebugIndices<'a>(pub &'a RawTable<usize>);
impl fmt::Debug for DebugIndices<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down

0 comments on commit 8eeea2c

Please sign in to comment.