-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
implement a hash function for Enums #30500
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
andyferris
reviewed
Dec 25, 2018
Co-Authored-By: KristofferC <kristoffer.carlsson@chalmers.se>
I rewrote this slightly to make it a bit easier to understand (IMO). This should be good to go now. cc @JeffBezanson if you want to take another look. |
jmert
added a commit
to jmert/BitFlags.jl
that referenced
this pull request
May 21, 2022
Duplicates improvement made to Base's Enums: JuliaLang/julia#30500
jmert
added a commit
to jmert/BitFlags.jl
that referenced
this pull request
May 21, 2022
Duplicates improvement made to Base's Enums: JuliaLang/julia#30500 **Before:** ```julia julia> @bitflag Flag1 flag1=0 flag2 flag3 flag4 julia> @benchmark hash(flag1) BenchmarkTools.Trial: 10000 samples with 1000 evaluations. Range (min … max): 11.035 ns … 52.801 ns ┊ GC (min … max): 0.00% … 0.00% Time (median): 15.714 ns ┊ GC (median): 0.00% Time (mean ± σ): 16.003 ns ± 1.305 ns ┊ GC (mean ± σ): 0.00% ± 0.00% ▁▃ █▁▄ ▁ ▂▂▂▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▄▂▃▄██▆███▃▃▂▂▂▂▆▅█▆▆█▅▃▂ ▃ 11 ns Histogram: frequency by time 17.5 ns < Memory estimate: 0 bytes, allocs estimate: 0. ``` **After:** ```julia julia> @benchmark hash(flag1) BenchmarkTools.Trial: 10000 samples with 1000 evaluations. Range (min … max): 1.397 ns … 33.455 ns ┊ GC (min … max): 0.00% … 0.00% Time (median): 4.819 ns ┊ GC (median): 0.00% Time (mean ± σ): 4.907 ns ± 1.042 ns ┊ GC (mean ± σ): 0.00% ± 0.00% ██ ▅▅ ▂▁▁▁▁▂▂▂▁▁▁▁▂▃▂▁▁▁▁▂▂▁▁▁▂▃▃▂▁▁▁▂▂▂▁▁▁▁▂▂▂▁▁▃███▆▁▂▄███▃▁▂▂ ▃ 1.4 ns Histogram: frequency by time 5.66 ns < Memory estimate: 0 bytes, allocs estimate: 0. ```
jmert
added a commit
to jmert/BitFlags.jl
that referenced
this pull request
May 21, 2022
Duplicates improvement made to Base's Enums: JuliaLang/julia#30500 **Before:** ```julia julia> @bitflag Flag1 flag1=0 flag2 flag3 flag4 julia> @benchmark hash(flag1) BenchmarkTools.Trial: 10000 samples with 1000 evaluations. Range (min … max): 11.035 ns … 52.801 ns ┊ GC (min … max): 0.00% … 0.00% Time (median): 15.714 ns ┊ GC (median): 0.00% Time (mean ± σ): 16.003 ns ± 1.305 ns ┊ GC (mean ± σ): 0.00% ± 0.00% ▁▃ █▁▄ ▁ ▂▂▂▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▄▂▃▄██▆███▃▃▂▂▂▂▆▅█▆▆█▅▃▂ ▃ 11 ns Histogram: frequency by time 17.5 ns < Memory estimate: 0 bytes, allocs estimate: 0. ``` **After:** ```julia julia> @benchmark hash(flag1) BenchmarkTools.Trial: 10000 samples with 1000 evaluations. Range (min … max): 1.397 ns … 33.455 ns ┊ GC (min … max): 0.00% … 0.00% Time (median): 4.819 ns ┊ GC (median): 0.00% Time (mean ± σ): 4.907 ns ± 1.042 ns ┊ GC (mean ± σ): 0.00% ± 0.00% ██ ▅▅ ▂▁▁▁▁▂▂▂▁▁▁▁▂▃▂▁▁▁▁▂▂▁▁▁▂▃▃▂▁▁▁▂▂▂▁▁▁▁▂▂▂▁▁▃███▆▁▂▄███▃▁▂▂ ▃ 1.4 ns Histogram: frequency by time 5.66 ns < Memory estimate: 0 bytes, allocs estimate: 0. ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Going via the fallback
object_id
is slow and I have also seen it make my hash function start to allocate when implementinghash
for structs containing enums.This will create hash-collisions if different enums with the same name are used in the same hashmap but that seems quite unlikely(?). Suggestions for better implementations are welcome.