Skip to content

feat: add numc_array_stack with tests and benchmark#2

Merged
rizukirr merged 2 commits into
mainfrom
feat/array-stack
Jun 16, 2026
Merged

feat: add numc_array_stack with tests and benchmark#2
rizukirr merged 2 commits into
mainfrom
feat/array-stack

Conversation

@rizukirr

Copy link
Copy Markdown
Owner

Summary

Adds numc_array_stack, which combines several equally shaped arrays along a brand new axis, producing an output of rank+1. The new axis (size n) is inserted at the requested position, so axis=0 lines up whole arrays while the last axis interleaves elements. This is the shape manipulation op described in ROADMAP.md, the close cousin of numc_array_concat.

Bug fixes

The initial implementation always returned -1. Three bugs were fixed:

  1. Inverted dtype check, it errored when the dtypes matched, which rejected every valid call.
  2. Out shape validation loop tested the wrong index variable, an out of bounds read.
  3. The copy block was nested inside the per array validation loop, so the output was written n times.

Tests

tests/core/test_stack.c, 15 cases covering positive, negative, and edge scenarios:

  • Positive: axis 0, middle axis, last axis, 1D inputs.
  • Negative: NULL args, n == 0, dim mismatch, shape mismatch, dtype mismatch, bad axis, wrong out shape.
  • Edge: single array, three way stack, non contiguous sliced views, full 10 dtype sweep.

All 15 pass, and the full suite stays green at 50/50.

Benchmark

bench/numc/bench_stack.c measures the fast path (axis=0, large memcpy) versus the interleaved path (last axis, per element copies), plus array count, dtype, and size scaling. The interleaved path runs roughly 12 to 40 times slower because copy granularity drops to a single element, a candidate for a future strided kernel.

rizukirr added 2 commits June 16, 2026 22:23
Stack multiple equally shaped arrays along a new axis, producing an
output of rank+1. The new axis (size n) is spliced in at the requested
position, so axis 0 lines up whole arrays while the last axis interleaves
elements.

Fix three bugs in the initial implementation: an inverted dtype check
that rejected every valid call, an out shape validation loop that tested
the wrong index variable, and a copy block nested inside the per array
loop that wrote the output n times.

Add tests/core/test_stack.c covering positive, negative, and edge cases
(15 cases, all passing, full suite green at 50/50), plus
bench/numc/bench_stack.c measuring the fast path, the interleaved path,
array count, dtype, and size scaling.
Wrap an over length comment in array_core.c, the long ASSERT_MSG_CTX
calls in test_stack.c, and the ternary in bench_stack.c to satisfy
clang-format, no behavior change.
@rizukirr rizukirr merged commit 7d964ba into main Jun 16, 2026
9 checks passed
@rizukirr rizukirr deleted the feat/array-stack branch June 16, 2026 16:14
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.

1 participant