Repository navigation
perf(random): Choice gathers without allocating per element - #115
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-D array 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.
Generator.Choice,RandomState.Choiceandrandom.Choiceallocated twice per drawn element. The gather (takeAxis) slicedreflect.Values for every index, and eachValue.Sliceallocates. 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.jsonanddocs/perf-table.mdare updated):random.Generator.Choicerandom.RandomState.Choicerandom.ChoiceNumPy 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.random/numpy_test.gopass.randomstays at 100 % statement coverage.unsafe.Sliceview.🤖 Generated with Claude Code