class.c: use tombstone for remove_method with ROM method tables

Previously, removing a ROM method required flattening all chain layers
into a single mutable table. This was O(n) and allocated RAM for all
previously-ROM methods.

Use a tombstone marker (MT_FUNC flag with func=NULL) instead. The
mt_get() lookup treats this as "not found" and stops the chain walk,
hiding the ROM entry while allowing superclass lookup.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-02-19 14:57:50 +09:00
parent 16fb8a6b08
commit 494d55c9f7
3 changed files with 56 additions and 55 deletions
+7
View File
@@ -49,6 +49,13 @@ typedef struct mt_tbl {
#define MT_PUBLIC 0 /* MRB_METHOD_PUBLIC_FL */
#define MT_PRIVATE 1 /* MRB_METHOD_PRIVATE_FL */
/* "removed" tombstone: MT_FUNC flag set with NULL function pointer.
This combination never occurs naturally (C functions are never NULL).
Unlike undef (proc=NULL without MT_FUNC), a removed marker makes
mt_get() return 0 ("not found"), blocking ROM chain walk while
allowing superclass lookup. */
#define MT_REMOVED_P(key, val) (((key)&MT_FUNC) && (val).func==NULL)
void mrb_mt_init_rom(struct RClass *c, mt_tbl *rom);
#endif