aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-07-27 00:09:04 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-07-27 00:09:04 -0700
commita5a456fab33cb7c56101efe70aa4542154f6b9da (patch)
treee9af310983176ef113f905ccda397d19c03d0b84
parentf86f75d7820012a9442e8c450b422f0f558d78f5 (diff)
Caching micro-optimizations
-rw-r--r--match.c7
1 files changed, 3 insertions, 4 deletions
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);