Skip to content

Can iterators of SlotMap and SecondaryMap safely be joined? #121

Open

Description

@NeverGivinUp

Iterators very explicitly say, that the visit key-value pairs "in arbitrary order".
Is this "arbitrary order" consistent between a SlotMap and its dependent SecondaryMaps, so they can be joined (assuming they contain exactly the same keys in the same version)?

Is the following guaranteed to work, or is it working just an accident?

        let mut a = SlotMap::new();
        let mut b = SecondaryMap::new();
        let mut c = SecondaryMap::new();

        let idx0 = a.insert(0);
        let idx1 = a.insert(1);
        let idx2 = a.insert(2);

        b.insert(idx0, 0);
        b.insert(idx1, 1);
        b.insert(idx2, 2);

        c.insert(idx0, 0);
        c.insert(idx1, 1);
        c.insert(idx2, 2);

        c.remove(idx1);
        b.remove(idx1);
        a.remove(idx1);

        for (((a_key, a_val), (b_key, b_val)), (c_key, c_val)) in a.iter().zip(b.iter_mut()).zip(c.iter()) {
            assert_eq!(a_key, b_key);
            assert_eq!(a_key, c_key);
            assert_eq!(a_val, b_val);
            assert_eq!(a_val, c_val);
        }

Activity

  1. orlp commented on Jul 16, 2024

    @orlp
    Owner

    At the moment this is not guaranteed but should work. For a DenseSlotMap it won't work. I'd like to guarantee this actually works in slotmap 2.0.

  2. NeverGivinUp commented on Jul 16, 2024

    @NeverGivinUp
    Author

    That's great. Indexing in a FxHashMap with slot-map keys is about the same speed as indexing in a SecondaryMap. But iterating a SecondaryMap is so much faster than iterating a FxHashMap. Since performance is the only reason I use slot-maps not having to index into a secondary map while iterating is very important for me. Looking forward to slotmap 2.0, then. Do you already have any estimates wtr. publication date and the amount of API changes?

  3. added this to the 2.0 milestone on Dec 4, 2025
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

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions