Skip to content

faster xor and != for Bool #31486

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

Closed
wants to merge 1 commit into from
Closed

faster xor and != for Bool #31486

wants to merge 1 commit into from

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Mar 26, 2019

See discourse. Before this change:

julia> b = rand(Bool, 150); i = UInt8.(b);

julia> f(a) = reduce(xor, a); g(a) = reduce(!=, a);

julia> @btime f($b); @btime g($b);
  84.023 ns (0 allocations: 0 bytes)
  92.179 ns (0 allocations: 0 bytes)

julia> @btime f($i); @btime g($i);
  14.540 ns (0 allocations: 0 bytes)
  97.243 ns (0 allocations: 0 bytes)

After this change:

julia> @btime f($b); @btime g($b);
  14.557 ns (0 allocations: 0 bytes)
  14.647 ns (0 allocations: 0 bytes)

Before this change:
```jl
julia> b = rand(Bool, 150); i = UInt8.(b);

julia> f(a) = reduce(xor, a); g(a) = reduce(!=, a);

julia> @Btime f($b); @Btime g($b);
  84.023 ns (0 allocations: 0 bytes)
  92.179 ns (0 allocations: 0 bytes)

julia> @Btime f($i); @Btime g($i);
  14.540 ns (0 allocations: 0 bytes)
  97.243 ns (0 allocations: 0 bytes)
```
After this change:
```jl
julia> @Btime f($b); @Btime g($b);
  14.557 ns (0 allocations: 0 bytes)
  14.647 ns (0 allocations: 0 bytes)
```
@stevengj stevengj added the performance Must go faster label Mar 26, 2019
@stevengj
Copy link
Member Author

stevengj commented Mar 26, 2019

Weirdly, for UInt8, !== is much faster than !=, and it is also faster than !== for Bool:

julia> h(a) = reduce(!==, a);

julia> @btime h($b); @btime h($i);
  89.135 ns (0 allocations: 0 bytes)
  8.933 ns (0 allocations: 0 bytes)

even though @code_native (!==)(true,false) and @code_native (!==)(0x01,0x02) have identical output. What gives?

@KristofferC
Copy link
Member

Probably specialization heuristics. That's usually the case when the `code_´ doesn't tell the truth about what code end up being run.

@vtjnash
Copy link
Member

vtjnash commented Mar 26, 2019

Ah, I found some potential codegen optimizations to be able to this automatically–and they made things worse :P

I think we used to have this optimization, but had to remove it because we don't zero-initialize arrays (#30350). This PR possibly just finds a code path that we don't sanitize and codegen correctly.

@vtjnash vtjnash closed this in ef82b7b Apr 3, 2019
@DilumAluthge DilumAluthge deleted the stevengj-patch-3 branch March 25, 2021 21:58
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.

3 participants