Files
magpie/internal/library/linkmemo.go
T
yetone 6d7e5b53a0 library: the Library page opens without looking at the same folders thousands of times (#1771, oxyuan)
oxyuan's profile after v0.1.1214: every page opens at once but the
Library, ~1.3s a visit on Windows, 68% of it in library.resolve and the
read 97% system calls.

Cause: resolve follows links a part at a time, and for each part it
called linkTarget (an Lstat, and a Readlink for a link) and then Lstat'd
the same part again. One read of the page resolves a few hundred paths,
most of them many times over (each agent's skills folder for each skill,
CC Switch's folder and the source for each skill, home and the shared
folder once per skill), so the same ~150 folders were looked at
~10,000 times a read. On Windows each look is a cgocall.

Fix: while a read of the page (Read, Glance) holds the library's lock,
lookPart (linkmemo.go) keeps what each entry was found to be, one Lstat
and a Readlink for a link, for the rest of that read; linkTarget and
resolve both go through it, so the second Lstat is gone too. No change
runs while the read holds mu, so nothing the read keeps can be changed
by magpie under it. Nothing is kept past the read: the next visit looks
again, so a link made or moved on disk between visits shows. A look that
failed for another reason than "not there" is unknown and isn't kept. I
looked at a cache across visits keyed on the folders' times; checking it
costs one stat per folder on the way, the same as the memo's single look,
so it would save nothing over this.

Numbers, 11 agents, 40 skills from a linked folder given to all of them,
10 MCP servers (the package's sandbox, test binary built for each):
  Windows 11 x64 box, median of 7:
    Glance  10,144 looks, 116ms -> 117 looks, 26ms
    Read    10,473 looks, 150ms ->  284 looks, 61ms
  macOS (arm64), median of 7:
    Glance  11,660 looks, 41ms -> 159 looks, 13ms
    Read    12,040 looks, 53ms -> 360 looks, 28ms
The rest of a full Read (comparing the agents' copies, found skills)
runs without the lock, as before, and isn't memoized.

Tests: TestReadLooksAtEachFolderOnce (red with the memo taken out, and
with it outliving the read), TestLinkChangedBetweenVisitsShows (red with
a memo kept across visits), TestUnreadablePartIsLookedAtAgain (red when
an unknown look is kept).

Run: go test -tags nogui ./internal/library ./internal/gui (ok);
gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/library; node --test internal/gui/tests/library-*.test.cjs,
chromium and webkit: 450/450 on the second full run. The first full run
had library-mcp-tool-switch red in WebKit (#1609, filed) and
library-chip-fan red once in Chromium under --test-concurrency=4; it
passed 3/3 alone and in the second full run. Not tried on oxyuan's
machine or with their library.
2026-10-13 11:10:59 +08:00

92 lines
2.7 KiB
Go

package library
// Reading the Library page follows the links on the way to a few hundred
// paths: each agent's instructions, MCP file and skills folder, each skill
// and where it came from, CC Switch's folder, the shared skills folder,
// most of them many times over (once for each skill, or each pair of
// agents). resolve looks at a path a part at a time, so one read of the
// page looked at the same few hundred folders thousands of times, two or
// three system calls each; on Windows that alone was over a second on
// every visit (oxyuan, #1771). While a read holds the library's lock,
// what each part was found to be is kept for the rest of that read, so
// each folder is looked at once. Nothing is kept past the read: the next
// visit looks again, so a link made or changed on disk in between shows.
import (
"errors"
"io/fs"
"os"
"sync"
"sync/atomic"
)
// part is what one entry on the way to a path is: there or not, and
// where it points when it is a link (a symlink, or on Windows a junction).
type part struct {
there, link bool
to string
}
type partMemo struct {
sync.Mutex
m map[string]part
}
// reading is the memo of the read in progress, nil when there is none.
var reading atomic.Pointer[partMemo]
// readParts keeps what lookPart finds until the returned func is called.
// Only a read of the page that holds mu calls it: no change can make or
// take away a link while it does, since every change holds mu too. A
// caller without mu meanwhile (a probe of a folder to add) is given what
// the read found, at most as old as the read.
func readParts() (done func()) {
reading.Store(&partMemo{m: map[string]part{}})
return func() { reading.Store(nil) }
}
// lookPart is what the entry at p is, as found once in this read.
func lookPart(p string) part {
pm := reading.Load()
if pm == nil {
e, _ := lookAt(p)
return e
}
pm.Lock()
e, ok := pm.m[p]
pm.Unlock()
if ok {
return e
}
e, sure := lookAt(p)
if sure {
pm.Lock()
pm.m[p] = e
pm.Unlock()
}
return e
}
// lookAt is statPart; a test counts the looks
var lookAt = statPart
// statPart looks at the entry at p: one Lstat, and a Readlink for a link.
// sure is false when it couldn't be looked at for another reason than
// its not being there: unknown, looked at again next time.
func statPart(p string) (e part, sure bool) {
fi, err := os.Lstat(p)
if err != nil {
return part{}, errors.Is(err, fs.ErrNotExist)
}
if !linkEntry(fi) {
return part{there: true}, true
}
to, err := os.Readlink(p)
if err != nil || to == "" {
// a link that can't be read is taken as an entry of its own, as
// before; but it isn't kept
return part{there: true}, false
}
return part{there: true, link: true, to: to}, true
}