Skip to content

reduce does not re-associate operations #48129

@mikmoore

Description

@mikmoore

reduce misses the opportunity to re-associate operations when given NTuple arguments. This has performance impacts on notable functions such as sum(::NTuple). One can use @fastmath to permit re-association in some cases, but I'm never a fan of suggesting @fastmath to people.

julia> using BenchmarkTools

julia> x = ntuple(_->randn(), 16);

julia> @btime sum($x)
  5.700 ns (0 allocations: 0 bytes)
-2.8324452894507908

julia> @btime @fastmath(reduce(+,$x))
  3.100 ns (0 allocations: 0 bytes)
-2.8324452894507903

You can see the lack of vectorization by comparing
code_native(sum,Tuple{NTuple{16,Float64}};debuginfo=:none)
to the SIMDing version
code_native(x->@fastmath(reduce(+,x)),Tuple{NTuple{16,Float64}};debuginfo=:none).

Methods like sum(::Vector) already do re-associate (with performance and accuracy benefits).

Metadata

Metadata

Assignees

No one assigned

    Labels

    foldsum, maximum, reduce, foldl, etc.performanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions