Skip to content

Commit f5ada17

Browse files
committed
Replace sequence_merge O(log N) recursion with O(1) fold expression
Use operator| with fold expression (Seqs{} | ...) to merge sequences in O(1) template depth instead of O(log N) binary tree recursion. - Reduces sequence_merge instantiations from 449 to 167 (63% reduction) - Total template instantiations: 47,186 → 46,974 (-212) - ADL finds operator| since Sequence is in ck namespace
1 parent 003d995 commit f5ada17

1 file changed

Lines changed: 9 additions & 46 deletions

File tree

include/ck/utility/sequence.hpp

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -199,57 +199,20 @@ template <index_t N>
199199
using make_index_sequence =
200200
typename __make_integer_seq<impl::__integer_sequence, index_t, N>::seq_type;
201201

202-
// merge sequence - optimized to avoid recursive instantiation
203-
namespace detail {
204-
205-
// Helper to concatenate multiple sequences in one step using fold expression
206-
template <typename... Seqs>
207-
struct sequence_merge_impl;
208-
209-
// Base case: single sequence
210-
template <index_t... Is>
211-
struct sequence_merge_impl<Sequence<Is...>>
212-
{
213-
using type = Sequence<Is...>;
214-
};
215-
216-
// Two sequences: direct concatenation
217-
template <index_t... Xs, index_t... Ys>
218-
struct sequence_merge_impl<Sequence<Xs...>, Sequence<Ys...>>
219-
{
220-
using type = Sequence<Xs..., Ys...>;
221-
};
222-
223-
// Three sequences: direct concatenation (avoids one level of recursion)
224-
template <index_t... Xs, index_t... Ys, index_t... Zs>
225-
struct sequence_merge_impl<Sequence<Xs...>, Sequence<Ys...>, Sequence<Zs...>>
226-
{
227-
using type = Sequence<Xs..., Ys..., Zs...>;
228-
};
229-
230-
// Four sequences: direct concatenation
231-
template <index_t... As, index_t... Bs, index_t... Cs, index_t... Ds>
232-
struct sequence_merge_impl<Sequence<As...>, Sequence<Bs...>, Sequence<Cs...>, Sequence<Ds...>>
202+
// merge sequence - O(1) template depth using fold expression
203+
// Binary merge operator for fold expression - enables O(1) depth via (S1 | S2 | S3 | ...)
204+
// Must be in ck namespace for ADL to find it when used with Sequence types
205+
template <index_t... As, index_t... Bs>
206+
constexpr Sequence<As..., Bs...> operator|(Sequence<As...>, Sequence<Bs...>)
233207
{
234-
using type = Sequence<As..., Bs..., Cs..., Ds...>;
235-
};
236-
237-
// General case: binary tree reduction (O(log N) depth instead of O(N))
238-
template <typename S1, typename S2, typename S3, typename S4, typename... Rest>
239-
struct sequence_merge_impl<S1, S2, S3, S4, Rest...>
240-
{
241-
// Merge pairs first, then recurse
242-
using left = typename sequence_merge_impl<S1, S2>::type;
243-
using right = typename sequence_merge_impl<S3, S4, Rest...>::type;
244-
using type = typename sequence_merge_impl<left, right>::type;
245-
};
246-
247-
} // namespace detail
208+
return {};
209+
}
248210

249211
template <typename... Seqs>
250212
struct sequence_merge
251213
{
252-
using type = typename detail::sequence_merge_impl<Seqs...>::type;
214+
// Left fold: ((S1 | S2) | S3) | ... - O(1) template depth
215+
using type = decltype((Seqs{} | ...));
253216
};
254217

255218
template <>

0 commit comments

Comments
 (0)