Skip to content

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 6 commits into from
May 19, 2022
Merged

implement a hash function for Enums #30500

merged 6 commits into from
May 19, 2022

Conversation

KristofferC
Copy link
Member

Going via the fallback object_id is slow and I have also seen it make my hash function start to allocate when implementing hash 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.

@KristofferC
Copy link
Member Author

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.

@KristofferC KristofferC added the merge me PR is reviewed. Merge when all tests are passing label May 17, 2022
@KristofferC KristofferC merged commit fb672da into master May 19, 2022
@KristofferC KristofferC deleted the kc/hash_enum branch May 19, 2022 06:41
@KristofferC KristofferC added the performance Must go faster label May 19, 2022
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.
```
@giordano giordano removed the merge me PR is reviewed. Merge when all tests are passing label Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants