]> git.ipfire.org Git - thirdparty/libsolv.git/commitdiff
Refactor pool_resize_rels_hash
authorMichael Schroeder <mls@suse.de>
Mon, 9 Jul 2018 11:32:54 +0000 (13:32 +0200)
committerMichael Schroeder <mls@suse.de>
Wed, 11 Jul 2018 13:35:12 +0000 (15:35 +0200)
src/pool.c
src/poolid.c
src/poolid.h
src/repo_solv.c

index ba5e799c15e8e718dcbb65f4dbd2b6ccbf5b35c0..60cc0f49c7dbfc7da8edb1c44e996ce55c3cc117 100644 (file)
@@ -44,7 +44,7 @@ pool_create(void)
 
   pool = (Pool *)solv_calloc(1, sizeof(*pool));
 
-  stringpool_init (&pool->ss, initpool_data);
+  stringpool_init(&pool->ss, initpool_data);
 
   /* alloc space for RelDep 0 */
   pool->rels = solv_extend_resize(0, 1, sizeof(Reldep), REL_BLOCK);
index bb8d4f6de26c0aff443b25cfb364638da62dbdd5..3b55f76455745ff88dfbbc7aa625dc13ff637038 100644 (file)
@@ -51,39 +51,58 @@ pool_strn2id(Pool *pool, const char *str, unsigned int len, int create)
   return id;
 }
 
+void
+pool_resize_rels_hash(Pool *pool, int numnew)
+{
+  Hashval h, hh, hashmask;
+  Hashtable hashtbl;
+  int i;
+  Reldep *rd;
+
+  if (numnew <= 0)
+    return;
+  hashmask = mkmask(pool->nrels + numnew);
+  if (hashmask <= pool->relhashmask)
+    return;    /* same as before */
+
+  /* realloc hash table */
+  pool->relhashmask = hashmask;
+  solv_free(pool->relhashtbl);
+  pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
+
+  /* rehash all rels into new hashtable */
+  for (i = 1, rd = pool->rels + i; i < pool->nrels; i++, rd++)
+    {
+      h = relhash(rd->name, rd->evr, rd->flags) & hashmask;
+      hh = HASHCHAIN_START;
+      while (hashtbl[h])
+       h = HASHCHAIN_NEXT(h, hh, hashmask);
+      hashtbl[h] = i;
+    }
+}
+
 Id
 pool_rel2id(Pool *pool, Id name, Id evr, int flags, int create)
 {
   Hashval h, hh, hashmask;
-  int i;
   Id id;
   Hashtable hashtbl;
   Reldep *ran;
 
-  hashmask = pool->relhashmask;
-  hashtbl = pool->relhashtbl;
-  ran = pool->rels;
 
   /* extend hashtable if needed */
+  hashmask = pool->relhashmask;
   if ((Hashval)pool->nrels * 2 > hashmask)
     {
-      solv_free(pool->relhashtbl);
-      pool->relhashmask = hashmask = mkmask(pool->nrels + REL_BLOCK);
-      pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
-      /* rehash all rels into new hashtable */
-      for (i = 1; i < pool->nrels; i++)
-       {
-         h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
-         hh = HASHCHAIN_START;
-         while (hashtbl[h])
-           h = HASHCHAIN_NEXT(h, hh, hashmask);
-         hashtbl[h] = i;
-       }
+      pool_resize_rels_hash(pool, REL_BLOCK);
+      hashmask = pool->relhashmask;
     }
+  hashtbl = pool->relhashtbl;
 
   /* compute hash and check for match */
   h = relhash(name, evr, flags) & hashmask;
   hh = HASHCHAIN_START;
+  ran = pool->rels;
   while ((id = hashtbl[h]) != 0)
     {
       if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
@@ -297,15 +316,28 @@ pool_dep2str(Pool *pool, Id id)
   return p;
 }
 
+static void
+pool_free_rels_hash(Pool *pool)
+{
+  pool->relhashtbl = solv_free(pool->relhashtbl);
+  pool->relhashmask = 0;
+}
+
 void
 pool_shrink_strings(Pool *pool)
 {
+  /* free excessive big hashes */
+  if (pool->ss.stringhashmask && pool->ss.stringhashmask > mkmask(pool->ss.nstrings + 8192))
+    stringpool_freehash(&pool->ss);
   stringpool_shrink(&pool->ss);
 }
 
 void
 pool_shrink_rels(Pool *pool)
 {
+  /* free excessive big hashes */
+  if (pool->relhashmask && pool->relhashmask > mkmask(pool->nrels + 4096))
+    pool_free_rels_hash(pool);
   pool->rels = solv_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
 }
 
@@ -314,8 +346,7 @@ void
 pool_freeidhashes(Pool *pool)
 {
   stringpool_freehash(&pool->ss);
-  pool->relhashtbl = solv_free(pool->relhashtbl);
-  pool->relhashmask = 0;
+  pool_free_rels_hash(pool);
 }
 
 /* EOF */
index 2363595569afcd233facceea5b19a6540656e7cc..79a3ccdc78213d7b1c6b6d56103151a689a2961b 100644 (file)
@@ -41,6 +41,7 @@ extern const char *pool_dep2str(Pool *pool, Id); /* might alloc tmpspace */
 extern void pool_shrink_strings(Pool *pool);
 extern void pool_shrink_rels(Pool *pool);
 extern void pool_freeidhashes(Pool *pool);
+extern void pool_resize_rels_hash(Pool *pool, int numnew);
 
 #ifdef __cplusplus
 }
index 3509e866b0b52aa6d877fda8c3a391c4487ada83..42c27e851fd848521f47df556e1d3d960d50448a 100644 (file)
@@ -739,11 +739,6 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
          idmap[i] = id;       /* repo relative -> pool relative */
          sp += l;             /* next string */
        }
-      if (hashmask > mkmask(spool->nstrings + 8192))
-       {
-         spool->stringhashtbl = solv_free(spool->stringhashtbl);
-         spool->stringhashmask = 0;
-       }
       stringpool_shrink(spool);                /* vacuum */
     }
 
@@ -761,31 +756,13 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
       pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
       ran = pool->rels;
 
-      /* grow hash if needed, otherwise reuse */
-      hashmask = mkmask(pool->nrels + numrel);
+      pool_resize_rels_hash(pool, numrel);
+      hashtbl = pool->relhashtbl;
+      hashmask = pool->relhashmask;
 #if 0
       POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
-      POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d, old %d\n", hashmask + 1, pool->relhashmask + 1);
+      POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
 #endif
-      if (hashmask > pool->relhashmask)
-       {
-         pool->relhashtbl = solv_free(pool->relhashtbl);
-         pool->relhashmask = hashmask;
-          pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
-         for (i = 1; i < pool->nrels; i++)
-           {
-             h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
-             hh = HASHCHAIN_START;
-             while (hashtbl[h])
-               h = HASHCHAIN_NEXT(h, hh, hashmask);
-             hashtbl[h] = i;
-           }
-       }
-      else
-       {
-         hashtbl = pool->relhashtbl;
-         hashmask = pool->relhashmask;
-       }
 
       /*
        * read RelDeps from repo
@@ -818,11 +795,6 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
            }
          idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
        }
-      if (hashmask > mkmask(pool->nrels + 4096))
-       {
-         pool->relhashtbl = solv_free(pool->relhashtbl);
-         pool->relhashmask = 0;
-       }
       pool_shrink_rels(pool);          /* vacuum */
     }