From f0adfb4dedd27d62db8579cbdac07f72f1500cf9 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sun, 5 Dec 2021 00:13:54 +0000 Subject: [PATCH] git: improve pack cache heuristics the pack cache was very stupid: it would close packs as early as possible, which would prevent packs from getting reused effectively. It would also select a bad pack to close. This picks the oldest pack, refcounts correctly, and keeps up to Npackcache open at once (though it will go over if more are in use). --- sys/src/cmd/git/pack.c | 43 +++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/sys/src/cmd/git/pack.c b/sys/src/cmd/git/pack.c index c86e06b29..ade8591fa 100644 --- a/sys/src/cmd/git/pack.c +++ b/sys/src/cmd/git/pack.c @@ -253,38 +253,51 @@ openpack(Packf *pf) vlong t; int i, best; - if(pf->pack == nil){ - if((pf->pack = Bopen(pf->path, OREAD)) == nil) - return nil; - openpacks++; + if(pf->pack != nil){ + pf->refs++; + return pf->pack; } - if(openpacks == Npackcache){ - t = pf->opentm; + /* + * If we've got more packs open + * than we want cached, try to + * free up the oldest ones. + * + * If we can't find a slot, this + * isn't fatal; we can just use + * another fd. + */ + while(openpacks >= Npackcache){ + t = (1ull<<62)-1; best = -1; for(i = 0; i < npackf; i++){ - if(packf[i].opentm < t && packf[i].refs > 0){ + if(&packf[i] != pf + && packf[i].pack != nil + && packf[i].opentm < t + && packf[i].refs == 0){ t = packf[i].opentm; best = i; } } - if(best != -1){ - Bterm(packf[best].pack); - packf[best].pack = nil; - openpacks--; + if(best == -1){ + fprint(2, "no available pack slots\n"); + break; } + Bterm(packf[best].pack); + packf[best].pack = nil; + openpacks--; } + openpacks++; pf->opentm = nsec(); pf->refs++; + if((pf->pack = Bopen(pf->path, OREAD)) == nil) + return nil; return pf->pack; } static void closepack(Packf *pf) { - if(--pf->refs == 0 && pf->pack != nil){ - Bterm(pf->pack); - pf->pack = nil; - } + pf->refs--; } static u32int