mie: implement caching and emitting string data

This commit is contained in:
2025-04-17 22:55:17 +01:00
parent abb7cfaf0e
commit 9cce4bf541
11 changed files with 211 additions and 58 deletions

View File

@@ -43,6 +43,7 @@ struct mie_ctx *mie_ctx_create(void)
out->ctx_false = MIE_CONST(mie_ctx_get_int(out, 0, 1));
out->ctx_sel_cache = b_hashmap_create(free, free);
out->ctx_string_cache = b_hashmap_create(free, free);
return out;
}
@@ -214,3 +215,38 @@ struct mie_value *mie_ctx_get_selector(struct mie_ctx *ctx, const char *sel)
return MIE_VALUE(sel_value);
}
struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s)
{
b_hashmap_key key = {
.key_data = s,
.key_size = strlen(s),
};
const b_hashmap_value *cache_entry
= b_hashmap_get(ctx->ctx_string_cache, &key);
if (cache_entry) {
return cache_entry->value_data;
}
struct mie_const *string_value = malloc(sizeof *string_value);
if (!string_value) {
return NULL;
}
mie_value_init(&string_value->c_base, MIE_VALUE_CONST);
string_value->c_type = mie_ctx_get_type(ctx, MIE_TYPE_STR);
string_value->c_v.v_str = b_strdup(s);
key.key_data = string_value->c_v.v_str;
b_hashmap_value hashmap_value = {
.value_data = string_value,
.value_size = sizeof *string_value,
};
b_hashmap_put(ctx->ctx_string_cache, &key, &hashmap_value);
return MIE_VALUE(string_value);
}