Skip to content

Interleaving remove() / getOrPutAssumeCapacity() calls to std.HashMap causes existing entries in a hash map to be reported to not be found. #7494

Open

Description

@lithdew

This continues on from #7468.

Using PR #7472, I did another test and noticed that entries in the hash map were reported to not be found under getOrPutAssumeCapacity(). I found that the entries did exist by iterating through the hash map via. HashMap.iterator().

const std = @import("std");
const testing = std.testing;

test "HashMap" {
    var entries: std.AutoHashMapUnmanaged(usize, void) = .{};
    defer entries.deinit(testing.allocator);

    try entries.ensureCapacity(testing.allocator, 11);

    { // Add 0 ... 9.
        var i: usize = 0;
        while (i < 10) : (i += 1) {
            const result = entries.getOrPutAssumeCapacity(i);
            testing.expect(!result.found_existing);
        }
    }

    // Add 10 ... 19, and remove 0 .. 9 on each iteration.
    // Then, check that values 15 to 20 exist in the hash map.
    // The test fails when checking if value 17 exists in the hashmap.

    {
        var i: usize = 10;
        while (i < 20) : (i += 1) {
            const result = entries.getOrPutAssumeCapacity(i);
            testing.expect(!result.found_existing);
            testing.expect(entries.remove(i - 10) != null);
        }

        var j: usize = 15;
        while (j < 20) : (j += 1) {
            const result = entries.getOrPutAssumeCapacity(j);
            testing.expect(result.found_existing);
        }
    }
}

I also tried setting the hash and equality functions for the HashMap to just directly cast the usize key into a u64 hash, as @LemonBoy did in PR #7472, to make sure this is not resultant of issues related to auto hashing (e.g. #7445).

Doing so would make the above test case pass, though subsequent tests for adding and removing larger amounts of items (~hundreds of thousands) yields the same issue.

Activity

  1. changed the title [-]Interleaving remove() / getOrPutAssumeCapacity() std.HashMap causes existing entries to be reported to not be found.[/-] [+]Interleaving remove() / getOrPutAssumeCapacity() calls to std.HashMap causes existing entries in a hash map to be reported to not be found.[/+] on Dec 19, 2020
  2. added this to the 0.8.0 milestone on Dec 19, 2020
  3. added
    bug
    standard library
    on Dec 19, 2020
  4. added a commit that references this issue on Dec 19, 2020
    2263ea5
  5. LemonBoy commented on Dec 19, 2020

    @LemonBoy
    Contributor

    Yo, I've pushed a fix for this in #7472. Please let me know if it works for you.

  6. lithdew commented on Dec 19, 2020

    @lithdew
    ContributorAuthor

    Yo, I've pushed a fix for this in #7472. Please let me know if it works for you.

    Thanks for the fix! Extended the test case to additionally removeAssertDiscard() all entries that should exist in the hash map, which unfortunately fails.

    const std = @import("std");
    const testing = std.testing;
    
    test "HashMap" {
        var entries: std.AutoHashMapUnmanaged(usize, void) = .{};
        defer entries.deinit(testing.allocator);
    
        try entries.ensureCapacity(testing.allocator, 11);
    
        { // Add 0 ... 9.
            var i: usize = 0;
            while (i < 10) : (i += 1) {
                const result = entries.getOrPutAssumeCapacity(i);
                testing.expect(!result.found_existing);
            }
        }
    
        // Add 10 ... 19, and remove 0 .. 9 on each iteration.
        // Then, check that values 15 to 20 exist in the hash map.
    
        {
            var i: usize = 10;
            while (i < 20) : (i += 1) {
                const result = entries.getOrPutAssumeCapacity(i);
                testing.expect(!result.found_existing);
                testing.expect(entries.remove(i - 10) != null);
            }
    
            var j: usize = 19;
            while (j >= 15) : (j -= 1) {
                const result = entries.getOrPutAssumeCapacity(j);
                testing.expect(result.found_existing);
            }
        }
    
        // Remove 10 .. 19, asserting that all entries exist.
    
        {
            var i: usize = 10;
            while (i < 20) : (i += 1) {
                entries.removeAssertDiscard(i);
            }
        }
    }
  7. LemonBoy commented on Dec 19, 2020

    @LemonBoy
    Contributor

    Oh, I forgot to git add some other changes.
    With all the changes in (to make getOrPutAssumeCapacity work when self.available==0) we take a noticeable performance hit due to the different stop conditions while iterating over the chain (we can't stop anymore when we find a free slot, we may have skipped it due to self.available being zero).

    I'll push an updated PR but I think we should go back to the drawing board and ask if getOrPutAssumeCapacity should just panic (the preconditions wrt the container capacity are missing) if self.available==0 and we're not re-using a tombstoned slot.

  8. added a commit that references this issue on Dec 19, 2020
    f092f7b
  9. lithdew commented on Dec 19, 2020

    @lithdew
    ContributorAuthor

    Oh, I forgot to git add some other changes.
    With all the changes in (to make getOrPutAssumeCapacity work when self.available==0) we take a noticeable performance hit due to the different stop conditions while iterating over the chain (we can't stop anymore when we find a free slot, we may have skipped it due to self.available being zero).

    I'll push an updated PR but I think we should go back to the drawing board and ask if getOrPutAssumeCapacity should just panic (the preconditions wrt the container capacity are missing) if self.available==0 and we're not re-using a tombstoned slot.

    All the tests I have which are using HashMap pass now - thanks!

    Though as you have mentioned, I profiled an app I am working on that extensively uses AutoHashMapUnmanaged and noticed performance regress significantly (from 100ms per op to 2 seconds per op).

    I'd personally be in favor of getOrPutAssumeCapacity panicking if no empty entries or tombstone slots are available.

  10. modified the milestones: 0.8.0, 0.8.1 on Jun 4, 2021
  11. modified the milestones: 0.8.1, 0.9.1 on Aug 31, 2021
  12. modified the milestones: 0.9.1, 0.9.0, 0.10.0 on Nov 20, 2021
  13. modified the milestones: 0.10.0, 0.11.0 on Apr 16, 2022
  14. rohlem commented on Jun 1, 2023

    @rohlem
    Contributor

    Is this issue still present/relevant?
    Both #7468 and this would have been closed by #7472, but instead #10337 was merged.
    The test case that would have been a part of #7472 works (once I remove the .capacity checks, since its definition was changed to include tomb stones).
    The tests posted in this issue (OP and #7494 (comment)) work for me on 0.11.0-dev.3312+ab37ab33c as well.

    @lithdew Do you still have a different reproduction, and/or the program that you created this issue for specifically?
    Or do you remember this issue either appearing or being solved since ef0566d (merged 2021-12-17) ?

  15. modified the milestones: 0.11.0, 0.12.0 on Jul 20, 2023
  16. NicoElbers commented on Feb 18, 2025

    @NicoElbers
    Contributor

    Updated code for 0.14.0-dev.3241+55c46870b, issue seems to no longer repro

    const std = @import("std");
    const testing = std.testing;
    
    test "HashMap" {
        var entries: std.AutoHashMapUnmanaged(usize, void) = .{};
        defer entries.deinit(testing.allocator);
    
        // try entries.ensureCapacity(testing.allocator, 11); // Seems to be renamed
        try entries.ensureTotalCapacity(testing.allocator, 11);
    
        { // Add 0 ... 9.
            var i: usize = 0;
            while (i < 10) : (i += 1) {
                const result = entries.getOrPutAssumeCapacity(i);
                try testing.expect(!result.found_existing);
            }
        }
    
        // Add 10 ... 19, and remove 0 .. 9 on each iteration.
        // Then, check that values 15 to 20 exist in the hash map.
    
        {
            var i: usize = 10;
            while (i < 20) : (i += 1) {
                const result = entries.getOrPutAssumeCapacity(i);
                try testing.expect(!result.found_existing);
                // testing.expect(entries.remove(i - 10) != null); // now returns a bool
                try testing.expect(entries.remove(i - 10));
            }
    
            var j: usize = 19;
            while (j >= 15) : (j -= 1) {
                const result = entries.getOrPutAssumeCapacity(j);
                try testing.expect(result.found_existing);
            }
        }
    
        // Remove 10 .. 19, asserting that all entries exist.
    
        {
            var i: usize = 10;
            while (i < 20) : (i += 1) {
                // No longer exists,
                // I believe this does the same
                // try entries.removeAssertDiscard(i);
                try testing.expect(entries.remove(i));
            }
        }
    }
    > zig test hashmap.zig
    All 1 tests passed.
    
  17. modified the milestones: 0.14.0, 0.14.1 on Mar 1, 2025
  18. modified the milestones: 0.14.1, 0.15.0 on Apr 15, 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

    bugstandard library

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions