@@ -53,6 +53,14 @@ include("unrollable_iterator_interface.jl")
5353# Analogue of the non-public Base._InitialValue for reduction and accumulation.
5454struct NoInit end
5555
56+ @inline empty_reduction_value (:: NoInit ) =
57+ error (" unrolled_reduce requires an init value for empty iterators" )
58+ @inline empty_reduction_value (init) = init
59+
60+ @inline first_reduction_value (op, itr, :: NoInit ) = generic_getindex (itr, 1 )
61+ @inline first_reduction_value (op, itr, init) =
62+ op (init, generic_getindex (itr, 1 ))
63+
5664# Analogue of ∘, but with only one function argument and guaranteed inlining.
5765# Base's ∘ leads to type instabilities in unit tests on Julia 1.10 and 1.11.
5866@inline ⋅ (f1:: F1 , f2:: F2 ) where {F1, F2} = x -> (@inline f1 (f2 (x)))
@@ -134,17 +142,16 @@ include("StaticBitVector.jl")
134142 unrolled_drop_into (inferred_output_type (itr), itr, val_N)
135143
136144# #
137- # # Functions unrolled using either recursion or generated expressions
145+ # # Functions unrolled using either hard-coded or generated expressions
138146# #
139147
140- include (" recursively_unrolled_functions.jl" )
141- include (" generatively_unrolled_functions.jl" )
148+ include (" manually_unrolled_functions.jl" )
142149
143150# The unrolled_map function could also be implemented in terms of ntuple, but
144151# then it would be subject to the same recursion limit as ntuple. On Julia 1.10,
145152# this leads to type instabilities in several unit tests for nested iterators.
146153@inline unrolled_map_into_tuple (f:: F , itr) where {F} =
147- ( rec_unroll ( itr) ? rec_unrolled_map : gen_unrolled_map)( f, itr)
154+ _unrolled_map ( Val ( length ( itr)), f, itr)
148155@inline unrolled_map_into (output_type, f:: F , itr) where {F} =
149156 constructor_from_tuple (output_type)(unrolled_map_into_tuple (f, itr))
150157@inline unrolled_map (f:: F , itr) where {F} =
@@ -154,32 +161,26 @@ include("generatively_unrolled_functions.jl")
154161
155162@inline unrolled_any (itr) = unrolled_any (identity, itr)
156163@inline unrolled_any (f:: F , itr) where {F} =
157- ( rec_unroll ( itr) ? rec_unrolled_any : gen_unrolled_any)( f, itr)
164+ _unrolled_any ( Val ( length ( itr)), f, itr)
158165
159166@inline unrolled_all (itr) = unrolled_all (identity, itr)
160167@inline unrolled_all (f:: F , itr) where {F} =
161- ( rec_unroll ( itr) ? rec_unrolled_all : gen_unrolled_all)( f, itr)
168+ _unrolled_all ( Val ( length ( itr)), f, itr)
162169
163170@inline unrolled_foreach (f:: F , itr) where {F} =
164- ( rec_unroll ( itr) ? rec_unrolled_foreach : gen_unrolled_foreach)( f, itr)
171+ _unrolled_foreach ( Val ( length ( itr)), f, itr)
165172@inline unrolled_foreach (f, itrs... ) = unrolled_foreach (splat (f), zip (itrs... ))
166173
167174@inline unrolled_reduce (op:: O , itr, init) where {O} =
168- isempty (itr) && init isa NoInit ?
169- error (" unrolled_reduce requires an init value for empty iterators" ) :
170- (rec_unroll (itr) ? rec_unrolled_reduce : gen_unrolled_reduce)(op, itr, init)
175+ _unrolled_reduce (Val (length (itr)), op, itr, init)
171176@inline unrolled_reduce (op:: O , itr; init = NoInit ()) where {O} =
172177 unrolled_reduce (op, itr, init)
173178
174179@inline unrolled_mapreduce (f:: F , op:: O , itrs... ; init = NoInit ()) where {F, O} =
175180 unrolled_reduce (op, unrolled_map (f, itrs... ), init)
176181
177182@inline unrolled_accumulate_into_tuple (op:: O , itr, init) where {O} =
178- (rec_unroll (itr) ? rec_unrolled_accumulate : gen_unrolled_accumulate)(
179- op,
180- itr,
181- init,
182- )
183+ _unrolled_accumulate (Val (length (itr)), op, itr, init)
183184@inline unrolled_accumulate_into (output_type, op:: O , itr, init) where {O} =
184185 constructor_from_tuple (output_type)(
185186 unrolled_accumulate_into_tuple (op, itr, init),
@@ -209,13 +210,7 @@ include("generatively_unrolled_functions.jl")
209210 itr,
210211 itrs... ,
211212) where {F, I, E} =
212- (rec_unroll (itr) ? rec_unrolled_ifelse : gen_unrolled_ifelse)(
213- f,
214- get_if,
215- get_else,
216- itr,
217- itrs... ,
218- )
213+ _unrolled_ifelse (Val (length (itr)), f, get_if, get_else, itr, itrs... )
219214
220215# #
221216# # Unrolled functions without any analogues in Base
0 commit comments