class.c: merge conditional methods into ROM tables

Move conditional mrb_define_method_id() calls into ROM entry
arrays using #ifdef guards. With linear search, sizeof in
MRB_MT_ROM_TAB() adjusts automatically after preprocessing.

Cross-class ROM tables (methods a gem defines on a class it does
not own) are reverted to mrb_define_method_id(). Multiple gems
should not add ROM table layers to the same class; each layer
costs a 16-byte mrb_mt_tbl struct in RAM and deepens the lookup
chain. Use mrb_define_method_id() for cross-class methods.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-20 11:05:03 +09:00
parent 8adba34bd9
commit 20b9002214
15 changed files with 74 additions and 135 deletions
+4 -6
View File
@@ -694,6 +694,10 @@ static const mrb_mt_entry kernel_rom_entries[] = {
MRB_MT_ENTRY(mrb_ensure_int_type, MRB_SYM(__to_int), MRB_MT_NOARG),
MRB_MT_ENTRY(mrb_false, MRB_SYM_Q(respond_to_missing), MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_obj_method_recursive_p, MRB_SYM_Q(__method_recursive), 0),
#ifndef HAVE_MRUBY_IO_GEM
MRB_MT_ENTRY(mrb_p_m, MRB_SYM(p), MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_print_m, MRB_SYM(print), MRB_MT_PRIVATE),
#endif
};
static mrb_mt_tbl kernel_rom_mt = MRB_MT_ROM_TAB(kernel_rom_entries);
@@ -711,11 +715,5 @@ mrb_init_kernel(mrb_state *mrb)
mrb_mt_init_rom(krn, &kernel_rom_mt);
/* conditional methods not in ROM table */
#ifndef HAVE_MRUBY_IO_GEM
mrb_define_private_method_id(mrb, krn, MRB_SYM(p), mrb_p_m, MRB_ARGS_ANY()); /* 15.3.1.3.34 */
mrb_define_private_method_id(mrb, krn, MRB_SYM(print), mrb_print_m, MRB_ARGS_ANY()); /* 15.3.1.3.35 */
#endif
mrb_include_module(mrb, mrb->object_class, mrb->kernel_module);
}