Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hash#reject! for non-equality key #10511

Conversation

straight-shoota
Copy link
Member

Fixes the immediate issue reported in #3923. It does not decide the discussion on hashing of NaN values but ensures equal behaviour between #reject and #reject! (and by extension, #select and #select!).

Currently, you get this behaviour because #reject! deletes by key which is based on quality of the key value:

h = {Float64::NAN => true}
h.reject(&.nan?)  # => {}
h.reject!(&.nan?) # {Float64::NAN => true}

With this change the latter call also properly deletes the entry.

I would expect this change to also improve performance because it avoids a duplicate hash lookup on deletion, but I couldn't actually demonstrate that in a benchmark. Performance is actually stable.
Maybe I'm missing something.
But it fixes a bug and doesn't deteriorate performance, so it's fine to merge.

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection labels Mar 14, 2021
@straight-shoota straight-shoota added this to the 1.5.0 milestone Jun 2, 2022
@straight-shoota straight-shoota merged commit 1597576 into crystal-lang:master Jun 3, 2022
@straight-shoota straight-shoota deleted the fix/hash-reject-nocompare-key branch June 3, 2022 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants