summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2020-12-15 20:57:32 +0100
committerJulian Andres Klode <julian.klode@canonical.com>2020-12-15 22:13:12 +0100
commitc6d40932f81edc656bbcc8dbd9d277aba543bc5b (patch)
tree83119f8e12d44a5d1da0c589c4489b675c8fbbd8
parent5afd3ff6834430bb252ce5a37686bf1481f7c590 (diff)
Unroll pkgCache::sHash 8 time, break up dependency
Unroll pkgCache::sHash 8 times and break up the dependency between the iterations by expanding the calculation H(n) = 33 * H(n-1) + c 8 times rather than performing it 8 times. This seems to yield about a 0.4% performance improvement. I tried unrolling 4 and 2 bytes as well, those only having 3 ifs at the end rather than 1 small loop; but that was actually slower - potentially the code got to large and the cache went bonkers. I also tried unrolling 4 times instead of 8, thinking that smaller code might yield better results overall then, but that was slower as well.
-rw-r--r--apt-pkg/pkgcache.cc18
1 files changed, 16 insertions, 2 deletions
diff --git a/apt-pkg/pkgcache.cc b/apt-pkg/pkgcache.cc
index 458860b0a..f7f3537aa 100644
--- a/apt-pkg/pkgcache.cc
+++ b/apt-pkg/pkgcache.cc
@@ -209,8 +209,22 @@ bool pkgCache::ReMap(bool const &Errorchecks)
map_id_t pkgCache::sHash(StringView Str) const
{
uint32_t Hash = 5381;
- for (auto I = Str.begin(); I != Str.end(); ++I)
- Hash = 33 * Hash + tolower_ascii_unsafe(*I);
+ auto I = Str.begin();
+ auto End = Str.end();
+ for (; I + 7 < End; I += 8)
+ {
+ Hash = (33u * 33u * 33u * 33u * 33u * 33u * 33u * 33u * Hash +
+ 33u * 33u * 33u * 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[0]) +
+ 33u * 33u * 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[1]) +
+ 33u * 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[2]) +
+ 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[3]) +
+ 33u * 33u * 33u * tolower_ascii_unsafe(I[4]) +
+ 33u * 33u * tolower_ascii_unsafe(I[5]) +
+ 33u * tolower_ascii_unsafe(I[6]) +
+ tolower_ascii_unsafe(I[7]));
+ }
+ for (; I != End; ++I)
+ Hash = 33u * Hash + tolower_ascii_unsafe(*I);
return Hash % HeaderP->GetHashTableSize();
}
uint32_t pkgCache::CacheHash()