From 8a440e4182f98d20919d3a1f69bc14348bfe63f3 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 12 Nov 2026 12:09:27 +0100 Subject: [PATCH 1/2] perf(random): Choice gathers without allocating per element (#103) Co-Authored-By: Claude Opus 5.5 --- SECURITY.md | 11 +++++++++++ docs/perf-table.md | 10 +++++----- docs/perf.json | 10 +++++----- random/arrays.go | 21 +++++++++++++++++--- random/choice_alloc_test.go | 39 +++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 random/choice_alloc_test.go diff --git a/SECURITY.md b/SECURITY.md index d272a9a..7a18b6c 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -66,6 +66,17 @@ standard library's known vulnerabilities; `govulncheck ./...` reports none. ## Audit log +**2026-11-12 (unreleased, #103).** The gather of `random`'s `Choice` +(`takeAxis`, random/arrays.go) now copies bytes through `bytesOf`, an +`unsafe.Slice` view of a slice's backing array of exactly `Len()·size` bytes. +Every dtype the package stores is a fixed-size value without pointers +(bool, the integers, float32/64, complex64/128), so a byte copy is a valid copy +and the garbage collector has nothing to trace in it. Both views come from +slices the function holds for the whole copy (the source array's storage and +the freshly allocated result), and every copy slices them with Go's bounds +checks. The indices are validated against the axis length before +`takeAxis` is called, as before. An empty slice gives an empty view. + **2026-11-11 (v0.39.0).** go-simd/floats v0.15.0 adds an AVX2 path to `Float32Max`/`Float32Min` on amd64 (four YMM accumulators, from 32 elements, SSE2 kept below that and on CPUs without AVX2+FMA), with both paths run through the diff --git a/docs/perf-table.md b/docs/perf-table.md index 64f6ee8..46f22d9 100644 --- a/docs/perf-table.md +++ b/docs/perf-table.md @@ -2,9 +2,9 @@ -- Date: 2026-11-08T12:33:21+01:00 +- Date: 2026-11-12T12:08:39+01:00 - Commit: -- Go: go1.27.3 +- Go: go1.27.4 - Machine: darwin/arm64, 16 CPUs - Timing: **deferred** (no verdict; allocation counts only) @@ -221,7 +221,7 @@ ns/call, median [first quartile, third quartile]; ratio = Go / reference. | random.Generator.Wald | `random.Generator.Wald` | numpy Generator.wald(1.0, 2.0, 100000) | — | — | — | deferred | none | 6 | | random.Generator.Weibull | `random.Generator.Weibull` | numpy Generator.weibull(1.5, 100000) | — | — | — | deferred | none | 6 | | random.Generator.Zipf | `random.Generator.Zipf` | numpy Generator.zipf(2.5, 100000) | — | — | — | deferred | none | 6 | -| random.Generator.Choice | `random.Generator.Choice` | numpy Generator.choice(x, 100000) | — | — | — | deferred | none | 200013 | +| random.Generator.Choice | `random.Generator.Choice` | numpy Generator.choice(x, 100000) | — | — | — | deferred | none | 13 | | random.Generator.PermutationOf | `random.Generator.PermutationOf` | numpy Generator.permutation(x, axis=0) | — | — | — | deferred | none | 10 | | random.Generator.Permuted | `random.Generator.Permuted` | numpy Generator.permuted(x) | — | — | — | deferred | none | 7 | | random.Generator.PermutedAxis | `random.Generator.PermutedAxis` | numpy Generator.permuted(x, axis=0) | — | — | — | deferred | none | 8 | @@ -249,7 +249,7 @@ ns/call, median [first quartile, third quartile]; ratio = Go / reference. | random.RandomState.StandardGamma | `random.RandomState.StandardGamma` | numpy RandomState.standard_gamma(2.0, 100000) | — | — | — | deferred | none | 6 | | random.RandomState.StandardNormal | `random.RandomState.StandardNormal` | numpy RandomState.standard_normal(100000) | — | — | — | deferred | none | 6 | | random.RandomState.Uniform | `random.RandomState.Uniform` | numpy RandomState.uniform(0.0, 1.0, 100000) | — | — | — | deferred | none | 6 | -| random.RandomState.Choice | `random.RandomState.Choice` | numpy RandomState.choice(x, 100000) | — | — | — | deferred | none | 200012 | +| random.RandomState.Choice | `random.RandomState.Choice` | numpy RandomState.choice(x, 100000) | — | — | — | deferred | none | 12 | | random.RandomState.PermutationOf | `random.RandomState.PermutationOf` | numpy RandomState.permutation(x) | — | — | — | deferred | none | 10 | | random.RandomState.Shuffle | `random.RandomState.Shuffle` | numpy RandomState.shuffle (in place, on a copy) | — | — | — | deferred | none | 10 | | random.RandomState.ShuffleInt64 | `random.RandomState.ShuffleInt64` | numpy RandomState.shuffle of 100000 int64 (in place) | — | — | — | deferred | none | 0 | @@ -265,6 +265,6 @@ ns/call, median [first quartile, third quartile]; ratio = Go / reference. | random.Randn | `random.Randn` | numpy np.random.randn(100000) | — | — | — | deferred | none | 6 | | random.RandomSample | `random.RandomSample` | numpy np.random.random_sample(100000) | — | — | — | deferred | none | 6 | | random.Uniform | `random.Uniform` | numpy np.random.uniform(0.0, 1.0, 100000) | — | — | — | deferred | none | 6 | -| random.Choice | `random.Choice` | numpy np.random.choice(x, 100000) | — | — | — | deferred | none | 200012 | +| random.Choice | `random.Choice` | numpy np.random.choice(x, 100000) | — | — | — | deferred | none | 12 | | random.Shuffle | `random.Shuffle` | numpy np.random.shuffle (in place, on a copy) | — | — | — | deferred | none | 11 | | random.RandomRaw | `random.RandomRaw` | numpy PCG64.random_raw(100000) | — | — | — | deferred | none | 1 | diff --git a/docs/perf.json b/docs/perf.json index c9e61aa..e36f64f 100644 --- a/docs/perf.json +++ b/docs/perf.json @@ -1,10 +1,10 @@ { "schema": 1, "status": "deferred", - "date": "2026-11-08T12:33:21+01:00", + "date": "2026-11-12T12:08:39+01:00", "commit": "", "repo": "go-ndarray/ndarray", - "go_version": "go1.27.3", + "go_version": "go1.27.4", "machine": "darwin/arm64, 16 CPUs", "max_load": 8, "load": { @@ -3781,7 +3781,7 @@ "func": "random.Generator.Choice", "reference": "numpy Generator.choice(x, 100000)", "parity": "none", - "allocs": 200013, + "allocs": 13, "go": { "median": 0, "q1": 0, @@ -4285,7 +4285,7 @@ "func": "random.RandomState.Choice", "reference": "numpy RandomState.choice(x, 100000)", "parity": "none", - "allocs": 200012, + "allocs": 12, "go": { "median": 0, "q1": 0, @@ -4573,7 +4573,7 @@ "func": "random.Choice", "reference": "numpy np.random.choice(x, 100000)", "parity": "none", - "allocs": 200012, + "allocs": 12, "go": { "median": 0, "q1": 0, diff --git a/random/arrays.go b/random/arrays.go index ded88c7..f39c7a0 100644 --- a/random/arrays.go +++ b/random/arrays.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "reflect" + "unsafe" "github.com/go-ndarray/ndarray" ) @@ -136,17 +137,31 @@ func takeAxis(a *ndarray.Array, idx []int64, idxShape []int, axis int) (*ndarray if err != nil { return nil, err } + // The gather runs on the bytes: every dtype is a pointer-free fixed-size + // value, so a slab is a run of inner·size bytes. reflect.Value.Slice cost + // two allocations per index (#103); this costs none. + size := int(src.Type().Elem().Size()) + sb := bytesOf(src, size) + db := bytesOf(dst, size) + slab := inner * size pos := 0 for o := range outer { for _, k := range idx { - from := (o*n + int(k)) * inner - reflect.Copy(dst.Slice(pos, pos+inner), src.Slice(from, from+inner)) - pos += inner + from := (o*n + int(k)) * slab + copy(db[pos:pos+slab], sb[from:from+slab]) + pos += slab } } return r, nil } +// bytesOf views the backing array of the slice s (elements of size bytes) as +// bytes. The caller keeps s alive for as long as the view is used. An empty s +// gives an empty view (unsafe.Slice accepts a nil pointer with length 0). +func bytesOf(s reflect.Value, size int) []byte { + return unsafe.Slice((*byte)(s.UnsafePointer()), s.Len()*size) +} + // swapSlabs swaps positions i and j along the middle axis of the (outer, n, // inner) layout of s. func swapSlabs(swap func(i, j int), outer, n, inner, i, j int) { diff --git a/random/choice_alloc_test.go b/random/choice_alloc_test.go new file mode 100644 index 0000000..3bcf3d2 --- /dev/null +++ b/random/choice_alloc_test.go @@ -0,0 +1,39 @@ +package random + +import ( + "testing" + + "github.com/go-ndarray/ndarray" +) + +// TestChoiceAllocsConstant: the gather of Choice allocates nothing per drawn +// element (#103: it was two per element through reflect.Value.Slice), so 100 +// and 10 000 draws cost the same number of allocations, along axis 0 of a 1-D +// array and along an inner axis of a 2-D one. +func TestChoiceAllocsConstant(t *testing.T) { + a, err := ndarray.Arange(0, 1000, 1) + if err != nil { + t.Fatal(err) + } + m, err := a.Reshape(10, 100) + if err != nil { + t.Fatal(err) + } + for _, c := range []struct { + name string + a *ndarray.Array + axis int + }{{"1-D", a, 0}, {"2-D axis 1", m, 1}} { + allocs := func(size int) float64 { + g := DefaultRNG(1) + return testing.AllocsPerRun(20, func() { + if _, err := g.Choice(c.a, ChoiceOptions{Axis: c.axis}, size); err != nil { + t.Fatal(err) + } + }) + } + if small, large := allocs(100), allocs(10000); large != small { + t.Errorf("%s: %v allocations for 100 draws, %v for 10000: the gather allocates per element", c.name, small, large) + } + } +} From da50ed994da0ebed2d3095b1eec09ca97bd07444 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 12 Nov 2026 15:56:08 +0100 Subject: [PATCH 2/2] test(random): Choice values for every dtype on the middle axis of a 3-D array Co-Authored-By: Claude Opus 5.5 --- random/choice_alloc_test.go | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/random/choice_alloc_test.go b/random/choice_alloc_test.go index 3bcf3d2..129a8fc 100644 --- a/random/choice_alloc_test.go +++ b/random/choice_alloc_test.go @@ -6,6 +6,59 @@ import ( "github.com/go-ndarray/ndarray" ) +// TestChoiceEveryDTypeMiddleAxis pins the values of the byte gather for every +// element size, with outer > 1 and inner > 1 at once (the middle axis of a +// (2, 7, 3) array). The int64 result must be a real gather: element (o, j, i) +// is o·21 + k_j·3 + i for one k_j per j. And the draws do not depend on the +// dtype, so for every dtype Choice(x.AsType(dt)) must equal +// Choice(x).AsType(dt) for the same seed: a slab of the wrong byte size would +// gather the wrong elements. +func TestChoiceEveryDTypeMiddleAxis(t *testing.T) { + x, err := ndarray.Arange(0, 42, 1) + if err != nil { + t.Fatal(err) + } + if x, err = x.AsType(ndarray.Int64).Reshape(2, 7, 3); err != nil { + t.Fatal(err) + } + const size = 5 + ref, err := DefaultRNG(7).Choice(x, ChoiceOptions{Axis: 1}, size) + if err != nil { + t.Fatal(err) + } + got, _ := ndarray.Data[int64](ref) + for j := range size { + k := (got[j*3] - 0) / 3 // from (o=0, j, i=0) + for o := range 2 { + for i := range 3 { + if want := int64(o*21) + k*3 + int64(i); got[(o*size+j)*3+i] != want { + t.Fatalf("int64 [%d %d %d] = %d, want %d (index %d)", o, j, i, got[(o*size+j)*3+i], want, k) + } + } + } + } + for _, dt := range []ndarray.DType{ + ndarray.Bool, ndarray.Int8, ndarray.Int16, ndarray.Int32, ndarray.Int64, + ndarray.Uint8, ndarray.Uint16, ndarray.Uint32, ndarray.Uint64, + ndarray.Float32, ndarray.Float64, ndarray.Complex64, ndarray.Complex128, + } { + r, err := DefaultRNG(7).Choice(x.AsType(dt), ChoiceOptions{Axis: 1}, size) + if err != nil { + t.Fatal(err) + } + if r.DType() != dt { + t.Fatalf("%v: result dtype %v", dt, r.DType()) + } + a, _ := ndarray.Data[complex128](r.AsType(ndarray.Complex128)) + b, _ := ndarray.Data[complex128](ref.AsType(dt).AsType(ndarray.Complex128)) + for i := range b { + if a[i] != b[i] { + t.Fatalf("%v: element %d = %v, want %v", dt, i, a[i], b[i]) + } + } + } +} + // TestChoiceAllocsConstant: the gather of Choice allocates nothing per drawn // element (#103: it was two per element through reflect.Value.Slice), so 100 // and 10 000 draws cost the same number of allocations, along axis 0 of a 1-D