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:
- 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.
- 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.