From b8f676ae7ffe12877d013c5256a785d35915c491 Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 6 Aug 2022 05:09:16 +0200 Subject: rehash types set --- bootstrap/types.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/bootstrap/types.c b/bootstrap/types.c index 386ad58..7b223f6 100644 --- a/bootstrap/types.c +++ b/bootstrap/types.c @@ -23,7 +23,7 @@ inittypes() { free(types.buckets); types.size = 0; - types.buckets = calloc(types.nbuckets = 64, sizeof(struct typesnode *)); + types.buckets = calloc(types.nbuckets = 16, sizeof(struct typesnode *)); } static u32 @@ -118,6 +118,29 @@ typesput(const struct type *key) { types.size++; n->hash = hash; *(struct type *)&n->ty = *key; + + if (++types.size > types.nbuckets) { + // rehash + struct typesnode *nodes = NULL; + for (int i = 0; i < types.nbuckets; ++i) { + for (struct typesnode *n = types.buckets[i], *next; n; n = next) { + next = n->next; + n->next = nodes; + nodes = n; + } + } + types.buckets = xrealloc(types.buckets, + (types.nbuckets *= 2) * sizeof(struct typesnode)); + memset(types.buckets, 0, types.nbuckets * sizeof(struct typesnode)); + + for (struct typesnode *next; nodes; nodes = next) { + u32 idx = nodes->hash & (types.nbuckets - 1); + next = nodes->next; + nodes->next = types.buckets[idx]; + types.buckets[idx] = nodes; + } + } + n->next = types.buckets[idx]; types.buckets[idx] = n; return &n->ty; -- cgit v1.2.3