From: "shia (Sangyong Sim) via ruby-core" Date: 2026-11-12T14:12:16+00:00 Subject: [ruby-core:127449] [Ruby Feature#22437] Speed up creation and basic operations of Struct / Data Issue #22437 has been updated by shia (Sangyong Sim). This is an interim report about iseq approach experiment. ## Background * For now, I only look at Struct. I am trying to build the `initialize` of classes made by `Struct.new(...)` (`keyword_init` nil/true/false) directly as ISEQs. * I tried three directions. Experiment A is a fast path only for keyword calls. Experiment B is a full rewrite. Experiment C is from ko1's idea (one ISEQ for each call shape). All numbers are ns per call. I built master and each experiment branch on the same machine and ran the same script twice, and I show the smaller result. Each run is the minimum of 5 x 1,000,000 calls, and each number also includes about 20ns of benchmark overhead (calling the lambda that wraps the call). `S8` is a Struct with 8 members. The `[class]` rows are plain classes for reference. Their `initialize` takes optional positional or keyword arguments and sets ivars (`C8`/`KC8` take 8 arguments). They do not depend on the patch, so I show only the master value. ## Experiment A: fast path only for keyword calls Code: https://github.com/riseshia/ruby/compare/master...struct-initialize-iseq-kwonly?expand=1 Initializing with keyword arguments is the slowest path. To keep the change small, I first changed only this path. When a call uses only keywords and every key is a member, it goes to a per-class ISEQ `initialize(a: nil, b: nil)`. All other calls go to the existing C implementation. Keyword calls no longer create a Hash, and the JITs also get faster. | call | interp master | interp A | YJIT master | YJIT A | ZJIT master | ZJIT A | |---|---:|---:|---:|---:|---:|---:| | `S.new(1, 2)` | 66 | 71 | 54 | 52 | 39 | 38 | | `S.new(1)` | 66 | 70 | 54 | 51 | 39 | 38 | | `S.new(a: 1, b: 2)` | 169 | 105 | 155 | 40 | 173 | 39 | | `S.new(1, b: 2)` | 96 | 103 | 90 | 81 | 94 | 87 | | `S.new(**h)` | 159 | 156 | 167 | 141 | 149 | 137 | | `S.new(*ary)` | 74 | 74 | 55 | 54 | 65 | 60 | | `S[1, 2]` | 86 | 86 | 73 | 72 | 70 | 71 | | `S8.new(1, 2, ..., 8)` | 87 | 85 | 66 | 59 | 43 | 43 | | `S8.new(a: 1, ..., h: 8)` | 388 | 215 | 372 | 48 | 371 | 46 | | `[class] C.new(1, 2)` | 51 | - | 29 | - | 25 | - | | `[class] KC.new(a: 1, b: 2)` | 63 | - | 28 | - | 25 | - | | `[class] C8.new(1, 2, ..., 8)` | 86 | - | 35 | - | 32 | - | | `[class] KC8.new(a: 1, ..., h: 8)` | 114 | - | 35 | - | 32 | - | Calls that mix positional and keyword arguments (`S.new(1, b: 2)` means `b = {b: 2}`) can simply go to the C implementation, so I did not need to add anything for them. The hard part was the dispatch: Struct#initialize has to send each call to one of two places by its shape, and I had to add this to the VM, YJIT, and ZJIT. We end up with two implementations (C and ISEQ) that do the same job, so I feel it mostly adds maintenance cost. ## Experiment B: full rewrite Code: https://github.com/riseshia/ruby/compare/master...struct-initialize-iseq-full?expand=1 After A, I tried rewriting everything as ISEQ, the simplest approach, to see how it performs. This rewrites the C implementation as a single ISEQ. It has to accept every call, so the signature becomes `initialize(*args, **kw)`. Inside it (`struct.rb`, about 70 lines), it checks the C rules again, including mixed calls and String/Integer keys. | call | interp master | interp B | YJIT master | YJIT B | ZJIT master | ZJIT B | |---|---:|---:|---:|---:|---:|---:| | `S.new(1, 2)` | 66 | 196 | 54 | 83 | 39 | 111 | | `S.new(1)` | 66 | 199 | 54 | 80 | 39 | 114 | | `S.new(a: 1, b: 2)` | 169 | 382 | 155 | 231 | 173 | 241 | | `S.new(1, b: 2)` | 96 | 232 | 90 | 151 | 94 | 154 | | `S.new(**h)` | 159 | 355 | 167 | 210 | 149 | 216 | | `S.new(*ary)` | 74 | 198 | 55 | 116 | 65 | 121 | | `S[1, 2]` | 86 | 235 | 73 | 134 | 70 | 148 | | `S8.new(1, 2, ..., 8)` | 87 | 360 | 66 | 115 | 43 | 150 | | `S8.new(a: 1, ..., h: 8)` | 388 | 925 | 372 | 486 | 371 | 531 | | `[class] C.new(1, 2)` | 51 | - | 29 | - | 25 | - | | `[class] KC.new(a: 1, b: 2)` | 63 | - | 28 | - | 25 | - | | `[class] C8.new(1, 2, ..., 8)` | 86 | - | 35 | - | 32 | - | | `[class] KC8.new(a: 1, ..., h: 8)` | 114 | - | 35 | - | 32 | - | Allocations changed: `S.new(1, 2)`, `S.new(1)`, `S.new(*ary)`, `S[1, 2]` and `S8.new(1, 2, ..., 8)` go from 1 to 3. `S.new(a: 1, b: 2)`, `S.new(**h)` and `S8.new(a: 1, ..., h: 8)` go from 2 to 4. `S.new(1, b: 2)` goes from 2 to 3. `*args` and `**kw` create a new Array and a new Hash on every call. Every call gets slower, including keyword calls (2-3x in the interpreter). The cause is the generic signature that must accept every call. In the interpreter, even a positional call has to go through argument setup that builds a rest Array and a keyword Hash, and keyword calls also build a Hash. Neither YJIT nor ZJIT can optimize a `*rest` + `**kwrest` signature well. ## Experiment C: one ISEQ for each call shape (ko1's idea) Code: https://github.com/riseshia/ruby/compare/master...struct-initialize-iseq?expand=1 I built this from ko1's idea (look at the shape of the call and call a body that fits it). At `Struct.new` time, each class gets a positional ISEQ `initialize(m0 = nil, ...)`, a keyword ISEQ `initialize(m0: nil, ...)`, and ISEQs for `S[]`. Struct#initialize becomes a dispatcher with no body. It looks at the call shape and sends the call to one of them. All other shapes (String/Integer keys, unknown keys, too many arguments, and so on) go to one generic ISEQ (`struct.rb`) that all classes share. One keyword ISEQ accepts every combination of keys, so the number of ISEQs per class is fixed, and the structure stays simple. A mixed call goes to the positional ISEQ, and the VM packs the keywords into a Hash, so the current behavior stays the same. Struct members are not ivars. They live in internal slots, so the ISEQ needs a way to write to them. At first I called a hidden setter for each member with send, but that cost 10-12ns per member in the interpreter. So I added a `setstructmember` instruction that writes to the slot directly. Disclaimer: I don't know YJIT/ZJIT deeply, so the explanations of the JIT optimizations were written with a lot of help from Claude ;) I also had to rework an existing optimization. On master, Struct#initialize is a C function, so ZJIT has a special rule just for it (inline_struct_initialize): instead of calling the C function, it writes each member directly into the slot (StoreField). With experiment C, initialize is an ISEQ, so this special rule is no longer used, and I removed it. Instead, ZJIT does two things: 1. At compile time, it looks at the call shape, picks the target ISEQ like the dispatcher does, and compiles it as a normal ISEQ call. 2. It turns setstructmember inside that ISEQ into the same StoreField. So ZJIT ends up with the same code as before. This is why ZJIT S.new(1, 2) is 39ns on both master and the final build. The columns in the tables mean: * **idea only:** only the dispatcher, the per-class ISEQs, and the generic ISEQ. YJIT gives up compiling when it sees the dispatcher, and ZJIT handles it with a dynamic send. * **+JIT:** "idea only" plus JIT optimizations. On the VM side, it only adds one thing: on the path without a call site (`vm_call0`), it picks the positional ISEQ by the number of arguments. * YJIT: picks the target ISEQ from the call shape at compile time and compiles it as a normal ISEQ call. It can also compile a call that passes keywords as a Hash to an ISEQ without keyword parameters (this is for mixed calls, but it is a general improvement not tied to Struct). * ZJIT: also picks the target ISEQ and compiles it as a normal ISEQ call. * **final:** "+JIT" plus VM changes and the matching JIT support. * VM: in the interpreter, the target of a positional call is fixed in the call cache. Adds `setstructmember`. Looks at `*ary` and `**h` at run time and sends them to the positional or keyword ISEQ. Adds the `S[]` dispatcher and the `newstruct` instruction (allocate and fill right away). * YJIT: compiles `setstructmember` to a class guard + frozen check + store, and `newstruct` to a C function call. For `*ary`, it picks the target from the array at compile time. For `S[]`, it picks the target and watches for redefinition of initialize. * ZJIT: lowers `setstructmember` to `StoreField`, and `newstruct` to `ObjectAlloc` + `StoreField`. For `S[]`, it picks the target and watches for redefinition of initialize. ### Interpreter | call | master | idea only | +JIT | final | |---|---:|---:|---:|---:| | `S.new(1, 2)` | 66 | 101 | 99 | 74 | | `S.new(1)` | 66 | 100 | 99 | 80 | | `S.new(a: 1, b: 2)` | 169 | 115 | 110 | 102 | | `S.new(1, b: 2)` | 96 | 129 | 132 | 100 | | `S.new(**h)` | 159 | 379 | 370 | 157 | | `S.new(*ary)` | 74 | 216 | 216 | 77 | | `S[1, 2]` | 86 | 242 | 129 | 71 | | `S8.new(1, 2, ..., 8)` | 87 | 156 | 159 | 107 | | `S8.new(a: 1, ..., h: 8)` | 388 | 228 | 227 | 193 | | `[class] C.new(1, 2)` | 51 | - | - | - | | `[class] KC.new(a: 1, b: 2)` | 63 | - | - | - | | `[class] C8.new(1, 2, ..., 8)` | 86 | - | - | - | | `[class] KC8.new(a: 1, ..., h: 8)` | 114 | - | - | - | ### YJIT | call | master | idea only | +JIT | final | |---|---:|---:|---:|---:| | `S.new(1, 2)` | 54 | 85 | 42 | 40 | | `S.new(1)` | 54 | 85 | 43 | 39 | | `S.new(a: 1, b: 2)` | 155 | 98 | 42 | 40 | | `S.new(1, b: 2)` | 90 | 112 | 66 | 64 | | `S.new(**h)` | 167 | 224 | 225 | 146 | | `S.new(*ary)` | 55 | 122 | 129 | 42 | | `S[1, 2]` | 73 | 140 | 114 | 41 | | `S8.new(1, 2, ..., 8)` | 66 | 94 | 50 | 48 | | `S8.new(a: 1, ..., h: 8)` | 372 | 162 | 49 | 48 | | `[class] C.new(1, 2)` | 29 | - | - | - | | `[class] KC.new(a: 1, b: 2)` | 28 | - | - | - | | `[class] C8.new(1, 2, ..., 8)` | 35 | - | - | - | | `[class] KC8.new(a: 1, ..., h: 8)` | 35 | - | - | - | ### ZJIT | call | master | idea only | +JIT | final | |---|---:|---:|---:|---:| | `S.new(1, 2)` | 39 | 82 | 43 | 39 | | `S.new(1)` | 39 | 81 | 42 | 38 | | `S.new(a: 1, b: 2)` | 173 | 92 | 41 | 38 | | `S.new(1, b: 2)` | 94 | 110 | 55 | 53 | | `S.new(**h)` | 149 | 222 | 236 | 141 | | `S.new(*ary)` | 65 | 125 | 130 | 67 | | `S[1, 2]` | 70 | 156 | 109 | 39 | | `S8.new(1, 2, ..., 8)` | 43 | 90 | 48 | 47 | | `S8.new(a: 1, ..., h: 8)` | 371 | 159 | 49 | 46 | | `[class] C.new(1, 2)` | 25 | - | - | - | | `[class] KC.new(a: 1, b: 2)` | 25 | - | - | - | | `[class] C8.new(1, 2, ..., 8)` | 32 | - | - | - | | `[class] KC8.new(a: 1, ..., h: 8)` | 32 | - | - | - | "idea only" and "+JIT" are intermediate builds that are not kept as commits on the branch, so I measured them separately before (only the `S8` rows were measured together this time). Allocations changed (final): `S.new(a: 1, b: 2)`, `S.new(**h)` and `S8.new(a: 1, ..., h: 8)` go from 2 to 1. With the final build, all of test/ruby and spec/ruby pass with 0 failures and 0 errors in the interpreter, YJIT, and ZJIT. ## Overall impressions If we go this way, almost the same approach should work for Data too, so I think experiment C is promising. But it needs many changes, and some of them may not be acceptable. I also have some doubt whether this can be maintained as it is. So for now, I think it is better to make things a bit faster with small, safe optimizations, and if we go this way, we should make a long-term plan for it. I think these changes (all added by the final version of experiment C) may be controversial: * Two new instructions: `setstructmember idx` (store into a member slot) and `newstruct n` (allocate and fill, without going through `new` or initialize). About `setstructmember`: I also wonder if we could keep members as ivars instead of dedicated slots. Then the per-class ISEQs could use `setinstancevariable` like a normal class, this instruction would not be needed, and the JITs could use their well-tuned ivar paths as they are. But that is a big change that touches the RStruct memory layout, C APIs like `RSTRUCT_*`, Marshal, index access (`s[0]`, `to_a`, and so on), and shape stability, so I left it out of scope this time. * Two new optimized method types: `STRUCT_INIT` (Struct#initialize) and `STRUCT_NEW` (`S[]`). * A builtin `struct.rb` (the generic ISEQ). If a user redefines a core method like `Hash#keys`, the behavior of the generic path changes. The C implementation did not have this property. * The per-class ISEQ bodies are new internal method entries. They are not in the method table, so S.instance_methods and the like look the same as on master. For now, they are kept in a hidden ivar of the class, like `__members__`. A better place for them may be needed. (maybe #note-8 solve this?) * TracePoint: initialize changes from `c_call` to `call`. This is already in the list of incompatibilities in the ticket description. One more thing: If mixed args calls (e.g. S.new(1, b: 2)) could be dropped, the design would get much simpler: C could drop its special path for mixed args calls. But dropping them is an incompatible and arguable change, so it would need a plan over several years. ---------------------------------------- Feature #22437: Speed up creation and basic operations of Struct / Data https://bugs.ruby-lang.org/issues/22437#change-119901 * Author: shia (Sangyong Sim) * Status: Open ---------------------------------------- ## Summary Struct and Data are slower to create and to operate on than a hand-written class that does the same job. Struct / Data are usually chosen because people expect them to be lightweight value objects, yet once they sit on a hot path they turn out to be slow, and the code ends up being rewritten as a plain class. I think the standard value objects should be chosen for their original reasons, such as readability and immutability, rather than avoided for performance. This ticket proposes to narrow that gap. ## Approach The experimental branch below is the result of asking Claude Opus 5.5 to speed things up under the following constraints: - Do not change any interface - Do not add any fields - Do not change how stack traces look - Do not change any observable behavior So I do not intend to merge this branch as is. I will review the changes one by one, pick only the ones that are realistic and maintainable, and submit them as small PRs. ## Summary of the changes in the experimental branch - Hidden ivar lookups (`__members__` etc.) are cached in six file-static slots in `struct.c` (48 bytes per process), keyed by shape id. - `Struct#initialize` - Reads the member count from self. - Walks the keyword Hash without rb_ensure. - Instance creation of Data classes (`Point.new`, where `Point = Data.define(...)`) - Stores positional and keyword arguments directly. - Allocates the object already frozen. - `Data#with`: copies the existing values directly. - `Data#=`=, `#eql?` and Hash key lookup - Skip the recursion guard. A Data's members are fixed at creation, so the guard has nothing to protect. - Struct keeps the guard, because a member's method can modify and freeze the struct and then re-enter the same computation. - `to_h`, `deconstruct_keys`: fill the Hash directly. - `inspect`: reserves the buffer up front and stringifies built-in types directly. ## Benchmarks master d71692d49f (2026-10-12) vs. the experimental branch, on x86_64 Linux. | | No JIT | YJIT | ZJIT | |---|---|---|---| | Creation, Struct (geometric mean) | 1.48x | 1.60x | 1.32x | | Creation, Data (geometric mean) | 3.90x | 4.74x | 4.26x | | Basic operations (geometric mean) | 2.37x | 3.28x | 2.87x | "Basic operations" are five frequently used methods: `Data#with`, `Struct#==`, Hash lookup with a Struct key, `Struct#to_h` and `Struct#inspect`. Selected cases (no JIT, ns/op; `S = Struct.new(:a, :b, :c)`, `D = Data.define(:a, :b, :c)`): - `D.new(1, 2, 3)`: 214 ��� 39 - `S.new(1, 2, 3)`: 51 ��� 34 - `Data#with`: 334 ��� 86 - `inspect`: 449 ��� 166 - Hash lookup with a Data key: 250 ��� 85 ruby/ruby-bench (ms; no JIT / YJIT / ZJIT): - struct-new-no-escape: 72���47 / 36���24 / 17���12 - The same benchmark rewritten with Data: 180���60 / 150���29 / 139���24 ## Compatibility checks (on the experimental branch) - 47 checks comparing observable behavior against master, run with no JIT / YJIT / ZJIT: all identical. They cover backtraces, TracePoint, overridden methods, re-entry and frozen state. - All of test/ruby in 4 modes: the same set of failures as master. - ruby/spec core/struct, data, hash, marshal, plus language/pattern_matching and keyword_arguments in 3 modes: 0 failures. ## Links - Experimental branch: https://github.com/riseshia/ruby/compare/d71692d49f...experiment/struct-data-core - Benchmark and check scripts: https://github.com/riseshia/ruby/tree/experiment/struct-new-observable-probe ## Note: the ISEQ initialize idea If incompatibilities were acceptable, another idea is to have `Struct.new` generate an ISEQ initialize for each class it defines, so that it gets the same optimizations as a plain class's initialize. I have not tested how far this could go, but a rough PoC showed the following incompatibilities: - Reflection on initialize changes: - owner changes from Struct to the generated class. - arity is no longer `-1`, and parameters is no longer `[[:rest]]`. - source_location is no longer nil. - `:initialize` appears in `private_instance_methods(false)`. - The TracePoint event changes from `:c_call` to `:call`. - When an exception is raised inside initialize, the backtrace frame changes from `Struct#initialize` to the generated method. Whether these incompatibilities are acceptable, and whether the speedup would be large enough to justify them, is left for further discussion. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/