From a5a456fab33cb7c56101efe70aa4542154f6b9da Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 27 Jul 2021 00:09:04 -0700 Subject: Caching micro-optimizations --- match.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'match.c') diff --git a/match.c b/match.c index 9bde70a..bfae88e 100644 --- a/match.c +++ b/match.c @@ -39,7 +39,6 @@ typedef struct { match_t **matches; } cache_t; -static const double CACHE_MAX_OCCUPANCY = (double)1.618f; #define MAX_CACHE_SIZE (1<<14) cache_t cache = {0, 0, NULL}; @@ -89,7 +88,7 @@ static inline void remove_ownership(match_t** owner) #define MATCHES(...) (match_t*[]){__VA_ARGS__, NULL} -static size_t hash(const char *str, pat_t *pat) +static inline size_t hash(const char *str, pat_t *pat) { return (size_t)str + 2*pat->id; } @@ -119,10 +118,10 @@ static void cache_remove(match_t *m) static void cache_save(match_t *m) { - if ((double)(cache.occupancy + 1) >= ((double)cache.size) * CACHE_MAX_OCCUPANCY) { + if (cache.occupancy+1 > 3*cache.size) { if (cache.size == MAX_CACHE_SIZE) { size_t h = hash(m->start, m->pat) & (cache.size-1); - for (int quota = 2; cache.matches[h] && quota > 0; --quota) { + for (int quota = 2; cache.matches[h] && quota > 0; quota--) { match_t *last = cache.matches[h]; while (last->cache_next) last = last->cache_next; cache_remove(last); -- cgit v1.2.3