Skip to content

Commit 5cd1e3e

Browse files
authored
implement with fewer afoldl methods (#39414)
With constant-propagation, inference (and Varargs runtime) is likely better able to handle this version now (and it removes the n^2 behavior definitions for semi-low argument counts). Now we also need to force Vararg specialization up to 16 arguments manually, so we do that explicitly (and slightly hack-y). Fixes regression in #39175
1 parent 527d6b6 commit 5cd1e3e

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

base/operators.jl

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,37 @@ xor(x::Integer) = x
577577

578578
const = xor
579579

580-
# foldl for argument lists. expand recursively up to a point, then
581-
# switch to a loop. this allows small cases like `a+b+c+d` to be inlined
580+
# foldl for argument lists. expand fully up to a point, then
581+
# switch to a loop. this allows small cases like `a+b+c+d` to be managed
582582
# efficiently, without a major slowdown for `+(x...)` when `x` is big.
583-
afoldl(op,a) = a
584-
afoldl(op,a,b) = op(a,b)
585-
afoldl(op,a,b,c...) = afoldl(op, op(a,b), c...)
586-
function afoldl(op,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,qs...)
587-
y = op(op(op(op(op(op(op(op(op(op(op(op(op(op(op(a,b),c),d),e),f),g),h),i),j),k),l),m),n),o),p)
588-
for x in qs; y = op(y,x); end
589-
y
583+
# n.b.: keep this method count small, so it can be inferred without hitting the
584+
# method count limit in inference
585+
afoldl(op, a) = a
586+
function afoldl(op, a, bs...)
587+
l = length(bs)
588+
i = 0; y = a; l == i && return y
589+
#@nexprs 15 i -> (y = op(y, bs[i]); l == i && return y)
590+
i = 1; y = op(y, bs[i]); l == i && return y
591+
i = 2; y = op(y, bs[i]); l == i && return y
592+
i = 3; y = op(y, bs[i]); l == i && return y
593+
i = 4; y = op(y, bs[i]); l == i && return y
594+
i = 5; y = op(y, bs[i]); l == i && return y
595+
i = 6; y = op(y, bs[i]); l == i && return y
596+
i = 7; y = op(y, bs[i]); l == i && return y
597+
i = 8; y = op(y, bs[i]); l == i && return y
598+
i = 9; y = op(y, bs[i]); l == i && return y
599+
i = 10; y = op(y, bs[i]); l == i && return y
600+
i = 11; y = op(y, bs[i]); l == i && return y
601+
i = 12; y = op(y, bs[i]); l == i && return y
602+
i = 13; y = op(y, bs[i]); l == i && return y
603+
i = 14; y = op(y, bs[i]); l == i && return y
604+
i = 15; y = op(y, bs[i]); l == i && return y
605+
for i in (i + 1):l
606+
y = op(y, bs[i])
607+
end
608+
return y
590609
end
610+
typeof(afoldl).name.mt.max_args = 18
591611

592612
for op in (:+, :*, :&, :|, :xor, :min, :max, :kron)
593613
@eval begin

test/operators.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,5 @@ end
277277

278278
a = rand(3, 3)
279279
@test transpose(a) === a'
280+
281+
@test [Base.afoldl(+, 1:i...) for i = 1:40] == [i * (i + 1) ÷ 2 for i = 1:40]

0 commit comments

Comments
 (0)