Eliminate repeated hashing of the same string

This commit is contained in:
Erica Fischer
2024-02-02 13:40:08 -08:00
parent 711a757895
commit 5383bb2062
+7 -10
View File
@@ -9,14 +9,13 @@
#include "errors.hpp" #include "errors.hpp"
#include "text.hpp" #include "text.hpp"
int swizzlecmp(const char *a, const char *b) { int swizzlecmp(const char *a, unsigned long long ahash, const char *b, int btype) {
unsigned long long h1 = fnv1a(a, 0); unsigned long long bhash = fnv1a(b, btype);
unsigned long long h2 = fnv1a(b, 0);
if (h1 == h2) { if (ahash == bhash) {
return strcmp(a, b); return strcmp(a, b);
} else { } else {
return h1 - h2; return (int) ahash - (int) bhash;
} }
} }
@@ -44,11 +43,9 @@ long long addpool(struct memfile *poolfile, struct memfile *treefile, const char
} }
while (*sp != 0) { while (*sp != 0) {
int cmp = swizzlecmp(s, poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off + 1); int cmp = swizzlecmp(s, hash,
poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off + 1,
if (cmp == 0) { (poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off)[0]);
cmp = type - (poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off)[0];
}
if (cmp < 0) { if (cmp < 0) {
sp = &(((struct stringpool *) (treefile->map.c_str() + *sp))->left); sp = &(((struct stringpool *) (treefile->map.c_str() + *sp))->left);