Repository navigation
feat: let a closure decide the order functions are emitted in - #342
logan-gatlin wants to merge 3 commits into
Conversation
|
I don't think function ordering effects performance for cloudflare workers because they don't do streaming instantiation. Order matters when a large module is being streamed to the client, and the client starts compiling / executing the module before its fully downloaded. In these cases I think most-used-first is still the right call. The functions likely to be executed first will arrive first. |
ce98fc5 to
81f029f
Compare
|
I'm okay with reordering, but it would help to have a very clear framing for the benefit. Perhaps streaming instantiation would benefit from a call graph ordering of sorts instead? Are there any other benefits of using small numbers for common functions? If we do land this, we should make a clear note that the tests are not there to lock this behaviour in, to leave future reorderings on the table. |
|
Yeah most-used-first is pretty much just a cruder version of call-graph ordering, and I'd bet the latter is actually faster if the difference is measurable at all. I'm leaning towards making function order configurable instead. My tool wants most-used-first just for the size reduction, but other users may want something different. Will do some benchmarks and file a new PR or update this one |
81f029f to
ba6d385
Compare
There was a problem hiding this comment.
Since my last review the PR was only rebased; the changelog entry now sits next to the base branch's new entries, and the code and tests are unchanged, so I still have no code concerns. What's new is the discussion: @guybedford (core) asked that, if this lands, it say clearly that the tests do not lock in this order, so it can be reordered later. Nothing in the PR says that yet. @logan-gatlin has also said they lean towards making function order configurable and may update this PR or open a new one, so the design isn't final. On the question "are there any other benefits of using small numbers for common functions?": as far as I can tell the only benefit is encoded size, since every call/return_call/ref.func, export, element entry and start index that drops below 128 saves a byte, and I know of no engine that treats low function indices differently.
Not merge-ready:
- The core member's request for a note that the tests do not lock in this function order hasn't been addressed.
- The author has said they may rework this into a configurable ordering, so the design isn't settled yet.
walrus emits defined functions, and so numbers them, largest first. That suits engines that compile functions in parallel, but a size optimizer wants the most-referenced functions first, so that their indices take one LEB128 byte, and streaming instantiation may want call-graph order. `ModuleConfig::function_order` takes a closure that is given every defined function as a `FunctionInfo` (its id, size and how many times its index is written), in the default order, to reorder in place. `FunctionInfo` can't be built, cloned or replaced outside walrus, so the closure can only permute the functions. Walrus calls it once per emit, and both the function and the code section use the order it leaves. The default order is unchanged, and documented as not guaranteed. Use counts are only gathered when a closure is set.
ba6d385 to
66752ef
Compare
Rather than collecting every called function id into each function's EmitFacts and merging them afterwards, the parallel body walks increment one AtomicU64 per function arena slot. The table is only allocated when a function_order closure is set.
There was a problem hiding this comment.
Since my last review the PR changed design. The default order (largest first) is no longer changed. Instead, ModuleConfig::function_order takes a closure that reorders FunctionInfo { id, size, uses } in place, and c8106f1 counts code uses in a shared table of AtomicU64s, as @guybedford suggested in his thread. That thread is his to resolve. My earlier concern about locking in the default order is fixed: the docs, changelog and function_order.rs now say the default isn't guaranteed. The design and tests look solid. One problem remains: uses() never counts ref.func in table initializer expressions, even though those indices are emitted.
Not merge-ready:
FunctionInfo::usesnever countsref.funcin table initializer expressions, even though they are emitted, so documented counts are wrong for those functions.
ModuleTables::emit writes a table's init expression, so a ref.func there writes a function index, but FunctionInfo::uses didn't count it.
guybedford
left a comment
There was a problem hiding this comment.
The atomics change and the table initializer fix both look good. Just a couple of small comment nits; this needs a rebase onto main before landing.
| /// How many times the emitted module writes this function's index: in | ||
| /// `call`, `return_call` and `ref.func` instructions, exports, element | ||
| /// segments, global and table initializers and the start section. The name section | ||
| /// and DWARF aren't counted. |
There was a problem hiding this comment.
This line is now past the wrap width, and custom sections in general aren't counted, not just these two:
| /// How many times the emitted module writes this function's index: in | |
| /// `call`, `return_call` and `ref.func` instructions, exports, element | |
| /// segments, global and table initializers and the start section. The name section | |
| /// and DWARF aren't counted. | |
| /// How many times the emitted module writes this function's index: in | |
| /// `call`, `return_call` and `ref.func` instructions, exports, element | |
| /// segments, global and table initializers and the start section. Custom | |
| /// sections, including the name section and DWARF, aren't counted. |
| @@ -728,7 +861,7 @@ impl Emit for ModuleFunctions { | |||
| #[cfg(not(feature = "parallel"))] | |||
There was a problem hiding this comment.
The comment just above this hunk (line 856) says "functions is sorted largest first", which is now only true by default. Something like "functions is sorted largest first by default" would keep it accurate.
Makes the order defined functions are emitted in configurable, instead of changing the default, as discussed above.
ModuleConfig::function_ordertakes a closure that reorders the defined functions in place. Each one is aFunctionInfowithid(),size()anduses(), which is how many times the module writes its index. For example, to give the most-referenced functions one-byte indices:FunctionInfocan't be built or cloned outside walrus, so the closure can only reorder the functions.New tests are in
crates/tests/tests/function_order.rs.