You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Interleaving remove() / getOrPutAssumeCapacity() calls to std.HashMap causes existing entries in a hash map to be reported to not be found. #7494
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().
conststd=@import("std");
consttesting=std.testing;
test"HashMap" {
varentries: std.AutoHashMapUnmanaged(usize, void) = .{};
deferentries.deinit(testing.allocator);
tryentries.ensureCapacity(testing.allocator, 11);
{ // Add 0 ... 9.vari: usize=0;
while (i<10) : (i+=1) {
constresult=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.
{
vari: usize=10;
while (i<20) : (i+=1) {
constresult=entries.getOrPutAssumeCapacity(i);
testing.expect(!result.found_existing);
testing.expect(entries.remove(i-10) !=null);
}
varj: usize=15;
while (j<20) : (j+=1) {
constresult=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.
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
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.
conststd=@import("std");
consttesting=std.testing;
test"HashMap" {
varentries: std.AutoHashMapUnmanaged(usize, void) = .{};
deferentries.deinit(testing.allocator);
tryentries.ensureCapacity(testing.allocator, 11);
{ // Add 0 ... 9.vari: usize=0;
while (i<10) : (i+=1) {
constresult=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.
{
vari: usize=10;
while (i<20) : (i+=1) {
constresult=entries.getOrPutAssumeCapacity(i);
testing.expect(!result.found_existing);
testing.expect(entries.remove(i-10) !=null);
}
varj: usize=19;
while (j>=15) : (j-=1) {
constresult=entries.getOrPutAssumeCapacity(j);
testing.expect(result.found_existing);
}
}
// Remove 10 .. 19, asserting that all entries exist.
{
vari: usize=10;
while (i<20) : (i+=1) {
entries.removeAssertDiscard(i);
}
}
}
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.
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.
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) ?
Activity
[-]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.[/+]std: Don't stop probing at first tombstone
LemonBoy commentedon Dec 19, 2020
Yo, I've pushed a fix for this in #7472. Please let me know if it works for you.
lithdew commentedon Dec 19, 2020
Thanks for the fix! Extended the test case to additionally
removeAssertDiscard()all entries that should exist in the hash map, which unfortunately fails.LemonBoy commentedon Dec 19, 2020
Oh, I forgot to
git addsome other changes.With all the changes in (to make
getOrPutAssumeCapacitywork whenself.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 toself.availablebeing zero).I'll push an updated PR but I think we should go back to the drawing board and ask if
getOrPutAssumeCapacityshould just panic (the preconditions wrt the container capacity are missing) ifself.available==0and we're not re-using a tombstoned slot.std: Don't stop probing at first tombstone
lithdew commentedon Dec 19, 2020
All the tests I have which are using
HashMappass now - thanks!Though as you have mentioned, I profiled an app I am working on that extensively uses
AutoHashMapUnmanagedand noticed performance regress significantly (from 100ms per op to 2 seconds per op).I'd personally be in favor of
getOrPutAssumeCapacitypanicking if no empty entries or tombstone slots are available.rohlem commentedon Jun 1, 2023
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
.capacitychecks, 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+ab37ab33cas 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) ?
NicoElbers commentedon Feb 18, 2025
Updated code for 0.14.0-dev.3241+55c46870b, issue seems to no longer repro