A start read sessions.json whole onto the heap (24.6 MB on a real
history) and decoded it; then each `named` object was copied again by
gjson.ParseBytes before being read. Nothing decoded keeps a byte of
the file: json.Unmarshal, ccStr and unique.Make all copy, and the keys
are cloned.
On unix the file is now mapped read-only and unmapped once decoded (it
is only replaced by a rename, never written in place); elsewhere it is
read as before. `named` parses its bytes in place.
Run:
- go test ./internal/sessions (mica): ok 63s.
- GOOS=darwin/linux/windows go vet ./internal/sessions: ok.
- Broken by hand: read onto the heap again, TestCacheFileReadWithoutACopy
is red ("allocates 688976 bytes; want it mapped"); a key left
pointing into the file (no Clone) faults it with SIGSEGV after the
unmap. The fixture has plain and escaped names, since an escaped one
is unescaped into a copy anyway.
- Every custom UnmarshalJSON in internal/sessions read for bytes it
keeps: none.
- Real account, mote sandbox HOME, start to 50s (pprof alloc_space):
sessions.loadCache 29.5 MB -> 4.4 MB, os.readFileContents 46.7 MB ->
22.0 MB. Footprint peak unchanged (163 to 164 MB); it is set later.
Not tried: a real Linux start; Windows reads the file as before.
12 lines
235 B
Go
12 lines
235 B
Go
//go:build !unix
|
|
|
|
package sessions
|
|
|
|
import "os"
|
|
|
|
// readCacheFile is the kept parses' file (cache_map_unix.go maps it).
|
|
func readCacheFile(path string) ([]byte, func(), error) {
|
|
b, err := os.ReadFile(path)
|
|
return b, func() {}, err
|
|
}
|