mkschreder opened PR #13710 from swedishembedded:cranelift-jit-finalize-btreemap to bytecodealliance:main:
Let's make cranelift a little faster when booting JITed linux :)
Currently
JITModuleresolves a PC back to its defining function (for exception unwinding) via aVec<(start, end, FuncId)>that was re-sorted in full on everyfinalize_definitionscall. Code that defines and finalizes functions one at a time therefore paid an O(n) sort per finalize, i.e. O(n^2) overall.I replace the vector with a
BTreeMapkeyed on the start address. Inserts stay sorted incrementally (O(log n)) and the lookup becomes an O(log n) range query, so finalizing is linear in the number of functions with no sort.Noticible speedup is observed without any (obvious) problems. I think the change makes sense and I don't see why it wouldn't simply just get rid of O(n^2) complexity without any other issues.
mkschreder requested cfallin for a review on PR #13710.
mkschreder requested wasmtime-compiler-reviewers for a review on PR #13710.
github-actions[bot] added the label cranelift on PR #13710.
:thumbs_up: cfallin submitted PR review:
Reasonable change and looks good -- thanks! (FWIW this is more or less how Wasmtime's address map data structures work so it's neat to see cranelift-jit have convergent evolution here.)
Just some style notes re: the comments below, then happy to merge.
:speech_balloon: cfallin created PR review comment:
No need for the detailed explainer here -- makes sense when justifying the change, but doesn't have to live in the source forever. Just the format note (map from start addr to end addr and function) is fine.
:speech_balloon: cfallin created PR review comment:
Likewise here -- comment only makes sense in context of the diff, no need to keep this forever.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
Thank you for your feedback. Don't you think though that the comment makes this choice much more obvious and potentially leads to similar choices being made more often in the future? I have the belief that the better code decisions are documented in place the better culture it creates over time because people (or machines) look at the comment like that and then realize that somewhere in a different place the same solution makes sense as well. Without the comment, it becomes an opaque decision burried in a git commit. Happy to remove the comments but I just think they do more good than bad.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
Well, no, I don't think we need this level of detail -- it describes the past implementation and its impact ("re-sort of the whole table ... which made ..."). Level of commenting is always a subjective call, and I am all for detailed comments where things are tricky, but a BTreeMap sorted by address is common enough throughout all of Wasmtime+Cranelift that we don't need this; rather, overly verbose comments like this look (in context of our local style) like an indication of something non-obvious, and take time to read and digest, which is a net negative for a straightforward idiom, IMHO. The comment describing what they keys and values are is enough.
Part of my thinking here is informed by recent LLM-isms too: they have a tendency to be extraordinarily verbose, and dump point-in-time thoughts in comments everywhere, which over time makes the code extremely difficult to read, ironically. So I think we need to apply some judgment and be a bit judicious in exchange.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
Done.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
@cfallin done.
mkschreder updated PR #13710.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
It looks like you deleted the entire comment. From above: "Just the format note (map from start addr to end addr and function) is fine."
Could you add back that note?
mkschreder updated PR #13710.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
Added back "// Search the sorted code-ranges for the PC."
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
Not sure what you mean by "note". Please paste the exact wording of the comment you want along with line number where it should be and I will update the commit.
mkschreder requested cfallin for a review on PR #13710.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
No, I did not ask for "// Search the sorted ..." to be added back in, and that is an incorrect comment now, since we do not have a sorted list. Please remove that.
Please add back the first half of the comment on the struct field. This is what I mean by "format note (map from start addr to end addr and function)". Your original wording is fine: "Map from a function's start address to its (end address, FuncId), used to resolve a PC back to its function for exception unwinding."
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
The map is implicitly sorted though, so I would say that the comment is still correct.
mkschreder updated PR #13710.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
Added the comment to struct member.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
@cfallin no offence, but I think we are nitpicking at this point :)
mkschreder updated PR #13710.
:memo: mkschreder submitted PR review.
:speech_balloon: mkschreder created PR review comment:
Done.
:thumbs_up: cfallin submitted PR review.
cfallin added PR #13710 Cranelift: use a BTreeMap for JITModule code ranges to the merge queue.
:memo: cfallin submitted PR review.
:speech_balloon: cfallin created PR review comment:
no offence, but I think we are nitpicking at this point :)
Well, no, just trying to get the comment I had originally asked for, no more, no less. Thanks for making the change!
github-merge-queue[bot] removed PR #13710 Cranelift: use a BTreeMap for JITModule code ranges from the merge queue.
cfallin added PR #13710 Cranelift: use a BTreeMap for JITModule code ranges to the merge queue.
:check: cfallin merged PR #13710.
cfallin removed PR #13710 Cranelift: use a BTreeMap for JITModule code ranges from the merge queue.
Last updated: Jul 29 2026 at 05:03 UTC