commit ba81fd47b2fc845a3b5e53e3203c9e67bca8fef7 from: Sven M. Hallberg date: Fri Jun 12 16:58:29 2020 UTC always rebuild the hash table on grow, no realloc commit - 2bac640ef0b48e6ad7a239c30efd1f40dea37caf commit + ba81fd47b2fc845a3b5e53e3203c9e67bca8fef7 blob - dd835e84fec59008d7c3219a6d67879906247244 blob + 2b72b70584f458631d9800269777c132a28b8287 --- ini_a.c +++ ini_a.c @@ -129,6 +129,7 @@ trace(void *ctx, void *env, const char *s, size_t len) if (env == NULL) return true; + if (env == NULL-1) return true; // XXX benchmark pos = s - begin; printf("%4zx: %4zu byte %s\n", pos, len, nt); @@ -173,6 +174,10 @@ main(int argc, char *argv[]) if (input == MAP_FAILED) err(2, "mmap"); + // XXX benchmark + for (int i = 0; i < 100000; i++) + inifile(input, input + sz, NULL-1); + /* run parser */ p = inifile(input, input + sz, (void *)input); if (p == NULL) { blob - 0c3683eb3845e8842f541548139e119e4fa502f6 blob + 1b3950f7c629dd6f556f022cdb590c93008588ef --- minip_m.h +++ minip_m.h @@ -254,7 +254,7 @@ memo_(struct stream inp, struct cache *cache, void *en /* grow hashtable if necessary */ if (cache->nused >= cache->capacity * 9 / 10) { - void *newp; + struct memento *newp; size_t newcap; size_t j; @@ -267,15 +267,10 @@ memo_(struct stream inp, struct cache *cache, void *en newcap = cache->capacity * 2; fprintf(stderr, "XXX grow at %zu/%zu -> newcap %zu\n", cache->nused, cache->capacity, newcap); - newp = realloc(cache->table, newcap * sizeof *cache->table); - memset(newp + cache->capacity, 0, - (newcap - cache->capacity) * sizeof *newp); - assert(newp != NULL); - if (newp != cache->table) - fprintf(stderr, "XXX TABLE MOVE!\n"); - cache->table = newp; + newp = calloc(newcap, sizeof *cache->table); + assert(newp != NULL); - /* relocate items as necessary */ + /* relocate items */ size_t count=0, count2=0; // XXX for (i=0; i < cache->capacity; i++) { const char *pi = cache->table[i].position; @@ -294,31 +289,28 @@ memo_(struct stream inp, struct cache *cache, void *en /* find the correct place for it in the new table */ for (s=1; ; h+=s, s*=5) { h %= newcap; - mem = cache->table + h; - if (h == i) /* same spot */ - break; + mem = newp + h; if (mem->position == NULL) /* unused */ break; - //if (mem->symbol == NULL) /* dummy */ - // break; /* reuse! */ } if (s != 1) count2++; - /* move the entry if needed, leaving a dummy behind */ + /* copy the entry to the new array */ if (h != i) { - fprintf(stderr, "XXX relocate %zu -> %u %s\n", + fprintf(stderr, "XXX move %zu -> %u %s\n", i, h, h < cache->capacity ? "(!)" : ""); count++; // XXX - *mem = cache->table[i]; - cache->table[i].symbol = NULL; } + *mem = cache->table[i]; } - fprintf(stderr, "XXX had to move %zu/%zu entries, i.e. %.1f%%\n", + fprintf(stderr, "XXX pos. changed on %zu/%zu entries = %.1f%%\n", count, cache->nused, count * 100.0 / cache->nused); fprintf(stderr, "XXX now %zu/%zu are off-base, i.e. %.1f%%\n", count2, cache->nused, count2 * 100.0 / cache->nused); + free(cache->table); + cache->table = newp; cache->capacity = newcap; }