Skip to content

Commit 037f40d

Browse files
committed
Make _varoffset_cumsum fast for large tuples
1 parent 2c28980 commit 037f40d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/named_tuple_shape.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
# This file is a part of ValueShapes.jl, licensed under the MIT License (MIT).
22

33

4-
@inline _varoffset_cumsum_impl(s, x, y, rest...) = (s, _varoffset_cumsum_impl(s+x, y, rest...)...)
5-
@inline _varoffset_cumsum_impl(s,x) = (s,)
6-
@inline _varoffset_cumsum_impl(s) = ()
7-
@inline _varoffset_cumsum(x::Tuple) = _varoffset_cumsum_impl(0, x...)
4+
function _varoffset_cumsum(x::Tuple{Vararg{Integer,N}}) where N
5+
if @generated
6+
if N < 1000
7+
vars = [Symbol("s$i") for i in 0:N-1]
8+
exprs = [:($(vars[i+1]) = $(vars[i]) + x[$i]) for i in 1:N-1]
9+
quote
10+
s0 = 0
11+
$(exprs...)
12+
($(vars...),)
13+
end
14+
else
15+
:((0, cumsum([x...][begin:end-1])...))
16+
end
17+
else
18+
(0, cumsum([x...][begin:end-1])...)
19+
end
20+
end
821

922

1023
"""

0 commit comments

Comments
 (0)