Repository navigation
perf: narrow integer sums widen as they read, without an int64 copy - #52
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem. A sum or product of bools or integers narrower than 64 bits accumulates in int64/uint64, as NumPy does.
reduceTypedgot 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,
benchstatp = 0.002:SumTested.
TestWidenedSumscovers 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 ownvalues.jsonchecks (sum1,prod1for every dtype) pass. Coverage is 100%, and the tests compile for 386 and arm.docs/perf.mdis updated. The one item left in that section is a float32 SIMD kernel, which comes in a separate PR.🤖 Generated with Claude Code