From 5383bb20623ef902030bcbf14b8bdb13a749f13d Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Fri, 2 Feb 2024 13:40:08 -0800 Subject: [PATCH] Eliminate repeated hashing of the same string --- pool.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pool.cpp b/pool.cpp index 744cda50..bf3059c3 100644 --- a/pool.cpp +++ b/pool.cpp @@ -9,14 +9,13 @@ #include "errors.hpp" #include "text.hpp" -int swizzlecmp(const char *a, const char *b) { - unsigned long long h1 = fnv1a(a, 0); - unsigned long long h2 = fnv1a(b, 0); +int swizzlecmp(const char *a, unsigned long long ahash, const char *b, int btype) { + unsigned long long bhash = fnv1a(b, btype); - if (h1 == h2) { + if (ahash == bhash) { return strcmp(a, b); } 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) { - int cmp = swizzlecmp(s, poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off + 1); - - if (cmp == 0) { - cmp = type - (poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off)[0]; - } + int cmp = swizzlecmp(s, hash, + poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off + 1, + (poolfile->map.c_str() + ((struct stringpool *) (treefile->map.c_str() + *sp))->off)[0]); if (cmp < 0) { sp = &(((struct stringpool *) (treefile->map.c_str() + *sp))->left);