Skip to content

hashmap and mutable values and efficiency #2567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jesse99 opened this issue Jun 10, 2012 · 4 comments
Closed

hashmap and mutable values and efficiency #2567

jesse99 opened this issue Jun 10, 2012 · 4 comments

Comments

@jesse99
Copy link
Contributor

jesse99 commented Jun 10, 2012

As far as I can tell there is no way to efficiently use a hashmap with a mutable value. Consider a type like

type pets = hashmap<str, dvec<str>>;

How can new elements be efficiently added to the dvec?

@jesse99
Copy link
Contributor Author

jesse99 commented Jun 10, 2012

Here is a test case for the above

// rustc --test test.rs && ./test
use std;
import std::map::hashmap;
import core::dvec::*;

type pets = hashmap<str, dvec<str>>;

fn broken_add(pets: pets, owner: str, name: str)
{
    alt pets.find(owner)
    {
        option::some(owns)
        {
            // owns is a copy so this is not very useful
            owns.push(name);
        }
        option::none
        {
            pets.insert(owner, dvec::from_vec([mut name]));
        }
    }
}

fn inefficient_add(pets: pets, owner: str, name: str)
{
    alt pets.find(owner)
    {
        option::some(owns)
        {
            // this does work, but there are at least two copies of a
            // potentially very large vector
            owns.push(name);
            pets.insert(owner, copy(owns));
        }
        option::none
        {
            pets.insert(owner, dvec::from_vec([mut name]));
        }
    }
}

#[test]
fn broken()
{
    let pets = std::map::str_hash();
    broken_add(pets, "Dr Evil", "Mr. Bigglesworth");
    broken_add(pets, "Dr Evil", "Sharks with frickin' laser beams");
    broken_add(pets, "Pinocchio", "Figaro");

    assert pets.size() == 2u;
    assert pets.get("Dr Evil").len() == 2u;        // this fails
    assert pets.get("Pinocchio").len() == 1u;
}

#[test]
fn inefficient()
{
    let pets = std::map::str_hash();
    inefficient_add(pets, "Dr Evil", "Mr. Bigglesworth");
    inefficient_add(pets, "Dr Evil", "Sharks with frickin' laser beams");
    inefficient_add(pets, "Pinocchio", "Figaro");

    assert pets.size() == 2u;
    assert pets.get("Dr Evil").len() == 2u;
    assert pets.get("Pinocchio").len() == 1u;
}

@nikomatsakis
Copy link
Contributor

Yes I want to refactor the API so that it is based on a with() primitive that gives you in-place access to the value rather than get() and find(). For now, I would suggest using @dvec as the value type.

@jesse99
Copy link
Contributor Author

jesse99 commented Jun 10, 2012

Ya, that works.

@catamorphism
Copy link
Contributor

Seems like this can be closed? @nikomatsakis , if you want to open an issue for your refactoring, please do :-)

oli-obk pushed a commit to oli-obk/rust that referenced this issue Sep 28, 2022
CI: use cargo sparse registry

CI spends a few minutes downloading the index so this could help.
RalfJung pushed a commit to RalfJung/rust that referenced this issue Oct 4, 2022
CI: use cargo sparse registry

CI spends a few minutes downloading the index so this could help.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants