Skip to content

Commit e23cd54

Browse files
committed
copy method cache into new singleton class for regular object
1 parent 958ea5e commit e23cd54

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

class.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ make_singleton_class(VALUE obj)
507507
{
508508
VALUE orig_class = RBASIC(obj)->klass;
509509
VALUE klass = rb_class_boot(orig_class);
510+
rb_method_cache_copy(orig_class, klass);
510511

511512
FL_SET(klass, FL_SINGLETON);
512513
RBASIC_SET_CLASS(obj, klass);

internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ VALUE rb_extract_keywords(VALUE *orighash);
805805
/* vm_method.c */
806806
void Init_eval_method(void);
807807
int rb_method_defined_by(VALUE obj, ID mid, VALUE (*cfunc)(ANYARGS));
808+
void rb_method_cache_copy(VALUE from, VALUE to);
808809

809810
/* miniprelude.c, prelude.c */
810811
void Init_prelude(void);

vm_method.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ rb_mcache_find(struct rb_meth_cache *cache, ID id)
113113
}
114114
}
115115

116+
void
117+
rb_method_cache_copy(VALUE from, VALUE to)
118+
{
119+
struct rb_meth_cache *from_cache = &RCLASS_EXT(from)->cache, *to_cache = &RCLASS_EXT(to)->cache;
120+
if (!from_cache->size) return;
121+
122+
if (to_cache->entries)
123+
xfree(to_cache->entries);
124+
to_cache->capa = from_cache->capa;
125+
to_cache->entries = xcalloc(to_cache->capa, sizeof(struct cache_entry));
126+
if (from_cache->entries)
127+
MEMCPY(to_cache->entries, from_cache->entries, struct cache_entry, from_cache->capa);
128+
to_cache->size = from_cache->size;
129+
to_cache->method_state = from_cache->method_state;
130+
to_cache->class_serial = RCLASS_SERIAL(to);
131+
}
132+
116133
#define ruby_running (GET_VM()->running)
117134
/* int ruby_running = 0; */
118135

0 commit comments

Comments
 (0)