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
+1 -6
View File
@@ -2276,18 +2276,13 @@ static const mrb_mt_entry string_ext_rom_entries[] = {
};
static mrb_mt_tbl string_ext_rom_mt = MRB_MT_ROM_TAB(string_ext_rom_entries);
static const mrb_mt_entry integer_chr_rom_entries[] = {
MRB_MT_ENTRY(int_chr, MRB_SYM(chr), 0),
};
static mrb_mt_tbl integer_chr_rom_mt = MRB_MT_ROM_TAB(integer_chr_rom_entries);
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
struct RClass *s = mrb->string_class;
mrb_mt_init_rom(s, &string_ext_rom_mt);
mrb_mt_init_rom(mrb->integer_class, &integer_chr_rom_mt);
mrb_define_method_id(mrb, mrb->integer_class, MRB_SYM(chr), int_chr, MRB_ARGS_NONE()|MRB_ARGS_OPT(1));
}
void