From b841bd74395168bcc5bd62dd7d82592eb922adb5 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 25 Dec 2025 02:45:06 +0900 Subject: [PATCH] doc/internal/opcode.md: document OP_GETIDX/OP_SETIDX optimization Add notes section explaining the optimization behavior: - Which functions are used for direct access - When fallback to method dispatch occurs - Why subclasses can override []/[]= Co-authored-by: Claude --- doc/internal/opcode.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/doc/internal/opcode.md b/doc/internal/opcode.md index bef081c21..71710f05b 100644 --- a/doc/internal/opcode.md +++ b/doc/internal/opcode.md @@ -136,3 +136,28 @@ See also `OP_EXT1`, `OP_EXT2` and `OP_EXT3`. | 103 | `OP_EXT2` | `-` | `make 2nd operand (b) 16bit` | 104 | `OP_EXT3` | `-` | `make 1st and 2nd operands 16bit` | 105 | `OP_STOP` | `-` | `stop VM` + +## Notes + +### OP_GETIDX / OP_SETIDX Optimization + +These instructions optimize `[]` and `[]=` access for Array, Hash, and String. + +**OP_GETIDX** uses direct function calls: + +- `Array`: `mrb_ary_entry()` (integer index only) +- `Hash`: `mrb_hash_get()` +- `String`: `mrb_str_aref()` (integer/string/range index) + +**OP_SETIDX** uses direct function calls: + +- `Array`: `mrb_ary_set()` (integer index only) +- `Hash`: `mrb_hash_set()` + +**Fallback to method dispatch** occurs when: + +- Object is a subclass (e.g., `MyArray < Array`) +- Object has a singleton class (singleton methods defined) +- Index type is not supported (e.g., non-integer for Array) + +This allows subclasses to override `[]`/`[]=` while base classes remain optimized.