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.
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package sessions
|
|
|
|
import (
|
|
"bytes"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A cold start collects about once a second, and a sync.Pool is empty two
|
|
// collections later: what the scans give back must still be there.
|
|
func TestLineReaderKeptAcrossCollections(t *testing.T) {
|
|
emptyShelf(&lineShelves[len(lineShelves)-1])
|
|
r, release := lineReader(bytes.NewReader(nil), 1<<20)
|
|
release()
|
|
runtime.GC()
|
|
runtime.GC()
|
|
runtime.GC()
|
|
again, release := lineReader(bytes.NewReader(nil), 1<<20)
|
|
defer release()
|
|
if again != r {
|
|
t.Fatal("a 1 MiB line reader was made again after three collections")
|
|
}
|
|
}
|
|
|
|
func TestLongLineKeptAcrossCollections(t *testing.T) {
|
|
emptyShelf(&longShelf)
|
|
p, b := takeLong(2 << 20)
|
|
giveLong(p, b)
|
|
runtime.GC()
|
|
runtime.GC()
|
|
runtime.GC()
|
|
again, b := takeLong(1 << 20)
|
|
defer giveLong(again, b)
|
|
if again != p {
|
|
t.Fatal("a 2 MiB long-line buffer was made again after three collections")
|
|
}
|
|
}
|
|
|
|
func TestShelfFullKeepsTheLarger(t *testing.T) {
|
|
var s shelf[int]
|
|
for i := 1; i <= shelfHeld; i++ {
|
|
s.give(i, nil)
|
|
}
|
|
if _, kept, _ := s.give(99, nil); kept {
|
|
t.Fatal("a full shelf kept one more")
|
|
}
|
|
out, kept, swapped := s.give(10, func(h, v int) bool { return h < v })
|
|
if !kept || !swapped || out != 1 {
|
|
t.Fatalf("gave 10 to a full shelf of 1..%d: out %d kept %v swapped %v", shelfHeld, out, kept, swapped)
|
|
}
|
|
if v, ok := s.take(func(v int) bool { return v >= 10 }); !ok || v != 10 {
|
|
t.Fatalf("took %d, %v", v, ok)
|
|
}
|
|
}
|
|
|
|
// An idle magpie holds nothing on a shelf.
|
|
func TestShelfLetsGoWhenIdle(t *testing.T) {
|
|
s := shelf[int]{idleFor: 20 * time.Millisecond}
|
|
s.give(1, nil)
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for s.size() > 0 {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("an idle shelf still holds what it was given")
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
// and keeps again afterwards
|
|
s.give(2, nil)
|
|
if v, ok := s.take(nil); !ok || v != 2 {
|
|
t.Fatalf("after letting go: took %d, %v", v, ok)
|
|
}
|
|
}
|
|
|
|
// emptyShelf takes what earlier tests' scans left on s.
|
|
func emptyShelf[T any](s *shelf[T]) {
|
|
for s.size() > 0 {
|
|
s.take(nil)
|
|
}
|
|
}
|