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

ProgressMeter Deprecation Warning and nothing comparison #536

Merged
merged 3 commits into from
Dec 19, 2023

Conversation

dylan-asmar
Copy link
Member

  • Updated the calls to Progress in line with the deprecation warning
  • Changed comparisons to nothing to use === vs ==

Closes #533

@zsunberg zsunberg merged commit d2e891f into master Dec 19, 2023
8 checks passed
@zsunberg
Copy link
Member

Just out of curiosity, what is the advantage of === for nothing? Is there a link?

@dylan-asmar
Copy link
Member Author

dylan-asmar commented Dec 19, 2023

I thought there was a reference in the performance section of the Julia documentation, but I can't seem to find it.

I have run into an issue once where == wasn't checking against nothing correctly because a custom type == operator wasn't implemented 100% correctly (my fault). However, ever since then, I have defaulted to === for a few reasons:

  • Efficiency: === is generally more efficient than == because it simply compares the memory addresses of the objects rather than checking for value equality. nothing I believe is a singleton and its identity is unique and constant.
  • Clarity and Safety: Using === with nothing ensures that you are specifically checking for the absence of a value, rather than a value that might be considered equivalent to nothing. This can prevent unexpected behavior in cases where an object's value comparison (==) to nothing might be true even though the object isn't actually nothing. This is probably niche though.
  • Avoiding Errors with Custom Types: If a custom type doesn't properly implement the == operator, comparing an instance of that type to nothing using == might result in errors or incorrect behavior. Since === does not rely on the potentially unimplemented or incorrectly implemented == method, it avoids this issue. This might be less of a thing in the last few versions of Julia...but in the back of my mind.

The Julia implementation of isnothing changed to just simply be === around 1.7 I think. I think there are some compile benefits of === as well...but I am not as certain about that one.

julia> x = [1, 3, 4];

julia> @benchmark x == nothing
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min  max):  3.041 ns  12.125 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     3.125 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   3.126 ns ±  0.214 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

             ▆           █          ▃           ▂            ▁
  █▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁█ █
  3.04 ns      Histogram: log(frequency) by time     3.25 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark x === nothing
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min  max):  1.500 ns  6.292 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     1.583 ns             ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.569 ns ± 0.059 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▁            ▆▇             █             ▃               ▁
  █▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁█ █
  1.5 ns      Histogram: log(frequency) by time     1.67 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark isnothing(x)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min  max):  1.458 ns  16.417 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     1.583 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.579 ns ±  0.172 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

          ▁       █       █▇       ▅       ▂       ▂         ▂
  ▃▁▁▁▁▁▁▁█▁▁▁▁▁▁▁█▁▁▁▁▁▁▁██▁▁▁▁▁▁▁█▁▁▁▁▁▁▁█▁▁▁▁▁▁▁█▁▁▁▁▁▁▁█ █
  1.46 ns      Histogram: log(frequency) by time     1.75 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

@dylan-asmar dylan-asmar deleted the progressmeter_deprecation_warn branch December 19, 2023 22:56
@zsunberg
Copy link
Member

wow, thanks for the thorough explanation! But actually, why aren't we using isnothing?

@dylan-asmar
Copy link
Member Author

dylan-asmar commented Dec 30, 2023

I changed the code to use isnothing to begin with. Then changed it to === to keep the code similar while still achieving the same result. I think we should probably use isnothing (at least in the future).

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.

Deprecation warning with ProgressMeter using HistoryRecorder
2 participants