The line readers, long-line buffers and zstd decoders scans use were kept
only in sync.Pools, and a pool lets what it holds go at every second
collection. A cold start collects about once a second (155 GCs in its
first 2 minutes), so the pools were mostly empty when asked. A probe
build counted, in those 2 minutes: 611 new 1 MiB line readers, 205
long-line buffers taken from an empty pool (114 more from one too small,
833 MB in all) and 281 new zstd decoders (~3 MiB each).
A shelf keeps up to 4 of each (per line-reader size class; long lines up
to 4 MiB, longer ones still go to the pool), takes before the pool and
gives back before it, and lets everything go once nothing was taken or
given back for 10 s, so an idle magpie holds none of it. A full long-line
shelf keeps the larger buffer.
Run:
- TestLineReaderKeptAcrossCollections, TestLongLineKeptAcrossCollections
(new): with the shelves' takes removed both are red ("a 1 MiB line
reader was made again after three collections", "a 2 MiB long-line
buffer ..."); green now. TestShelfFullKeepsTheLarger,
TestShelfLetsGoWhenIdle (new, waits for the state, 10 s bar).
- go test ./internal/sessions/ ok; -race ok (the first try raced on a
test writing the idle time as a global under a live timer; it is a
field of the shelf now); -race -count=20 on the new tests ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ok.
- Cold start in a sandbox HOME (startheap-home, calls and sessions
caches removed), serve --web, run until the caches stop growing; both
wrote the same 6,437 files, 70,069,451 bytes. With this and the two
commits before (objectEnd, prefixHash), two runs each, alternating:
old: 12.57 / 11.92 GB allocated, 145 / 149 GCs, CPU 2:01 / 1:55,
settled at 213 / 192 s
new: 7.38 / 7.23 GB allocated, 85 / 87 GCs, CPU 1:52 / 1:50,
settled at 191 / 161 s
RSS at the end is unchanged (714-748 MB): the peak is the warm-up
listing holding every row, not these buffers.
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package sessions
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// shelfIdle is how long a shelf keeps what it holds after its last use.
|
|
const shelfIdle = 10 * time.Second
|
|
|
|
// shelfHeld is the most a shelf holds, which bounds it: 4 MiB of 1 MiB line
|
|
// readers, 16 MiB of long lines, 4 zstd decoders (~3 MiB each).
|
|
const shelfHeld = 4
|
|
|
|
// shelf keeps up to shelfHeld buffers (or decoders) given back, for the next
|
|
// scan to take. A sync.Pool lets what it holds go at every second garbage
|
|
// collection, and a cold start collects about once a second: in its first
|
|
// 2 minutes (155 GCs) the pools were empty for 611 of the 1 MiB line
|
|
// readers, 205 long-line buffers and 281 zstd decoders scans asked for,
|
|
// ~1.9 GB allocated again. A shelf keeps them across collections while
|
|
// scans run, and lets them all go once none was taken or given back for
|
|
// shelfIdle, so an idle magpie holds none.
|
|
type shelf[T any] struct {
|
|
idleFor time.Duration // shelfIdle when 0; set by tests
|
|
|
|
mu sync.Mutex
|
|
held []T
|
|
used time.Time
|
|
timer *time.Timer
|
|
}
|
|
|
|
// take is the first held thing ok accepts, if any.
|
|
func (s *shelf[T]) take(ok func(T) bool) (T, bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.used = time.Now()
|
|
for i, v := range s.held {
|
|
if ok == nil || ok(v) {
|
|
s.held = append(s.held[:i], s.held[i+1:]...)
|
|
return v, true
|
|
}
|
|
}
|
|
var zero T
|
|
return zero, false
|
|
}
|
|
|
|
// give keeps v, in place of the first held thing worse says v beats when
|
|
// the shelf is full. It says false when v isn't kept (the shelf is full
|
|
// and v beats none), and gives back what v took the place of, if any.
|
|
func (s *shelf[T]) give(v T, worse func(held, v T) bool) (out T, kept, swapped bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.used = time.Now()
|
|
if s.timer == nil {
|
|
s.timer = time.AfterFunc(s.wait(), s.idle)
|
|
}
|
|
if len(s.held) < shelfHeld {
|
|
s.held = append(s.held, v)
|
|
return out, true, false
|
|
}
|
|
for i, h := range s.held {
|
|
if worse != nil && worse(h, v) {
|
|
s.held[i] = v
|
|
return h, true, true
|
|
}
|
|
}
|
|
return out, false, false
|
|
}
|
|
|
|
// idle lets go of everything held once the shelf has gone unused for
|
|
// shelfIdle, or checks again when it was used since.
|
|
func (s *shelf[T]) idle() {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if left := s.wait() - time.Since(s.used); left > 0 {
|
|
s.timer.Reset(left)
|
|
return
|
|
}
|
|
clear(s.held)
|
|
s.held = s.held[:0]
|
|
s.timer = nil
|
|
}
|
|
|
|
func (s *shelf[T]) wait() time.Duration {
|
|
if s.idleFor > 0 {
|
|
return s.idleFor
|
|
}
|
|
return shelfIdle
|
|
}
|
|
|
|
// size is how many things the shelf holds.
|
|
func (s *shelf[T]) size() int {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return len(s.held)
|
|
}
|