Skip to content

perf(fft): FFTShift/IFFTShift in one pass, written into the result (#118) - #127

Merged
tannevaled merged 3 commits into
mainfrom
perf/fftshift-one-pass
Nov 15, 2026
Merged

tannevaled merged 3 commits into
mainfrom
perf/fftshift-one-pass

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

fft.FFTShift/IFFTShift were 9.7–9.9× slower than NumPy in the single-core table (#118). They rolled one axis at a time: each axis was sliced in two and joined back with Concatenate, a full copy per axis plus Concatenate's own overhead.

Change

  • The roll of every axis is computed at once. An axis given twice is rolled twice, as before and as numpy does.
  • Each output row is written from its input row, and the innermost axis is two copy() calls.
  • The output goes straight into the storage of a ZerosOf result: Data of a contiguous array we own is its storage. The result is therefore allocated once and never copied again.
  • It works for any dtype, through a switch over the 13.

Tests

  • The previous implementation is kept in shift_test.go as the oracle. TestShiftMatchesConcat compares the two over:
    • 0-d to 3-D shapes with even, odd, unit and empty axes;
    • axis subsets, negative and repeated axes included;
    • float64, int16, complex128 and bool, plus a transposed (non-contiguous) input;
    • both directions.
  • A mutation that drops the innermost roll fails it by assertion.
  • TestShiftDoesNotAliasItsInput.
  • The full suite passes.

Measured on cfarm420 (EPYC 7773X): one pinned core, three interleaved rounds, load 2.2–2.4, 256×256 float64, every axis (the table's case).

time memory allocs
before 206–209 µs 2.1 MB 29
after 51–56 µs 0.52 MB 9

The allocation counts change, so perf (refbench CI mode) will ask for a new table run at this head. That run follows in this PR, and the NumPy ratio will come from it.

Refs #118

🤖 Generated with Claude Code

tannevaled and others added 3 commits November 15, 2026 22:05
)

shift rolled one axis at a time by slicing it in two halves and
Concatenate-ing them the other way round: a full copy per axis plus
Concatenate's own overhead (29 allocations, 2.1 MB per 256x256 call).
It now computes the roll of every axis at once (an axis given twice is
rolled twice, as before and as numpy) and writes each output row from
its input row, the innermost axis as two copy() calls, straight into the
storage of a ZerosOf result (Data of a contiguous array we own is its
storage), so the result is allocated once. Any dtype, through a type
switch over the 13.

The previous implementation is kept in shift_test.go as the oracle:
TestShiftMatchesConcat compares the two over 0-d to 3-D shapes (even,
odd, unit and empty axes), axis subsets with negative and repeated axes,
float64/int16/complex128/bool and a transposed (non-contiguous) input,
both directions; a mutation that drops the innermost roll fails it.

cfarm420, one core, three interleaved rounds, load 2.2-2.4, 256x256
float64, every axis:
  before   206-209 us   2.1 MB   29 allocs
  after     51-56 us    0.52 MB   9 allocs

Refs #118

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
FFTShift and IFFTShift shift in one pass (29 -> 9 allocations). The table
is measured again on cfarm420 (one pinned core, NumPy 2.5.4, load 2.62 to
3.99, 256 valid rows): 185 slower, 54 faster, 17 tie. The shifts go from
9.7 and 9.9 to 2.0 times NumPy's time.

docs/perf.md: Status and the causes list are re-read from this table, and
the section on changes since v0.41.0 gains the shift rows and notes the
run-to-run spread (for example SubScalar 4.8 to 7.1 on unchanged code).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The 100% coverage gate found nine uncovered blocks in shift: the dtype
switch had cases only four dtypes reached in the test, and rollAs had an
error branch for ZerosOf that cannot fire (the shape comes from a valid
array). TestShiftMatchesConcat now runs every dtype through its own
branch, against the old implementation; the unreachable branch is
written as the repository writes it elsewhere (ZerosOf(...) // a's
shape is valid). Replayed locally with the CI gate's own command
(go test -race -coverpkg + covblocks): 0 of 5825 blocks uncovered.

Refs #118

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 2ffe02d into main Nov 15, 2026
13 checks passed
@tannevaled
tannevaled deleted the perf/fftshift-one-pass branch November 15, 2026 22:00
tannevaled added a commit to go-ndarray/docs that referenced this pull request Nov 15, 2026
Every mirrored page, hugo.yaml (version and baseURL /docs/0.45/) and the
README now point at v0.45.0. Re-synced from the v0.45.0 source, the only one
that changed since v0.44.0: performance.md (docs/perf.md: the table measured
at 37c1b86; fft.FFTShift/IFFTShift in one pass). The other mirrored pages are
only relabelled. The home page quotes the new counts (185 slower, 54 faster,
17 tied). performance-history.md is left alone.

Refs go-ndarray/ndarray#127

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit to go-ndarray/go-ndarray.github.io that referenced this pull request Nov 15, 2026
The card says "library · v0.45.0". The stats and the measured paragraph quote
the new table, read from docs/perf-table.md at v0.45.0: 54 faster, 17 tied,
185 slower of 256 (Sum 0.80, fft.FFT 0.52, Ravel 0.36, Real 0.58, SqrtInto
0.50; Exp a tie at 0.98). The playground builds against ndarray v0.45.0; its
tests pass.

Refs go-ndarray/ndarray#127

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
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