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

add isequal and hash methods to check tuple in BoundaryIndex Sets without type piracy #835

Merged
merged 4 commits into from
Nov 7, 2023

Conversation

koehlerson
Copy link
Member

Copy link
Member

@KnutAM KnutAM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Are there any performance differences?

src/Grid/grid.jl Outdated Show resolved Hide resolved
@koehlerson
Copy link
Member Author

koehlerson commented Nov 3, 2023

Nice!

Are there any performance differences?

For unordered Set:
master

julia> @benchmark in((1,2),testset)
BenchmarkTools.Trial: 10000 samples with 989 evaluations.
 Range (min … max):  48.218 ns … 69.210 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     48.507 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   48.717 ns ±  0.988 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▆▆█▇█▆▄                              ▁ ▁▁                   ▂
  ███████▆▃▃▁▄▄▅▃▅▄▅▄▃▁▅▃▃▄▃▁▄▄▅▄▄▁▅▆▇█████▇▇█▇██▆▇▇▇▆▇▇▆▆▆▆▆ █
  48.2 ns      Histogram: log(frequency) by time        53 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

PR

julia> @benchmark in((1,2),testset)
BenchmarkTools.Trial: 10000 samples with 994 evaluations.
 Range (min … max):  31.995 ns … 63.816 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     32.391 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   32.565 ns ±  1.520 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▃  █▃                                                       ▁
  █▅▃██▆▃▃▁▁▁▃▁▁▁▁▁▁▃▁▁▁▁▁▁▁▃▁▁▁▁▁▃▁▁▁▁▁▃▄▃▃▆▅▄▆▅▄▄▅▅▅▅▃▄▇▄▃▄ █
  32 ns        Histogram: log(frequency) by time      38.6 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
 

Co-authored-by: Knut Andreas Meyer <knutam@gmail.com>
@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (fe44e7f) 92.67% compared to head (7e9837d) 92.98%.
Report is 4 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #835      +/-   ##
==========================================
+ Coverage   92.67%   92.98%   +0.31%     
==========================================
  Files          33       33              
  Lines        4955     4950       -5     
==========================================
+ Hits         4592     4603      +11     
+ Misses        363      347      -16     
Files Coverage Δ
src/Grid/grid.jl 92.80% <75.00%> (-0.32%) ⬇️

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@KnutAM KnutAM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Perhaps add a changelog entry before merging

@koehlerson
Copy link
Member Author

koehlerson commented Nov 6, 2023

I just noticed the following, which I encountered in the p4est branch as well. If we hash now FaceIndex(1,2) and EdgeIndex(1,2) the hash will be the same. This can be problematic if you have Set{Union{FaceIndex,EdgeIndex}} and do some set operations (which I think rely on the hashes and not only the isequal operation). In the p4est branch I just wrapped the Tuple{Int,Int} in a Tuple{Int,Tuple{Int,Int}} where the first int is sort of a category integer, but not sure if relevant

Edit: Nvm, the set operations seem fine as well as a small test with a dict

@KnutAM
Copy link
Member

KnutAM commented Nov 6, 2023

Good point - did not think of this!

I tried to understand what's going on, and AFAIU it works because isequal(FaceIndex(1,2), EdgeIndex(1,2)) is false, such that the following check give false and the loop continues until eventually returning a negative index (indicating that the key wasn't found), for a set without FaceIndex(1,2) but with EdgeIndex(1,2).

https://github.com/JuliaLang/julia/blob/6d4f40957c6e68ecfd1dc13115beadff493406fd/base/dict.jl#L289

That being said, I think we need @fredrikekre or @KristofferC to confirm that this way is ok. The rule for defining the hash: "Compute an integer hash code such that isequal(x,y) implies hash(x)==hash(y)", use implies, so to my understanding the implementation in this PR follows the docstring.

@koehlerson
Copy link
Member Author

yeah I'm just unsure if the statement needs to hold the other way around too. So should hash(x) == hash(y) imply isequal(x,y). If so, then we should change it.

src/Grid/grid.jl Outdated Show resolved Hide resolved
@KnutAM
Copy link
Member

KnutAM commented Nov 6, 2023

yeah I'm just unsure if the statement needs to hold the other way around too. So should hash(x) == hash(y) imply isequal(x,y). If so, then we should change it.

At least the docstring for isequal is consistent with our interpretation: "isequal(x,y) must imply that hash(x) == hash(y)."

@termi-official
Copy link
Member

yeah I'm just unsure if the statement needs to hold the other way around too. So should hash(x) == hash(y) imply isequal(x,y). If so, then we should change it.

At least the docstring for isequal is consistent with our interpretation: "isequal(x,y) must imply that hash(x) == hash(y)."

This does not imply the reverse.

Copy link
Member

@fredrikekre fredrikekre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. You would still see strange things for e.g. Set{Union{FaceIndex, Tuple{Int,Int}}} where there is no way to differentiate FaceIndex((1, 2)) and (1, 2). I don't think such mixed sets are so common though.

src/Grid/grid.jl Outdated Show resolved Hide resolved
Co-authored-by: Knut Andreas Meyer <knutam@gmail.com>
@fredrikekre fredrikekre merged commit d0f0e07 into master Nov 7, 2023
7 checks passed
@fredrikekre fredrikekre deleted the mk/idxcheck branch November 7, 2023 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants