Skip to content

Commit

Permalink
specialize on mapfoldl(::Type, ...) (#39019)
Browse files Browse the repository at this point in the history
Before:
```julia
julia> using BenchmarkTools

julia> b = rand(Bool, 10000);

julia> @Btime mapfoldl(Int, +, b; init=0)
  1.136 ms (9025 allocations: 141.02 KiB)
5057
```

After:
```julia
julia> @Btime mapfoldl(Int, +, b; init=0)
  1.094 μs (1 allocation: 16 bytes)
5057
```
  • Loading branch information
simeonschaub committed Dec 30, 2020
1 parent 31eb6c2 commit 3d922e4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Create a mapping reducing function `rf′(acc, x) = rf(acc, f(x))`.
struct MappingRF{F, T}
f::F
rf::T
MappingRF(f::F, rf::T) where {F,T} = new{F,T}(f, rf)
MappingRF(::Type{f}, rf::T) where {f,T} = new{Type{f},T}(f, rf)
end

@inline (op::MappingRF)(acc, x) = op.rf(acc, op.f(x))
Expand Down
3 changes: 3 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,6 @@ x = [j+7 for j in i]
Iterators.flatten((1:2, 3:4)),
) == (1, 4)
end

# make sure we specialize on mapfoldl(::Type, ...)
@test @inferred(mapfoldl(Int, +, [1, 2, 3]; init=0)) === 6

0 comments on commit 3d922e4

Please sign in to comment.