Skip to content

Commit

Permalink
brain/kvbrain: use efficient sequence sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Aug 8, 2024
1 parent 37ce5e9 commit b88158e
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions brain/kvbrain/speak.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func (br *Brain) next(b []byte, prompt []string, opts badger.IteratorOptions) ([
// smaller contexts.
var (
key []byte
m uint64
skip brain.Skip
picked int
n uint64
)
b = appendPrefix(b, prompt)
if len(prompt) == 0 {
Expand All @@ -71,19 +72,15 @@ func (br *Brain) next(b []byte, prompt []string, opts badger.IteratorOptions) ([
defer it.Close()
it.Seek(b)
for it.ValidForPrefix(b) {
// We generate a uniform variate per key, then choose the key
// that gets the maximum variate.
// TODO(zeph): gumbel distribution
u := rand.Uint64()
if m <= u {
if n == 0 {
item := it.Item()
// TODO(zeph): for #43, check deleted uuids so we never
// pick a message that has been deleted
key = item.KeyCopy(key[:0])
m = u
picked++
n = skip.N(rand.Uint64(), rand.Uint64())
}
it.Next()
n--
}
return nil
})
Expand Down

0 comments on commit b88158e

Please sign in to comment.