Skip to content

perf(random): Choice gathers without allocating per element - #115

Merged
tannevaled merged 2 commits into
mainfrom
perf/choice-gather
Nov 12, 2026
Merged

tannevaled merged 2 commits into
mainfrom
perf/choice-gather

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Generator.Choice, RandomState.Choice and random.Choice allocated twice per drawn element. The gather (takeAxis) sliced reflect.Values for every index, and each Value.Slice allocates. It now copies whole slabs through a byte view of the source and the result: one code path for all 13 dtypes, none of which hold pointers.

Closes #103

Allocations for 100 000 draws from a 1000-element array, from go test -run TestPerf -perf.record (exact under any load; docs/perf.json and docs/perf-table.md are updated):

row before after
random.Generator.Choice 200 013 13
random.RandomState.Choice 200 012 12
random.Choice 200 012 12

NumPy makes one allocation (the result); the 12–13 left are the fixed cost of the index draw and the result array. Timing is deferred: the 1-minute load here was 11, above the harness's limit of 8.

  • TestChoiceAllocsConstant: 100 and 10 000 draws must cost the same number of allocations, along axis 0 of a 1-D array and along axis 1 of a 2-D one. On the old code it fails with 213 vs 20 013 and 2 014 vs 200 014.
  • The values are unchanged: the NumPy golden tests in random/numpy_test.go pass.
  • random stays at 100 % statement coverage.
  • SECURITY.md: an audit-log entry for the unsafe.Slice view.

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 12, 2026 12:09
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-D array

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 8ac94fb into main Nov 12, 2026
13 checks passed
@tannevaled
tannevaled deleted the perf/choice-gather branch November 12, 2026 15:15
tannevaled added a commit that referenced this pull request Nov 15, 2026
…udit entries

docs/linalg.md and docs/design-linalg.md named the ndarray package's own
GEMM; since #122 it is go-blas/blas. SECURITY.md: the go-blas entry is
v0.42.0 (#122, commit 208bdac, tag v0.42.0 contains it); the Choice gather
entry is v0.40.2 (#115, 8ac94fb, first tag containing it, Refs #103).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit that referenced this pull request Nov 15, 2026
… v0.23.0; Round is half to even, add RoundHalfAway (#123)

* perf: float64 Abs, Neg, Square, Floor, Ceil on go-simd/floats v0.23.0 kernels

Bump go-simd/floats to v0.23.0 and route the contiguous float64 case of
Abs, Neg, Square, Floor and Ceil through its SIMD kernels, still split
across cores above kernels.ParThreshold. Results are bit for bit those of
math.Abs, -x, x*x, math.Floor and math.Ceil (a signalling NaN may come
back quieted). Round is unchanged: it rounds half away from zero, which
floats.RoundEven does not.

On 10^5 float64, one core of cfarm420: Abs 244-258 -> 70-75 us, Neg
243-249 -> 72-75, Square 243-247 -> 72-80, Floor 432-457 -> 73-81, Ceil
437-464 -> 72-81 (result allocation included).

Refs #118

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs: GEMM now in go-blas/blas; release tags for the two unreleased audit entries

docs/linalg.md and docs/design-linalg.md named the ndarray package's own
GEMM; since #122 it is go-blas/blas. SECURITY.md: the go-blas entry is
v0.42.0 (#122, commit 208bdac, tag v0.42.0 contains it); the Choice gather
entry is v0.40.2 (#115, 8ac94fb, first tag containing it, Refs #103).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* feat!: Round is half to even (np.round) on the RoundEven kernel; add RoundHalfAway

Round now rounds exact halves to the even neighbour on every dtype
(float64 on the go-simd/floats RoundEven kernel; float32 and each part of
a complex number through math.RoundToEven), as np.round with no decimals.
The old half-away-from-zero behaviour (math.Round) is RoundHalfAway, on
the scalar path, on the same dtypes. Integers and bools are unchanged by
both. There is no decimals variant.

Tests: halves and the edge table for both against math.RoundToEven and
math.Round; Abs and Neg now compared with strict NaN bits, the others
with is-NaN only. On 10^5 float64, cfarm420 one core: Round 325-340 ->
71-80 us; RoundHalfAway (control) 324-345 us.

Refs #118

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs: exempt Array.RoundHalfAway from the perf table (no NumPy counterpart)

TestPerf requires every public function to have a row or an exemption.
RoundHalfAway rounds halves away from zero; np.round is half to even,
which is Array.Round, so there is nothing to time it against.

Refs #118

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

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.

perf: random Choice allocates twice per drawn element (200 013 allocations for 100 000 draws)

1 participant