Skip to content

feat: Ravel and Real return views, as NumPy does (#118) - #125

Merged
tannevaled merged 5 commits into
mainfrom
feat/ravel-real-views
Nov 15, 2026
Merged

tannevaled merged 5 commits into
mainfrom
feat/ravel-real-views

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Ravel and Real return views, as NumPy does. Both used to copy, which made their #118 rows 566× slower than NumPy.

Behaviour change, decided by the maintainer on 2026-11-15: views as in NumPy, with Flatten/Copy documented as the way to get an independent array.

  • Ravel of a contiguous array shares its storage (numpy.ravel). A non-contiguous array is still copied.
  • Real of a non-complex array is the array itself (numpy.real): same layout, and the weak-scalar flag is kept. Real of a complex array is still a new float array.
  • A Set through the result now changes the original. The README states this for the next MINOR release.
  • Flatten keeps copying (numpy.flatten). It used to delegate to Ravel for dtypes other than float64; the existing TestTypedStructural caught that, and it now copies explicitly.

Internal callers, checked:

  • the reductions (argTyped, scanTyped, reduceTyped), Outer and linalg's MultiDot only read the view;
  • fft's x.Real().AsType(...) stays a private copy, because AsType clones even when the dtype is unchanged.

Measured on cfarm420 (EPYC 7773X): one pinned core, GOMAXPROCS=1, three interleaved rounds, load 2.1–2.3, 10⁵ float64.

before after memory per call
Ravel (contiguous) 83–90 µs 93–101 ns 803 kB → 128 B
Real (float64) 84–91 µs 101–104 ns 803 kB → 128 B

The table's NumPy times were 176 ns and 164 ns. That is not a new measurement against NumPy, and docs/perf.md says so.

Tests

  • TestRavelIsAViewWhenContiguous covers float64 and int8 aliasing, the transposed copy, and Flatten's independence.
  • TestRealIsAViewOfARealArray covers a transposed view, complex independence and the weak flag.
  • The full suite passes.

Refs #118

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 15, 2026 20:20
Ravel of a contiguous array now shares its storage (numpy.ravel), and
Real of a non-complex array is the array itself (numpy.real). Both used
to copy, which made their #118 rows 566x slower than NumPy. A Set
through the result now changes the original; Ravel of a non-contiguous
array still copies, Real of a complex array is still a new float array,
and Flatten keeps copying (numpy.flatten) - it no longer delegates to
Ravel for other dtypes, which the existing TestTypedStructural caught.

Internal callers checked: the reductions, Outer and linalg's power only
read the view; fft's Real().AsType() clones (AsType copies even when the
dtype is unchanged).

cfarm420, one core, 3 interleaved rounds, load 2.1-2.3, 10^5 float64:
  Ravel  83-90 us -> 93-101 ns   (803 kB -> 128 B per call)
  Real   84-91 us -> 101-104 ns

Decided by the user, 2026-11-15: views as NumPy, with Flatten/Copy
documented as the way to get an independent array.

Refs #118

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of #125: isContiguous refuses any offset, so Ravel of a
contiguous slice of a larger array (p.Slice(Rng(1,4,1))) still copied,
where numpy.ravel returns a view of a C-contiguous slice. The new
isRowMajor asks only that the elements be one row-major run from
a.offset; Ravel views that run, and isContiguous is isRowMajor plus
offset 0 and full storage. A strided slice still copies. Tests cover
both.

Refs #118

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tannevaled and others added 3 commits November 15, 2026 20:47
Ravel and Real return views (4 -> 3 allocations), and the fft real-input
transforms, which call x.Real(), save one allocation each. The table is
measured again on cfarm420 (one pinned core, NumPy 2.5.4, load 3.01 to
3.86, 256 valid rows): 186 slower, 51 faster, 19 tie (v0.41.0: 188, 54, 14).

docs/perf.md: the Status section describes this run, and the section on
changes since v0.41.0 now states the ratios against that table, with the
run-to-run spread of rows whose code did not change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The cause list in docs/perf.md still quoted the v0.41.0 ratios (Add 3.1,
AddInto 1.03, Ceil 20, Abs 12, SqrtInto 0.51, ...). Re-read each from the
table measured at c198892, and name the unary rows that now run on kernels.
README: the allocating forms are 3.4-3.8x slower, the *Into forms within 7%.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
isRowMajor had taken isContiguous's doc comment, leaving isContiguous with
none. docs/perf.md called Power binary, but it takes a scalar exponent.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit f0b972d into main Nov 15, 2026
13 checks passed
@tannevaled
tannevaled deleted the feat/ravel-real-views branch November 15, 2026 20:18
tannevaled added a commit to go-ndarray/go-ndarray.github.io that referenced this pull request Nov 15, 2026
The card says "library · v0.44.0". The stats and the measured paragraph quote
the new table (51 faster, 19 tied, 186 slower of 256; Sum 0.76, fft.FFT 0.64,
Ravel 0.35, Real 0.57, SqrtInto 0.52; Exp is now a tie at 0.92), read from
docs/perf-table.md at v0.44.0. The views family says Ravel is a view when the
elements are one row-major run. The playground builds against ndarray v0.44.0;
its tests pass.

Refs go-ndarray/ndarray#125

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.44/) and the
README now point at v0.44.0. Re-synced from the v0.44.0 sources, which changed
since v0.43.1: performance.md (docs/perf.md: the table measured at c198892,
"Since the v0.41.0 table"). The other mirrored sources (roadmap, dtypes,
linalg, random, security, and the accuracy section of the README) are
unchanged and only relabelled. The home page quotes the new counts (186
slower, 51 faster, 19 tied) and says Ravel is a view when the elements are one
row-major run. performance-history.md is left alone.

Refs go-ndarray/ndarray#125

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.44.0". The stats and the measured paragraph quote
the new table (51 faster, 19 tied, 186 slower of 256; Sum 0.76, fft.FFT 0.64,
Ravel 0.35, Real 0.57, SqrtInto 0.52; Exp is now a tie at 0.92), read from
docs/perf-table.md at v0.44.0. The views family says Ravel is a view when the
elements are one row-major run. The playground builds against ndarray v0.44.0;
its tests pass.

Refs go-ndarray/ndarray#125

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