Initial (bad) gc

This commit is contained in:
2026-01-21 20:52:18 -08:00
parent 4c04e71078
commit 656846ddc0
16 changed files with 650 additions and 79 deletions
+46 -15
View File
@@ -6,14 +6,28 @@
#define GROWTH_THRESHOLD 0.5
#define GROWTH_FACTOR 2
static ALWAYS_INLINE bool BUCKET_EMPTY_P(struct HashTableBucket *b) {
return !b->key;
}
static ALWAYS_INLINE float TABLE_LOAD(LispHashTable *ht) {
return ((float) ht->count) / ht->size;
}
LispVal *make_hash_table_no_gc(LispVal *hash_fn, LispVal *eq_fn) {
LispHashTable *obj =
lisp_alloc_object_no_gc(sizeof(LispHashTable), TYPE_HASH_TABLE);
obj->eq_fn = eq_fn;
obj->hash_fn = hash_fn;
obj->count = 0;
obj->size = INITIAL_SIZE;
obj->data = lisp_malloc0(sizeof(struct HashTableBucket) * obj->size);
return obj;
}
void release_hash_table_no_gc(LispVal *val) {
assert(HASH_TABLE_P(val));
LispHashTable *ht = val;
lisp_free(ht->data);
lisp_free(ht);
}
DEFUN(make_hash_table, "make-hash-table", (LispVal * hash_fn, LispVal *eq_fn),
"(hash-fn eq-fn)", "") {
LispHashTable *obj =
@@ -51,7 +65,7 @@ find_bucket_for_key(LispHashTable *ht, LispVal *key, uintptr_t hash) {
assert(TABLE_LOAD(ht) < 0.95f);
for (uintptr_t i = hash % ht->size; true; i = (i + 1) % ht->size) {
struct HashTableBucket *cb = &ht->data[i];
if (BUCKET_EMPTY_P(cb)
if (HT_BUCKET_EMPTY_P(cb)
|| (cb->hash == hash && compare_keys(ht, key, cb->key))) {
return cb;
}
@@ -59,13 +73,14 @@ find_bucket_for_key(LispHashTable *ht, LispVal *key, uintptr_t hash) {
}
static void rehash(LispHashTable *ht, size_t new_size) {
ht->cache_bucket = NULL;
struct HashTableBucket *old_data = ht->data;
size_t old_size = ht->size;
ht->size = new_size;
ht->data = lisp_malloc0(sizeof(struct HashTableBucket) * new_size);
for (size_t i = 0; i < old_size; ++i) {
struct HashTableBucket *cob = &old_data[i];
if (!BUCKET_EMPTY_P(cob)) {
if (!HT_BUCKET_EMPTY_P(cob)) {
struct HashTableBucket *nb =
find_bucket_for_key(ht, cob->key, cob->hash);
nb->hash = cob->hash;
@@ -84,37 +99,53 @@ static void maybe_rehash(LispHashTable *ht) {
// TODO type checking
DEFUN(gethash, "gethash", (LispVal * ht, LispVal *key, LispVal *def),
"(ht key &optional def)", "") {
uintptr_t hash = hash_key_for_table(ht, key);
struct HashTableBucket *b = find_bucket_for_key(ht, key, hash);
return BUCKET_EMPTY_P(b) ? def : b->value;
LispHashTable *obj = ht;
if (obj->cache_bucket && key == obj->cache_bucket->key) {
return obj->cache_bucket->value;
}
uintptr_t hash = hash_key_for_table(obj, key);
struct HashTableBucket *b = find_bucket_for_key(obj, key, hash);
obj->cache_bucket = b;
return HT_BUCKET_EMPTY_P(b) ? def : b->value;
}
DEFUN(puthash, "puthash", (LispVal * ht, LispVal *key, LispVal *val),
"(ht key val)", "") {
LispHashTable *obj = ht;
if (obj->cache_bucket && key == obj->cache_bucket->key) {
obj->cache_bucket->value = val;
return Qnil;
}
maybe_rehash(ht);
uintptr_t hash = hash_key_for_table(ht, key);
struct HashTableBucket *b = find_bucket_for_key(ht, key, hash);
if (BUCKET_EMPTY_P(b)) {
if (HT_BUCKET_EMPTY_P(b)) {
b->hash = hash;
b->key = key;
}
b->value = val;
++((LispHashTable *) ht)->count;
obj->cache_bucket = b;
return Qnil;
}
DEFUN(remhash, "remhash", (LispVal * ht, LispVal *key), "(ht key)", "") {
LispHashTable *obj = ht;
uintptr_t hash = hash_key_for_table(ht, key);
struct HashTableBucket *b = find_bucket_for_key(ht, key, hash);
if (BUCKET_EMPTY_P(b)) {
struct HashTableBucket *b;
if (obj->cache_bucket && obj->cache_bucket->key == key) {
b = obj->cache_bucket;
} else {
b = find_bucket_for_key(ht, key, hash);
}
if (HT_BUCKET_EMPTY_P(b)) {
return Qnil;
}
b->key = NULL;
b->value = NULL; // just because
LispHashTable *tobj = ht;
--tobj->count;
size_t k = hash % tobj->size;
for (size_t i = (k + 1) % tobj->size; !BUCKET_EMPTY_P(&tobj->data[i]);
for (size_t i = (k + 1) % tobj->size; !HT_BUCKET_EMPTY_P(&tobj->data[i]);
i = (i + 1) % tobj->size) {
size_t target = tobj->data[i].hash % tobj->size;
if ((i > k && target >= k && target < i)
@@ -123,10 +154,10 @@ DEFUN(remhash, "remhash", (LispVal * ht, LispVal *key), "(ht key)", "") {
tobj->data[k].key = tobj->data[i].key;
tobj->data[k].value = tobj->data[i].value;
tobj->data[i].key = NULL;
tobj->data[i].value = NULL;
k = i;
}
}
obj->cache_bucket = NULL;
return Qt;
}