Skip to content

Conversation

@CongMa13
Copy link
Collaborator

Refactor cd sequence_reverse_inclusive_scan from recursive template to constexpr for-loop.

This pull request improves the utility array functionality and its integration with sequence utilities, as well as adds comprehensive unit tests for the Array class. The main changes include refactoring the implementation of reverse inclusive scan for sequences, updating header dependencies, optimizing the Array assignment operator, and introducing a full test suite for Array.

Array and Sequence Utility Improvements:

  • Refactored the implementation of sequence_reverse_inclusive_scan in sequence.hpp to use a more efficient, array-based approach, encapsulated in a new impl namespace. This change simplifies the logic and should improve compile-time performance.
  • Updated header dependencies in both array.hpp and sequence.hpp to remove unused includes and ensure correct dependency order, notably including array.hpp in sequence.hpp to support the new scan implementation. [1] [2]
  • Optimized the assignment operator in the Array struct by replacing the static_for loop with a standard for-loop and adding a pragma for potential unrolling, improving both readability and performance.

Testing Enhancements:

  • Added a new unit test file unit_array.cpp with comprehensive tests covering construction, element access, assignment, iterators, the make_array helper, and different data types for the Array class.
  • Updated the CMakeLists.txt to build and link the new unit_array test executable.

Tracking issue: #3575

Comment on lines 34 to 38
#pragma unroll
for(int i = 0; i < NSize; i++)
{
mData[i] = a[i];
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the dependency on static_for. Hence, only need to include type.hpp.

And the code is easier to read.

@afagaj afagaj requested a review from Copilot January 20, 2026 22:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the sequence_reverse_inclusive_scan implementation from a recursive template-based approach to a more efficient constexpr for-loop using the Array utility class. The changes improve compile-time performance and simplify the logic.

Changes:

  • Refactored sequence_reverse_inclusive_scan to use an array-based iterative approach instead of recursive templates
  • Updated header dependencies to remove circular includes and optimize dependencies
  • Optimized Array::operator= by replacing static_for with a standard for-loop with unroll pragma
  • Added comprehensive unit tests for the Array class

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
include/ck/utility/sequence.hpp Refactored sequence_reverse_inclusive_scan from recursive templates to constexpr array-based implementation
include/ck/utility/array.hpp Updated header dependencies and optimized assignment operator with standard for-loop
test/util/unit_array.cpp Added comprehensive unit tests covering Array construction, access, assignment, iterators, and edge cases
test/util/CMakeLists.txt Added build configuration for new unit_array test executable

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


using namespace ck;

// Test basic Sequence construction and properties
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment mentions 'Sequence' but should refer to 'Array' since this test is for the Array class.

Suggested change
// Test basic Sequence construction and properties
// Test basic Array construction and properties

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be updated.


static_for<0, Size(), 1>{}([&](auto i) { operator()(i) = a[i]; });
#pragma unroll
for(int i = 0; i < NSize; i++)
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop variable uses int type while NSize is of type index_t (typically size_t or similar). For consistency and to avoid potential type conversion issues, use index_t i = 0 instead of int i = 0.

Suggested change
for(int i = 0; i < NSize; i++)
for(index_t i = 0; i < NSize; i++)

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's use index_t for consistency.


static_for<0, Size(), 1>{}([&](auto i) { operator()(i) = a[i]; });
#pragma unroll
for(int i = 0; i < NSize; i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need pragma unroll here. The compiler should optimize correctly.


using type = typename sequence_merge<Sequence<new_reduce>, old_scan>::type;
};
if constexpr(size == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hard code for a few of the shorter sequences:

if constexpr(size == 0)
{
return Sequence<>{};
}
else if constexpr(size == 1)
{
constexpr index_t values[1] = {Is...};
return Sequence<Reduce{}(values[0], Init)>{};
}
else if constexpr(size == 2)
{
constexpr index_t values[2] = {Is...};
constexpr index_t r1 = Reduce{}(values[1], Init);
constexpr index_t r0 = Reduce{}(values[0], r1);
return Sequence<r0, r1>{};
}

}
else
{
constexpr Array<index_t, size> arr = []() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be slightly faster to replace this lambda with a named constexpr function helper.

template <index_t I, index_t... Is, typename Reduce, index_t Init>
struct sequence_reverse_inclusive_scan<Sequence<I, Is...>, Reduce, Init>
template <index_t... Is, typename Reduce, index_t Init>
struct sequence_reverse_inclusive_scan_impl<Sequence<Is...>, Reduce, Init>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have unit tests for the reverse inclusive scan?

Copy link
Collaborator Author

@CongMa13 CongMa13 Jan 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we have.

test/util/unit_sequence.cpp

ReverseInclusiveScan, ReverseExclusiveScan

shumway
shumway previously approved these changes Jan 21, 2026
Copy link
Collaborator

@shumway shumway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. I just have a few small comments.

I think we may need unit tests for the reverse inclusive scan.

@illsilin
Copy link
Collaborator

Hey @CongMa13, please resolve conflicts for the headers, and make sure ci tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants