filememo: a file not there is, while a request holds what was read, not looked for again

A provider with no live model list fetched has no live-models file, and
each request asked after it for each of its models (Provider.Exposed ->
ListedAPIs -> catalog.LiveByID -> readLive), each time an os.Stat that
failed: in a CPU profile of a 30-provider, 60-model catalog, os.Stat was
32% of a request, nearly all of it this. A file found unchanged was
already taken as unchanged for heldFor (1s) within a hold; a file found
missing now is too, in the same generation of Forget. Forget (a write
magpie makes), Look(path) (another's write), the hold's end, a second
gone by, or a stat finding the file there drops it. Unheld, every read
still looks.

Numbers, BenchmarkGroupOverhead, 200KB body, stream=false, 100x each,
binaries alternated twice (Apple M4 Pro, loaded by other sessions):
                       before (cpu-ms/op, allocs)  after
  30x60  p0/m1         34.69 / 35.14   95126        32.87 / 31.84  89266
  30x60  group/g4      44.33 / 43.88  115303        40.77 / 40.77 109071
  12x20  p0/m1         15.75 / 16.30   42535        16.22 / 15.09  41645
  12x20  group/g4      18.15 / 18.50   54138        18.16 / 17.76  53118
(about 7-8% of a request's CPU on the larger catalog; within noise on
the small one, which has fewer missing files.)

Test: TestHoldMissing: 100 held reads of a missing file make one stat; a
file written meanwhile isn't seen until Forget, and is seen after Forget,
after Look and after the release; unheld, 3 reads make 3 stats. Broken by
hand (the missing check disabled): red, "100 looks at a file not there,
held".

Run: go test ./internal/filememo ./internal/catalog: ok. The full suite
run is in the last commit of this series.
This commit is contained in:
yetone
2026-10-19 15:19:31 +08:00
parent a20d86900d
commit e1ebd173e8
2 changed files with 84 additions and 0 deletions
+29
View File
@@ -44,8 +44,23 @@ var (
// on again (steady.Stat): one there before and not now may be being
// replaced (Windows), and is
gone = map[string]bool{}
// missing is the files found not there while a request held what was
// read, when, and in which of Forget's generations: taken as still
// not there as a file found unchanged is, not looked at again. A
// provider with no list fetched has no file of one, and a request
// asked after it for each of its models, each time a look at the disk
// (a third of a request's CPU with 30 providers of 60 models).
missing = map[string]missingFile{}
// stats counts the looks at the disk, for tests
stats int
)
type missingFile struct {
err error
checked time.Time
gen uint64
}
// heldFor is how long a file found unchanged is taken as unchanged, while
// a request holds what was read, before it is looked at again.
const heldFor = time.Second
@@ -86,6 +101,7 @@ func Look(path string) {
seen[k] = e
}
}
delete(missing, path)
}
// Read is parse of the file at path, from what it gave last time when the
@@ -101,10 +117,21 @@ func Read[T any](kind, path string, parse func([]byte) (T, error)) (T, error) {
mu.Unlock()
return e.v.(T), nil
}
if m, ok := missing[path]; ok && holds > 0 && m.gen == gen && time.Since(m.checked) < heldFor {
mu.Unlock()
return zero, m.err
}
g := gen
mu.Unlock()
fi, err := statFile(path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
mu.Lock()
if holds > 0 && g == gen {
missing[path] = missingFile{err, time.Now(), g}
}
mu.Unlock()
}
return zero, err
}
if ok && e.mod.Equal(fi.ModTime()) && e.size == fi.Size() && e.raw == nil {
@@ -153,6 +180,7 @@ func Read[T any](kind, path string, parse func([]byte) (T, error)) (T, error) {
func statFile(path string) (os.FileInfo, error) {
mu.Lock()
wasGone := gone[path]
stats++
mu.Unlock()
var fi os.FileInfo
var err error
@@ -166,6 +194,7 @@ func statFile(path string) (os.FileInfo, error) {
gone[path] = true
} else {
delete(gone, path)
delete(missing, path)
}
mu.Unlock()
return fi, err
+55
View File
@@ -96,3 +96,58 @@ func TestHold(t *testing.T) {
t.Fatalf("a file written just now: %q", v)
}
}
// A file not there is, while a request holds what was read, taken as
// still not there as a file found unchanged is: a provider with no list
// fetched was a look at the disk for each of its models, each request. A
// write magpie makes (Forget), another's (Look), or the hold's end has it
// looked at again; unheld, every read looks.
func TestHoldMissing(t *testing.T) {
p := filepath.Join(t.TempDir(), "none")
parse := func(b []byte) (string, error) { return string(b), nil }
looks := func() int {
mu.Lock()
defer mu.Unlock()
return stats
}
release := Hold()
before := looks()
for range 100 {
if _, err := Read("missing", p, parse); !os.IsNotExist(err) {
t.Fatalf("a file not there: %v", err)
}
}
if n := looks() - before; n != 1 {
t.Fatalf("%d looks at a file not there, held", n)
}
os.WriteFile(p, []byte("a"), 0o600)
if _, err := Read("missing", p, parse); !os.IsNotExist(err) {
t.Fatalf("looked at again while held: %v", err)
}
Forget()
if v, err := Read("missing", p, parse); v != "a" || err != nil {
t.Fatalf("after a write magpie made: %q %v", v, err)
}
os.Remove(p)
Read("missing", p, parse)
os.WriteFile(p, []byte("b"), 0o600)
Look(p)
if v, err := Read("missing", p, parse); v != "b" || err != nil {
t.Fatalf("after another's write: %q %v", v, err)
}
os.Remove(p)
Read("missing", p, parse)
release()
os.WriteFile(p, []byte("c"), 0o600)
if v, err := Read("missing", p, parse); v != "c" || err != nil {
t.Fatalf("released: %q %v", v, err)
}
os.Remove(p)
before = looks()
for range 3 {
Read("missing", p, parse)
}
if n := looks() - before; n != 3 {
t.Fatalf("unheld, %d looks for 3 reads", n)
}
}