Skip to content

Conversation

@LilithHafner
Copy link
Member

These functions should only accept one argument signature so we should fully specialize.

Fixes #54141

I was also going to make this change for flatten but couldn't demonstrate a performance improvement.

Performance demo (taken from the OP of the linked issue):

julia> versioninfo()
Julia Version 1.12.0-DEV.344
Commit 7ba1b332f87 (2024-04-15 07:40 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 8 × unknown
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, apple-m2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

julia> using StaticArrays, LinearAlgebra

julia> ab = (a=zeros(SVector{3,Float64}, 1000000),
             b=zeros(SVector{3,Float64}, 1000000));

julia> f_bitwise(ab) = (first.(ab.a).>0) .| (norm.(ab.b) .!= 0)
f_bitwise (generic function with 1 method)

julia> f_boolean(ab) = (first.(ab.a).>0) .|| (norm.(ab.b) .!= 0)
f_boolean (generic function with 1 method)

julia> f_bitwise(ab) == f_boolean(ab)
true

julia> using Chairmarks

julia> @b f_bitwise($ab)
2.282 ms (4 allocs: 122.219 KiB)

julia> @b f_boolean($ab)
51.262 ms (3000004 allocs: 76.413 MiB, 7.88% gc time)

julia> @eval Base.Broadcast function broadcasted(::OrOr, a, bc::Broadcasted)
           bcf = flatten(bc)
           broadcasted(((a, args::Vararg{Any, N}) where {N}) -> a || bcf.f(args...), a, bcf.args...)
       end
broadcasted (generic function with 67 methods)

julia> @b f_bitwise($ab)
2.294 ms (4 allocs: 122.219 KiB)

julia> @b f_boolean($ab)
3.238 ms (4 allocs: 122.219 KiB)

…onstructed by oror and andand

These functions should only accept one argument signature so we should fully specialize.
@LilithHafner LilithHafner added performance Must go faster broadcast Applying a function over a collection labels Apr 19, 2024
@LilithHafner LilithHafner added the merge me PR is reviewed. Merge when all tests are passing label Apr 19, 2024
@LilithHafner LilithHafner merged commit 3364aa5 into master Apr 19, 2024
@LilithHafner LilithHafner deleted the lh/broadcast-perf-specialization branch April 19, 2024 21:50
@LilithHafner LilithHafner removed the merge me PR is reviewed. Merge when all tests are passing label Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

broadcast Applying a function over a collection performance Must go faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

broadcast of short-circuiting boolean operator || generates runtime dispatch and per-element allocations

2 participants