rand icon indicating copy to clipboard operation
rand copied to clipboard

Find a good design for Sample

Open flyingmutant opened this issue 2 years ago • 0 comments

Sample was added in ce7c277b399941e9dc949893fe405610004dbcc2, however there are several problematic things:

  • as of go1.18, compiler can't allocate the result on the stack, even when Sample was inline and the k is constant (!); see:
    • https://github.com/golang/go/issues/20533
    • https://github.com/golang/go/issues/27625
    • however, changing the signature introduces inconsistency with Perm
  • the map is not reused between different calls
    • however, placing it into Rand increases its size, and introduces a pointer in a previously pointer-free struct
  • Floyd's sampling algorithm does not guarantee random order, so people are required to read the docs and don't forget to Shuffle if required
  • potential PartialShuffle is much faster and guarantees random order (although it requires mutable slice of size n)
  • having all 4 of Perm, Sample, PartialShuffle and Shuffle is 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.

flyingmutant avatar Jul 28 '22 22:07 flyingmutant