mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
vm.c: replace constant cache generation counter with direct invalidation
Remove the per-entry generation field and per-state generation counter. Invalidation now clears entries directly, removing one comparison from every OP_GETCONST hot path. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
+5
-2
@@ -256,7 +256,6 @@ mrb_static_assert_powerof2(MRB_CONST_CACHE_SIZE);
|
|||||||
struct mrb_const_cache_entry {
|
struct mrb_const_cache_entry {
|
||||||
const struct mrb_irep *irep;
|
const struct mrb_irep *irep;
|
||||||
mrb_sym sym;
|
mrb_sym sym;
|
||||||
uint32_t generation;
|
|
||||||
mrb_value value;
|
mrb_value value;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -316,7 +315,6 @@ struct mrb_state {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MRB_NO_CONST_CACHE
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
uint32_t const_generation;
|
|
||||||
struct mrb_const_cache_entry const_cache[MRB_CONST_CACHE_SIZE];
|
struct mrb_const_cache_entry const_cache[MRB_CONST_CACHE_SIZE];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -1307,6 +1305,11 @@ MRB_API void mrb_method_cache_clear(mrb_state *mrb);
|
|||||||
#else
|
#else
|
||||||
#define mrb_method_cache_clear(mrb) ((void)0)
|
#define mrb_method_cache_clear(mrb) ((void)0)
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
|
void mrb_const_cache_clear(mrb_state *mrb);
|
||||||
|
#else
|
||||||
|
#define mrb_const_cache_clear(mrb) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if mrb_open() failed
|
* Check if mrb_open() failed
|
||||||
|
|||||||
+15
-3
@@ -1423,6 +1423,18 @@ mrb_vm_const_get(mrb_state *mrb, mrb_sym sym)
|
|||||||
* Raises:
|
* Raises:
|
||||||
* E_TYPE_ERROR: If `mod` is not a class or module.
|
* E_TYPE_ERROR: If `mod` is not a class or module.
|
||||||
*/
|
*/
|
||||||
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
|
void
|
||||||
|
mrb_const_cache_clear(mrb_state *mrb)
|
||||||
|
{
|
||||||
|
struct mrb_const_cache_entry *cc = mrb->const_cache;
|
||||||
|
|
||||||
|
for (int i=0; i<MRB_CONST_CACHE_SIZE; cc++,i++) {
|
||||||
|
cc->irep = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
MRB_API void
|
MRB_API void
|
||||||
mrb_const_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
|
mrb_const_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
|
||||||
{
|
{
|
||||||
@@ -1432,7 +1444,7 @@ mrb_const_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
|
|||||||
}
|
}
|
||||||
mrb_obj_iv_set(mrb, mrb_obj_ptr(mod), sym, v);
|
mrb_obj_iv_set(mrb, mrb_obj_ptr(mod), sym, v);
|
||||||
#ifndef MRB_NO_CONST_CACHE
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
mrb->const_generation++;
|
mrb_const_cache_clear(mrb);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!mrb->bootstrapping) {
|
if (!mrb->bootstrapping) {
|
||||||
@@ -1461,7 +1473,7 @@ mrb_const_remove(mrb_state *mrb, mrb_value mod, mrb_sym sym)
|
|||||||
mod_const_check(mrb, mod);
|
mod_const_check(mrb, mod);
|
||||||
mrb_iv_remove(mrb, mod, sym);
|
mrb_iv_remove(mrb, mod, sym);
|
||||||
#ifndef MRB_NO_CONST_CACHE
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
mrb->const_generation++;
|
mrb_const_cache_clear(mrb);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1481,7 +1493,7 @@ mrb_define_const_id(mrb_state *mrb, struct RClass *mod, mrb_sym name, mrb_value
|
|||||||
{
|
{
|
||||||
mrb_obj_iv_set(mrb, (struct RObject*)mod, name, v);
|
mrb_obj_iv_set(mrb, (struct RObject*)mod, name, v);
|
||||||
#ifndef MRB_NO_CONST_CACHE
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
mrb->const_generation++;
|
mrb_const_cache_clear(mrb);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2032,7 +2032,7 @@ RETRY_TRY_BLOCK:
|
|||||||
mrb_sym sym = irep->syms[b];
|
mrb_sym sym = irep->syms[b];
|
||||||
uint32_t h = mrb_int_hash_func(mrb, ((intptr_t)irep) ^ sym) & (MRB_CONST_CACHE_SIZE-1);
|
uint32_t h = mrb_int_hash_func(mrb, ((intptr_t)irep) ^ sym) & (MRB_CONST_CACHE_SIZE-1);
|
||||||
struct mrb_const_cache_entry *cc = &mrb->const_cache[h];
|
struct mrb_const_cache_entry *cc = &mrb->const_cache[h];
|
||||||
if (cc->irep == irep && cc->sym == sym && cc->generation == mrb->const_generation) {
|
if (cc->irep == irep && cc->sym == sym) {
|
||||||
regs[a] = cc->value;
|
regs[a] = cc->value;
|
||||||
NEXT;
|
NEXT;
|
||||||
}
|
}
|
||||||
@@ -2044,7 +2044,6 @@ RETRY_TRY_BLOCK:
|
|||||||
#ifndef MRB_NO_CONST_CACHE
|
#ifndef MRB_NO_CONST_CACHE
|
||||||
cc->irep = irep;
|
cc->irep = irep;
|
||||||
cc->sym = sym;
|
cc->sym = sym;
|
||||||
cc->generation = mrb->const_generation;
|
|
||||||
cc->value = v;
|
cc->value = v;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user