class.c: use linear search for method tables; make ROM entries const

Replace binary search with linear scan in mt_get(), mt_put(),
mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache
makes repeated lookups O(1), so linear scan on cache misses is
acceptable.

This removes the sorting requirement, allowing ROM entry arrays
to be declared const. On embedded systems, const static data
resides in flash/ROM instead of RAM, saving ~8.4KB for ~700
method entries on 32-bit MCUs.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-20 08:26:05 +09:00
parent ce8ce3f96d
commit 0fab703028
34 changed files with 119 additions and 176 deletions
+1 -1
View File
@@ -3508,7 +3508,7 @@ mrb_encoding(mrb_state *mrb, mrb_value self)
}
/* ---------------------------*/
static mrb_mt_entry string_rom_entries[] = {
static const mrb_mt_entry string_rom_entries[] = {
MRB_MT_ENTRY(mrb_str_bytesize, MRB_SYM(bytesize), MRB_MT_FUNC|MRB_MT_NOARG),
MRB_MT_ENTRY(mrb_str_cmp_m, MRB_OPSYM(cmp), MRB_MT_FUNC),
MRB_MT_ENTRY(mrb_str_equal_m, MRB_OPSYM(eq), MRB_MT_FUNC),