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 <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-12-25 02:45:06 +09:00
parent 8a29ab591e
commit b841bd7439
+25
View File
@@ -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.