Skip to content

perf: narrow integer sums widen as they read, without an int64 copy - #52

Merged
tannevaled merged 1 commit into
mainfrom
int-sums-widen
Oct 18, 2026
Merged

tannevaled merged 1 commit into
mainfrom
int-sums-widen

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

The problem. A sum or product of bools or integers narrower than 64 bits accumulates in int64/uint64, as NumPy does. reduceTyped got there by first converting the whole array to the accumulator type: an extra pass plus an allocation of 8 bytes per element.

The fix. Along a contiguous axis, each element is now widened as it is read (foldWidened). The arithmetic is unchanged, wrap-around included. Non-contiguous views gather in the narrow dtype first.

Measured on POWER9 (cfarm29): one pinned thread, host 2–3% busy, six runs each with old and new binaries alternating, benchstat p = 0.002:

Sum before after
int32, n = 2²⁰ 2.872 ms 1.548 ms −46%
int32, n = 2¹⁴ 61.4 µs 30.3 µs −51%
int64 / float32 / float64 (controls) ~

Tested. TestWidenedSums covers int32, uint16, bool and the other narrow dtypes, for sums and products. It includes sums past 2³² and products that wrap, and a transposed view. The expectation widens each element first and then folds with Go's arithmetic. A deliberately sabotaged fold, skipping one element, fails the test 16 times. NumPy's own values.json checks (sum1, prod1 for every dtype) pass. Coverage is 100%, and the tests compile for 386 and arm.

docs/perf.md is updated. The one item left in that section is a float32 SIMD kernel, which comes in a separate PR.

🤖 Generated with Claude Code

A sum or product of bools or integers narrower than 64 bits accumulates
in int64/uint64, as NumPy does, and reduceTyped got there by converting
the whole array first: an extra pass and 8 bytes an element. Along a
contiguous axis each element is now widened as it is read
(foldWidened); the arithmetic, wrap-around included, is unchanged.

POWER9, 1 thread, 6 interleaved runs, benchstat p=0.002: int32 Sum of
2^20 2.872 -> 1.548 ms (-46%), 2^14 -51%; float64, float32 and int64 ~
(controls). TestWidenedSums checks every narrow dtype, sums and
products, wrap-around and a transposed view against widen-then-fold; a
sabotaged fold (one element skipped) fails it 16 times. Coverage 100%,
compiles for 386 and arm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 8bd10b2 into main Oct 18, 2026
12 checks passed
@tannevaled
tannevaled deleted the int-sums-widen branch October 18, 2026 07:50
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