hashbrown icon indicating copy to clipboard operation
hashbrown copied to clipboard

Fix `HashSet::get_or_insert_with`

Open JustForFun88 opened this issue 2 years ago • 4 comments

Fix #399. Tried to do it without additional overhead. Unless there are additional comparisons.

JustForFun88 avatar Feb 16 '23 13:02 JustForFun88

Should an unsafe _unchecked function also be added? Hashing and comparing the insert_value should be unnecessary if this function is used properly.

yyny avatar Feb 16 '23 18:02 yyny

Should an unsafe _unchecked function also be added? Hashing and comparing the insert_value should be unnecessary if this function is used properly.

Well, strictly speaking, rehashing was present in the old version of the code. Only two comparisons and a panic were added here. Let's see what @Amanieu says :-).

In theory, as I understand it, there is nothing that violates memory or causes UB in the fact that there are two or more identical elements in a Hashmap or HashSet. It just increases the collision, the elements after the first one will be lost and will only show up when iterating. The documentation for HashMap::insert_unique_unchecked scares people a little. Anyway, I can't even imagine when insert_unique_unchecked «operation may panic, loop forever, or any following operation with the map may panic, loop forever or return arbitrary result».

JustForFun88 avatar Feb 16 '23 19:02 JustForFun88

:umbrella: The latest upstream changes (presumably #390) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Mar 24 '23 07:03 bors

:umbrella: The latest upstream changes (presumably #533) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Jun 19 '24 17:06 bors