From 4789df803aba25340ebe0eaf472ad22bd92d7f96 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 17 Nov 2025 00:00:00 +0000 Subject: pack: Use a Go map with a mutex instead of a sync.Map for packfiles Very few writes, you don't typically see more than a dozen packfiles. A ton of reads. Go maps are the obvious choice. --- pack_pack.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'pack_pack.go') diff --git a/pack_pack.go b/pack_pack.go index 5c2c8628..d7b565f5 100644 --- a/pack_pack.go +++ b/pack_pack.go @@ -570,3 +570,27 @@ func (pf *packFile) cursor(ofs uint64) (io.Reader, error) { } return bytes.NewReader(pf.data[ofs:]), nil } + +func (repo *Repository) packFile(rel string) (*packFile, error) { + repo.packFilesMu.RLock() + pf, ok := repo.packFiles[rel] + repo.packFilesMu.RUnlock() + if ok { + return pf, nil + } + + pf, err := openPackFile(repo.repoPath(rel), rel) + if err != nil { + return nil, err + } + + repo.packFilesMu.Lock() + if existing, ok := repo.packFiles[rel]; ok { + repo.packFilesMu.Unlock() + _ = pf.Close() + return existing, nil + } + repo.packFiles[rel] = pf + repo.packFilesMu.Unlock() + return pf, nil +} -- cgit v1.3.1-10-gc9f91