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

Override ==, any() and all() for AbstractArray{:>Null} #37

Merged
merged 1 commit into from
Oct 4, 2017
Merged

Conversation

nalimilan
Copy link
Member

@nalimilan nalimilan commented Oct 2, 2017

This is needed to return null when needed instead of throwing an error.
The methods will (and have to) also apply to Array{Any}, but "a little bit
type piracy is not the end of the world", at least temporarily.

This is needed to return null when needed instead of throwing an error.
The methods will (and have to) also apply to Array{Any}, but "a little bit
type piracy is not the end of the world", at least temporarily.
@ararslan
Copy link
Member

ararslan commented Oct 2, 2017

but "a little bit of type piracy is not the end of the world", at least temporarily.

grumble grumble

anynull = false
@inbounds for (a, b) in zip(A, B)
eq = (a == b)
if eq === false
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't have thought to use === for checking booleans before seeing this, but it's noticably faster

julia> @benchmark false === false
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     0.038 ns (0.00% GC)
  median time:      0.048 ns (0.00% GC)
  mean time:        0.052 ns (0.00% GC)
  maximum time:     14.335 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000

julia> @benchmark false == false
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     2.353 ns (0.00% GC)
  median time:      2.958 ns (0.00% GC)
  mean time:        3.835 ns (0.00% GC)
  maximum time:     4.157 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000

is there any reason to not always use === rather than == for boolean comparison?

Copy link
Member

Choose a reason for hiding this comment

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

is there any reason to not always use === rather than == for boolean comparison?

In general, boolean comparisons aren't that common, especially in control flow, since it's typically easier to just say if eq or if !eq. In this case === is needed to ensure that null doesn't propagate in the comparison.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wouldn't trust benchmarks on elementary operations like this. Wrapping these in a function gives exactly the same native code.

I could also have used isequal(eq, true), but using === we ensure no other type can override isequal, as "truthiness" isn't considered as a good idea in general in Base Julia.

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.

None yet

4 participants