Skip to content

Commit

Permalink
Replace dependency lookup cache with an STL unordered map
Browse files Browse the repository at this point in the history
Annoyingly, rpmdbProvides() can get called with a NULL dcache so we
can't use a reference for passing it around. Dynamically allocating
it would be silly so just pass a pointer to the local variable around
instead (to avoid otherwise unnecessary code changes)
  • Loading branch information
pmatilai committed May 7, 2024
1 parent cce0bdb commit 24b6105
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions lib/depends.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "system.h"

#include <string>
#include <unordered_map>

#include <rpm/rpmlib.h> /* rpmVersionCompare, rpmlib provides */
#include <rpm/rpmtag.h>
#include <rpm/rpmlog.h>
Expand All @@ -30,14 +33,7 @@ const char * const rpmEVR = VERSION;

const int rpmFLAGS = RPMSENSE_EQUAL;

#define HASHTYPE depCache
#define HTKEYTYPE const char *
#define HTDATATYPE int
#include "rpmhash.H"
#include "rpmhash.C"
#undef HASHTYPE
#undef HTKEYTYPE
#undef HTDATATYPE
typedef std::unordered_map<std::string,int> depCache;

#define HASHTYPE filedepHash
#define HTKEYTYPE rpmsid
Expand Down Expand Up @@ -508,24 +504,22 @@ int rpmtsAddEraseElement(rpmts ts, Header h, int dboffset)
}

/* Cached rpmdb provide lookup, returns 0 if satisfied, 1 otherwise */
static int rpmdbProvides(rpmts ts, depCache dcache, rpmds dep, dbiIndexSet *matches)
static int rpmdbProvides(rpmts ts, depCache *dcache, rpmds dep, dbiIndexSet *matches)
{
const char * Name = rpmdsN(dep);
const char * DNEVR = rpmdsDNEVR(dep);
rpmTagVal deptag = rpmdsTagN(dep);
int *cachedrc = NULL;
rpmdbMatchIterator mi = NULL;
Header h = NULL;
int rc = 0;
/* pretrans deps are provided by current packages, don't prune erasures */
int prune = (rpmdsFlags(dep) & (RPMSENSE_PRETRANS|RPMSENSE_PREUNTRANS)) ? 0 : 1;
unsigned int keyhash = 0;

/* See if we already looked this up */
if (prune && !matches) {
keyhash = depCacheKeyHash(dcache, DNEVR);
if (depCacheGetHEntry(dcache, DNEVR, keyhash, &cachedrc, NULL, NULL)) {
rc = *cachedrc;
auto ret = dcache->find(DNEVR);
if (ret != dcache->end()) {
rc = ret->second;
rpmdsNotify(dep, "(cached)", rc);
return rc;
}
Expand Down Expand Up @@ -600,7 +594,7 @@ static int rpmdbProvides(rpmts ts, depCache dcache, rpmds dep, dbiIndexSet *matc
/* Cache the relatively expensive rpmdb lookup results */
/* Caching the oddball non-pruned case would mess up other results */
if (prune && !matches)
depCacheAddHEntry(dcache, xstrdup(DNEVR), keyhash, rc);
dcache->insert({DNEVR, rc});
return rc;
}

Expand Down Expand Up @@ -672,7 +666,7 @@ static dbiIndexSet unsatisfiedDependSet(rpmts ts, rpmds dep)
* @param dep dependency
* @return 0 if satisfied, 1 if not satisfied
*/
static int unsatisfiedDepend(rpmts ts, depCache dcache, rpmds dep)
static int unsatisfiedDepend(rpmts ts, depCache *dcache, rpmds dep)
{
tsMembers tsmem = rpmtsMembers(ts);
int rc;
Expand Down Expand Up @@ -801,7 +795,7 @@ static int unsatisfiedDepend(rpmts ts, depCache dcache, rpmds dep)
}

/* Check a dependency set for problems */
static void checkDS(rpmts ts, depCache dcache, rpmte te,
static void checkDS(rpmts ts, depCache *dcache, rpmte te,
const char * pkgNEVRA, rpmds ds,
rpm_color_t tscolor)
{
Expand All @@ -822,7 +816,7 @@ static void checkDS(rpmts ts, depCache dcache, rpmte te,
}

/* Check a given dependency against installed packages */
static void checkInstDeps(rpmts ts, depCache dcache, rpmte te,
static void checkInstDeps(rpmts ts, depCache *dcache, rpmte te,
rpmTag depTag, const char *dep, rpmds depds, int neg)
{
Header h;
Expand Down Expand Up @@ -872,7 +866,7 @@ static void checkInstDeps(rpmts ts, depCache dcache, rpmte te,
free(ndep);
}

static void checkInstFileDeps(rpmts ts, depCache dcache, rpmte te,
static void checkInstFileDeps(rpmts ts, depCache *dcache, rpmte te,
rpmTag depTag, rpmfi fi, int is_not,
filedepHash cache, fingerPrintCache *fpcp)
{
Expand Down Expand Up @@ -979,7 +973,7 @@ int rpmtsCheck(rpmts ts)
rpmtsi pi = NULL; rpmte p;
int closeatexit = 0;
int rc = 0;
depCache dcache = NULL;
depCache _dcache, *dcache = &_dcache;
filedepHash confilehash = NULL; /* file conflicts of installed packages */
filedepHash connotfilehash = NULL; /* file conflicts of installed packages */
depexistsHash connothash = NULL;
Expand All @@ -1003,10 +997,6 @@ int rpmtsCheck(rpmts ts)
if (rdb)
rpmdbCtrl(rdb, RPMDB_CTRL_LOCK_RO);

/* XXX FIXME: figure some kind of heuristic for the cache size */
dcache = depCacheCreate(5001, rstrhash, strcmp,
(depCacheFreeKey)rfree, NULL);

/* build hashes of all confilict sdependencies */
confilehash = filedepHashCreate(257, sidHash, sidCmp, NULL, NULL);
connothash = depexistsHashCreate(257, sidHash, sidCmp, NULL);
Expand Down Expand Up @@ -1117,7 +1107,6 @@ int rpmtsCheck(rpmts ts)
rpmdbCtrl(rdb, RPMDB_CTRL_UNLOCK_RO);

exit:
depCacheFree(dcache);
filedepHashFree(confilehash);
filedepHashFree(connotfilehash);
depexistsHashFree(connothash);
Expand Down

0 comments on commit 24b6105

Please sign in to comment.