Skip to content

hashmap and mutable values and efficiency #2567

Closed

Description

@jesse99

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?

Activity

  1. jesse99 commented on Jun 10, 2012

    @jesse99
    ContributorAuthor

    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;
    }
    
  2. nikomatsakis commented on Jun 10, 2012

    @nikomatsakis
    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.

  3. jesse99 commented on Jun 10, 2012

    @jesse99
    ContributorAuthor

    Ya, that works.

  4. catamorphism commented on Jun 14, 2012

    @catamorphism
    Contributor

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

  5. added a commit that references this issue on Sep 28, 2022
  6. added a commit that references this issue on Sep 8, 2025
  7. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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