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
+5 -3
View File
@@ -59,11 +59,13 @@ typedef struct mrb_mt_tbl {
#define MRB_MT_ENTRY(fn, sym, flags) \
{ { .func = (fn) }, (sym), (flags) }
/* ROM table initializer from entries array (auto-computes size) */
/* ROM table initializer from const entries array (auto-computes size).
Casts away const because mrb_mt_tbl.ptr is shared with mutable layers;
the MRB_MT_READONLY_BIT prevents writes. */
#define MRB_MT_ROM_TAB(entries) { \
(int)(sizeof(entries)/sizeof(entries[0])), \
(int)(sizeof(entries)/sizeof(entries[0])), \
(entries), NULL }
(int)(sizeof(entries)/sizeof(entries[0])) | MRB_MT_READONLY_BIT, \
(mrb_mt_entry*)(entries), NULL }
/* "removed" tombstone: MRB_MT_FUNC flag set with NULL function pointer.
This combination never occurs naturally (C functions are never NULL).