Skip to content

Commit 3d922e4

Browse files
authored
specialize on mapfoldl(::Type, ...) (#39019)
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 ```
1 parent 31eb6c2 commit 3d922e4

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

base/reduce.jl

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Create a mapping reducing function `rf′(acc, x) = rf(acc, f(x))`.
8888
struct MappingRF{F, T}
8989
f::F
9090
rf::T
91+
MappingRF(f::F, rf::T) where {F,T} = new{F,T}(f, rf)
92+
MappingRF(::Type{f}, rf::T) where {f,T} = new{Type{f},T}(f, rf)
9193
end
9294

9395
@inline (op::MappingRF)(acc, x) = op.rf(acc, op.f(x))

test/reduce.jl

+3
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,6 @@ x = [j+7 for j in i]
640640
Iterators.flatten((1:2, 3:4)),
641641
) == (1, 4)
642642
end
643+
644+
# make sure we specialize on mapfoldl(::Type, ...)
645+
@test @inferred(mapfoldl(Int, +, [1, 2, 3]; init=0)) === 6

0 commit comments

Comments
 (0)