diff options
| author | 2022-08-06 05:09:16 +0200 | |
|---|---|---|
| committer | 2022-08-06 05:19:08 +0200 | |
| commit | b8f676ae7ffe12877d013c5256a785d35915c491 (patch) | |
| tree | 581884d3b4cf8649a7ccc9f615300617831363bd /bootstrap/types.c | |
| parent | 4ebbafbb19455d71cd185504cdef992e1a91db7e (diff) | |
rehash types set
Diffstat (limited to 'bootstrap/types.c')
| -rw-r--r-- | bootstrap/types.c | 25 |
1 files changed, 24 insertions, 1 deletions
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; |