Skip to content

Cranelift: use a BTreeMap for JITModule code ranges#13710

Merged
cfallin merged 1 commit into
bytecodealliance:mainfrom
swedishembedded:cranelift-jit-finalize-btreemap
Jun 28, 2026
Merged

Cranelift: use a BTreeMap for JITModule code ranges#13710
cfallin merged 1 commit into
bytecodealliance:mainfrom
swedishembedded:cranelift-jit-finalize-btreemap

Conversation

@mkschreder

Copy link
Copy Markdown
Contributor

Let's make cranelift a little faster when booting JITed linux :)

Currently JITModule resolves a PC back to its defining function (for exception unwinding) via a Vec<(start, end, FuncId)> that was re-sorted in full on every finalize_definitions call. 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 BTreeMap keyed 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 mkschreder requested a review from a team as a code owner June 22, 2026 20:44
@mkschreder mkschreder requested review from cfallin and removed request for a team June 22, 2026 20:44
@github-actions github-actions Bot added the cranelift Issues related to the Cranelift code generator label Jun 22, 2026

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cranelift/jit/src/backend.rs Outdated
Comment thread cranelift/jit/src/backend.rs Outdated
@mkschreder mkschreder force-pushed the cranelift-jit-finalize-btreemap branch 2 times, most recently from 4de0155 to eef6f2c Compare June 26, 2026 09:02
@mkschreder mkschreder requested a review from cfallin June 26, 2026 14:37
@mkschreder mkschreder force-pushed the cranelift-jit-finalize-btreemap branch from eef6f2c to a24d90c Compare June 28, 2026 12:45
`JITModule` resolves a PC back to its defining function (for exception
unwinding) via a `Vec<(start, end, FuncId)>` that was re-sorted in full on
every `finalize_definitions` call. Code that defines and finalizes functions
one at a time therefore paid an O(n) sort per finalize, i.e. O(n^2) overall.

Replace the vector with a `BTreeMap` keyed 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.
@mkschreder mkschreder force-pushed the cranelift-jit-finalize-btreemap branch from a24d90c to 5f2f2a4 Compare June 28, 2026 16:23
@cfallin cfallin added this pull request to the merge queue Jun 28, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 28, 2026
@cfallin cfallin added this pull request to the merge queue Jun 28, 2026
Merged via the queue into bytecodealliance:main with commit 08c456e Jun 28, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants