From 69cb701178ce9e3c74f36fc47f4e16b5ab7104eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Tue, 24 Nov 2026 19:21:35 +0100 Subject: [PATCH] Avoid quadratic use-list copies when splitting off the cold part of a bundle `try_split_off_cold` cuts each live range at every boundary between hot and cold blocks. At each cut, it copied all the uses after the cut into a new list for the rest of the range, and truncated the list of the current piece. Truncating does not release memory, so each piece kept a buffer sized for all the uses that remained when it was created. The remaining uses were also scanned at each cut, to find the first use after the cut and to compute the spill weight of the rest of the range. A range with U uses cut into P pieces therefore took O(U * P) time and memory. This happens when a value is live across most of a large function with many cold blocks. In Wasmtime, compiling a module with 4000 GC global initializers went from 4.4 GB to 360 MB, and compiling the wasm_of_ocaml build of js_of_ocaml (3.4 MB of Wasm) went from more than 11 GB to 1.2 GB. Now the range's use list is walked once: the first piece keeps the list, truncated to its own uses, and each later piece gets a copy of only its own uses. The allocation results are unchanged: the old and new code give identical output on random ion fuzz inputs and on Wasmtime compiles of about 30 large modules (wasm_of_ocaml, C/C++ and .NET programs). --- src/ion/process.rs | 51 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/ion/process.rs b/src/ion/process.rs index 7bd14479..aea231c2 100644 --- a/src/ion/process.rs +++ b/src/ion/process.rs @@ -844,7 +844,13 @@ impl<'a, F: Function> Env<'a, F> { for entry in &ranges { let end = entry.range.to; - let mut lr = entry.index; + let first_lr = entry.index; + let mut lr = first_lr; + // Walk the range's sorted use list once. The first piece keeps the + // list, truncated to its own uses at the end; each later piece gets + // a copy of its own uses. + let mut next_use = 0; + let mut first_piece_uses = 0; loop { let from = self.ctx.ranges[lr].range.from; let block = self.ctx.cfginfo.insn_block[from.inst().index()].index(); @@ -869,7 +875,31 @@ impl<'a, F: Function> Env<'a, F> { None }; - // Split everything after the run off into a new LR. + // Find the uses of this piece: those before the cut, or all + // remaining uses if this is the last piece. + let first_use = next_use; + let uses = &self.ctx.ranges[first_lr].uses; + next_use = match cut { + Some(cut) => { + first_use + uses[first_use..].iter().take_while(|u| u.pos < cut).count() + } + None => uses.len(), + }; + if lr == first_lr { + first_piece_uses = next_use; + } else { + let mut piece_uses = UseList::new_in(self.ctx.bump()); + piece_uses.extend( + self.ctx.ranges[first_lr].uses[first_use..next_use] + .iter() + .cloned(), + ); + self.ctx.ranges[lr].uses = piece_uses; + self.recompute_range_properties(lr); + } + + // Split everything after the run off into a new LR. Its uses + // are assigned on the next iteration. let rest = cut.map(|cut| { let new_lr = self.ctx.ranges.add( CodeRange { from: cut, to: end }, @@ -877,18 +907,7 @@ impl<'a, F: Function> Env<'a, F> { ); let vreg = self.ctx.ranges[lr].vreg; self.ctx.ranges[new_lr].vreg = vreg; - let first_use = self.ctx.ranges[lr] - .uses - .iter() - .position(|u| u.pos >= cut) - .unwrap_or(self.ctx.ranges[lr].uses.len()); - let mut rest_uses = UseList::new_in(self.ctx.bump()); - rest_uses.extend(self.ctx.ranges[lr].uses.iter().cloned().skip(first_use)); - self.ctx.ranges[new_lr].uses = rest_uses; - self.ctx.ranges[lr].uses.truncate(first_use); self.ctx.ranges[lr].range.to = cut; - self.recompute_range_properties(lr); - self.recompute_range_properties(new_lr); self.ctx.vregs[vreg].ranges.push(LiveRangeListEntry { range: self.ctx.ranges[new_lr].range, index: new_lr, @@ -911,6 +930,12 @@ impl<'a, F: Function> Env<'a, F> { None => break, } } + + // If the range was cut, its first piece keeps only its own uses. + if lr != first_lr { + self.ctx.ranges[first_lr].uses.truncate(first_piece_uses); + self.recompute_range_properties(first_lr); + } } debug_assert!(!hot.is_empty() && !cold.is_empty());