Skip to content

Commit fbffe7c

Browse files
authored
Merge pull request #40468 from JuliaLang/ksh/tuple32
Allow use of 32 element tuples without dynamic allocation
2 parents 29d5158 + 9300073 commit fbffe7c

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

base/combinatorics.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function isperm(P::Tuple)
9191
end
9292
end
9393

94-
isperm(P::Any16) = _isperm(P)
94+
isperm(P::Any32) = _isperm(P)
9595

9696
# swap columns i and j of a, in-place
9797
function swapcols!(a::AbstractMatrix, i, j)
@@ -286,7 +286,7 @@ function invperm(P::Tuple)
286286
end
287287
end
288288

289-
invperm(P::Any16) = Tuple(invperm(collect(P)))
289+
invperm(P::Any32) = Tuple(invperm(collect(P)))
290290

291291
#XXX This function should be moved to Combinatorics.jl but is currently used by Base.DSP.
292292
"""

base/operators.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ afoldl(op, a) = a
590590
function afoldl(op, a, bs...)
591591
l = length(bs)
592592
i = 0; y = a; l == i && return y
593-
#@nexprs 15 i -> (y = op(y, bs[i]); l == i && return y)
593+
#@nexprs 31 i -> (y = op(y, bs[i]); l == i && return y)
594594
i = 1; y = op(y, bs[i]); l == i && return y
595595
i = 2; y = op(y, bs[i]); l == i && return y
596596
i = 3; y = op(y, bs[i]); l == i && return y
@@ -606,12 +606,28 @@ function afoldl(op, a, bs...)
606606
i = 13; y = op(y, bs[i]); l == i && return y
607607
i = 14; y = op(y, bs[i]); l == i && return y
608608
i = 15; y = op(y, bs[i]); l == i && return y
609+
i = 16; y = op(y, bs[i]); l == i && return y
610+
i = 17; y = op(y, bs[i]); l == i && return y
611+
i = 18; y = op(y, bs[i]); l == i && return y
612+
i = 19; y = op(y, bs[i]); l == i && return y
613+
i = 20; y = op(y, bs[i]); l == i && return y
614+
i = 21; y = op(y, bs[i]); l == i && return y
615+
i = 22; y = op(y, bs[i]); l == i && return y
616+
i = 23; y = op(y, bs[i]); l == i && return y
617+
i = 24; y = op(y, bs[i]); l == i && return y
618+
i = 25; y = op(y, bs[i]); l == i && return y
619+
i = 26; y = op(y, bs[i]); l == i && return y
620+
i = 27; y = op(y, bs[i]); l == i && return y
621+
i = 28; y = op(y, bs[i]); l == i && return y
622+
i = 29; y = op(y, bs[i]); l == i && return y
623+
i = 30; y = op(y, bs[i]); l == i && return y
624+
i = 31; y = op(y, bs[i]); l == i && return y
609625
for i in (i + 1):l
610626
y = op(y, bs[i])
611627
end
612628
return y
613629
end
614-
typeof(afoldl).name.mt.max_args = 18
630+
typeof(afoldl).name.mt.max_args = 34
615631

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

base/tuple.jl

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,17 @@ map(f, t::Tuple{Any, Any}) = (@_inline_meta; (f(t[1]), f(t[2])))
215215
map(f, t::Tuple{Any, Any, Any}) = (@_inline_meta; (f(t[1]), f(t[2]), f(t[3])))
216216
map(f, t::Tuple) = (@_inline_meta; (f(t[1]), map(f,tail(t))...))
217217
# stop inlining after some number of arguments to avoid code blowup
218-
const Any16{N} = Tuple{Any,Any,Any,Any,Any,Any,Any,Any,
219-
Any,Any,Any,Any,Any,Any,Any,Any,Vararg{Any,N}}
220-
const All16{T,N} = Tuple{T,T,T,T,T,T,T,T,
221-
T,T,T,T,T,T,T,T,Vararg{T,N}}
222-
function map(f, t::Any16)
218+
const Any32{N} = Tuple{Any,Any,Any,Any,Any,Any,Any,Any,
219+
Any,Any,Any,Any,Any,Any,Any,Any,
220+
Any,Any,Any,Any,Any,Any,Any,Any,
221+
Any,Any,Any,Any,Any,Any,Any,Any,
222+
Vararg{Any,N}}
223+
const All32{T,N} = Tuple{T,T,T,T,T,T,T,T,
224+
T,T,T,T,T,T,T,T,
225+
T,T,T,T,T,T,T,T,
226+
T,T,T,T,T,T,T,T,
227+
Vararg{T,N}}
228+
function map(f, t::Any32)
223229
n = length(t)
224230
A = Vector{Any}(undef, n)
225231
for i=1:n
@@ -235,7 +241,7 @@ function map(f, t::Tuple, s::Tuple)
235241
@_inline_meta
236242
(f(t[1],s[1]), map(f, tail(t), tail(s))...)
237243
end
238-
function map(f, t::Any16, s::Any16)
244+
function map(f, t::Any32, s::Any32)
239245
n = length(t)
240246
A = Vector{Any}(undef, n)
241247
for i = 1:n
@@ -251,7 +257,7 @@ function map(f, t1::Tuple, t2::Tuple, ts::Tuple...)
251257
@_inline_meta
252258
(f(heads(t1, t2, ts...)...), map(f, tails(t1, t2, ts...)...)...)
253259
end
254-
function map(f, t1::Any16, t2::Any16, ts::Any16...)
260+
function map(f, t1::Any32, t2::Any32, ts::Any32...)
255261
n = length(t1)
256262
A = Vector{Any}(undef, n)
257263
for i = 1:n
@@ -317,8 +323,8 @@ function _totuple(T, itr, s...)
317323
end
318324

319325
# use iterative algorithm for long tuples
320-
function _totuple(T::Type{All16{E,N}}, itr) where {E,N}
321-
len = N+16
326+
function _totuple(T::Type{All32{E,N}}, itr) where {E,N}
327+
len = N+32
322328
elts = collect(E, Iterators.take(itr,len))
323329
if length(elts) != len
324330
_totuple_err(T)
@@ -342,15 +348,15 @@ end
342348
filter(f, xs::Tuple) = afoldl((ys, x) -> f(x) ? (ys..., x) : ys, (), xs...)
343349

344350
# use Array for long tuples
345-
filter(f, t::Any16) = Tuple(filter(f, collect(t)))
351+
filter(f, t::Any32) = Tuple(filter(f, collect(t)))
346352

347353
## comparison ##
348354

349355
isequal(t1::Tuple, t2::Tuple) = (length(t1) == length(t2)) && _isequal(t1, t2)
350356
_isequal(t1::Tuple{}, t2::Tuple{}) = true
351357
_isequal(t1::Tuple{Any}, t2::Tuple{Any}) = isequal(t1[1], t2[1])
352358
_isequal(t1::Tuple, t2::Tuple) = isequal(t1[1], t2[1]) && _isequal(tail(t1), tail(t2))
353-
function _isequal(t1::Any16, t2::Any16)
359+
function _isequal(t1::Any32, t2::Any32)
354360
for i = 1:length(t1)
355361
if !isequal(t1[i], t2[i])
356362
return false
@@ -380,7 +386,7 @@ function _eq_missing(t1::Tuple, t2::Tuple)
380386
return _eq_missing(tail(t1), tail(t2))
381387
end
382388
end
383-
function _eq(t1::Any16, t2::Any16)
389+
function _eq(t1::Any32, t2::Any32)
384390
anymissing = false
385391
for i = 1:length(t1)
386392
eq = (t1[i] == t2[i])
@@ -396,7 +402,7 @@ end
396402
const tuplehash_seed = UInt === UInt64 ? 0x77cfa1eef01bca90 : 0xf01bca90
397403
hash(::Tuple{}, h::UInt) = h + tuplehash_seed
398404
hash(t::Tuple, h::UInt) = hash(t[1], hash(tail(t), h))
399-
function hash(t::Any16, h::UInt)
405+
function hash(t::Any32, h::UInt)
400406
out = h + tuplehash_seed
401407
for i = length(t):-1:1
402408
out = hash(t[i], out)
@@ -417,7 +423,7 @@ function <(t1::Tuple, t2::Tuple)
417423
end
418424
return tail(t1) < tail(t2)
419425
end
420-
function <(t1::Any16, t2::Any16)
426+
function <(t1::Any32, t2::Any32)
421427
n1, n2 = length(t1), length(t2)
422428
for i = 1:min(n1, n2)
423429
a, b = t1[i], t2[i]
@@ -444,7 +450,7 @@ function isless(t1::Tuple, t2::Tuple)
444450
a, b = t1[1], t2[1]
445451
isless(a, b) || (isequal(a, b) && isless(tail(t1), tail(t2)))
446452
end
447-
function isless(t1::Any16, t2::Any16)
453+
function isless(t1::Any32, t2::Any32)
448454
n1, n2 = length(t1), length(t2)
449455
for i = 1:min(n1, n2)
450456
a, b = t1[i], t2[i]

0 commit comments

Comments
 (0)