-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Sample was added in ce7c277, however there are several problematic things:
- as of go1.18, compiler can't allocate the result on the stack, even when
Samplewas inline and thekis constant (!); see:- cmd/compile: improve escape analysis of make([]T, n) where n is non-constant golang/go#20533
- cmd/compile: automatically stack-allocate small non-escaping slices of dynamic size golang/go#27625
- however, changing the signature introduces inconsistency with
Perm
- the map is not reused between different calls
- however, placing it into
Randincreases its size, and introduces a pointer in a previously pointer-free struct
- however, placing it into
- Floyd's sampling algorithm does not guarantee random order, so people are required to read the docs and don't forget to
Shuffleif required - potential
PartialShuffleis much faster and guarantees random order (although it requires mutable slice of sizen) - having all 4 of
Perm,Sample,PartialShuffleandShuffleis maybe too much
On the other hand, Sample runs in O(k) for both time and memory. Also, algorithm is simple but non-obvious (a bit like Shuffle), as is the linear-search-instead-of-map optimization.
Also, with #1, Sample may be not required at all.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request