Skip to content

Dropping empty map takes time proportional to capacity #170

@jonhoo

Description

@jonhoo

We currently iterate over the map on drop:

hashbrown/src/raw/mod.rs

Lines 1194 to 1195 in efbd036

if mem::needs_drop::<T>() {
for item in self.iter() {

And the iterator iterates over all the buckets, even if it knows there are no more items (RawIter::items == 0):

hashbrown/src/raw/mod.rs

Lines 1401 to 1402 in efbd036

fn next(&mut self) -> Option<Bucket<T>> {
if let Some(b) = self.iter.next() {

This means that code like this will unnecessarily take fairly long:

let map: HashMap<String, String> = HashMap::with_capacity(1024 * 1024);
drop(map);

The above is a little contrived, but the same occurs if the caller calls drain or clear, and then drops the map. Not sure if this is something we want to optimize for, but the fix is fairly straightforward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions