Truly independent web browser
LibJS: Drop the sites of dead JIT code on a compile thread
Andreas Klingac8ceab21dcf
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rsindex 8d9ea3dfaf6..3ac7dfb0a65 100644--- a/Libraries/LibJS/Rust/src/jit/code.rs+++ b/Libraries/LibJS/Rust/src/jit/code.rs@@ -229,6 +229,11 @@ impl JitCode {&self.sites[index as usize]}+ /// Takes the sites of code that died, so that they can be dropped elsewhere.+ pub fn take_sites(&mut self) -> Vec<Site> {+ core::mem::take(&mut self.sites)+ }+pub fn snapshot_executable(&self, index: u32) -> SnapshotExecutable {self.snapshot_executables[index as usize]}@@ -349,9 +354,18 @@ impl Executable {/// Frees the slot of the executable, which died, and drops its JIT code.pub fn free_jit_entry_slot(&self, vm: &Vm) {- if let Some(table) = vm.jit.entry_table.as_ref() {- table.free(self.head.jit_entry_slot.get(), core::ptr::from_ref(&self.head) as u64);+ let Some(table) = vm.jit.entry_table.as_ref() else {+ return;+ };+ let Some(mut code) = table.free(self.head.jit_entry_slot.get(), core::ptr::from_ref(&self.head) as u64) else {+ return;+ };+ // OPTIMIZATION: The compile threads allocated the sites, so one of them frees them, outside the collection+ // this runs in.+ if let Some(queue) = vm.jit.started_compile_queue() {+ queue.drop_sites(code.take_sites());}+ drop(code);}/// The executable's attached JIT code, if any.diff --git a/Libraries/LibJS/Rust/src/jit/compile_queue.rs b/Libraries/LibJS/Rust/src/jit/compile_queue.rsindex e2da5edbc60..0518a07ba54 100644--- a/Libraries/LibJS/Rust/src/jit/compile_queue.rs+++ b/Libraries/LibJS/Rust/src/jit/compile_queue.rs@@ -10,22 +10,30 @@ use std::sync::mpsc::{Receiver, Sender, channel};use std::sync::{Arc, Mutex};use libjs_jit::CompileFailure;-use libjs_jit::code::CompiledCode;+use libjs_jit::code::{CompiledCode, Site};use libjs_jit::snapshot::Snapshot;pub type CompileResult = Result<CompiledCode, CompileFailure>;+/// What the compile threads do: compile a snapshot, or drop what dead code no longer needs.+enum Job {+ Compile(u64, Box<Snapshot>),+ /// The sites of code that died. The compile threads allocated them, so they free them faster than the thread that+ /// collects the code, and outside its collection.+ Drop(Vec<Site>),+}+/// Threads that compile the snapshots they are sent, each taking the oldest job no other thread took yet. Jobs finish/// in any order. Dropping the queue lets the threads end once they are done with the jobs they compile; the results of/// abandoned jobs are dropped.pub struct CompileQueue {- jobs: Sender<(u64, Box<Snapshot>)>,+ jobs: Sender<Job>,results: Receiver<(u64, CompileResult)>,}impl CompileQueue {pub fn start(thread_count: usize) -> Self {- let (jobs, job_receiver) = channel::<(u64, Box<Snapshot>)>();+ let (jobs, job_receiver) = channel::<Job>();let job_receiver = Arc::new(Mutex::new(job_receiver));let (result_sender, results) = channel();for index in 0..thread_count.max(1) {@@ -40,8 +48,13 @@ impl CompileQueue {.lock().expect("no compile thread panics while waiting").recv();- let Ok((id, snapshot)) = job else {- return;+ let (id, snapshot) = match job {+ Ok(Job::Compile(id, snapshot)) => (id, snapshot),+ Ok(Job::Drop(sites)) => {+ drop(sites);+ continue;+ }+ Err(_) => return,};let result = libjs_jit::compile(&snapshot);if result_sender.send((id, result)).is_err() {@@ -61,7 +74,14 @@ impl CompileQueue {pub fn submit(&self, id: u64, snapshot: Box<Snapshot>) {self.jobs- .send((id, snapshot))+ .send(Job::Compile(id, snapshot))+ .expect("the compile threads run while the queue lives");+ }++ /// Has a compile thread drop the sites of code that died.+ pub fn drop_sites(&self, sites: Vec<Site>) {+ self.jobs+ .send(Job::Drop(sites)).expect("the compile threads run while the queue lives");}diff --git a/Libraries/LibJS/Rust/src/jit/entry_table.rs b/Libraries/LibJS/Rust/src/jit/entry_table.rsindex 53440ff8924..149eef76db2 100644--- a/Libraries/LibJS/Rust/src/jit/entry_table.rs+++ b/Libraries/LibJS/Rust/src/jit/entry_table.rs@@ -148,20 +148,21 @@ impl JitEntryTable {slot}- /// Frees the slot of the executable whose head is at `owner`, which died, and drops its code.- pub fn free(&self, slot: u32, owner: u64) {+ /// Frees the slot of the executable whose head is at `owner`, which died, and returns its code.+ pub fn free(&self, slot: u32, owner: u64) -> Option<Box<JitCode>> {let slot = slot & JIT_ENTRY_SLOT_MASK;if slot == NO_JIT_ENTRY_SLOT {- return;+ return None;}assert!(self.is_owned_by(slot, owner),"only the owner of a JIT entry slot frees it");- drop(self.code_cell(slot).take());+ let code = self.code_cell(slot).take();self.entry_cell(slot).set(self.not_compiled_entry);self.owner_cell(slot).set(0);self.free_slots.borrow_mut().push(slot);+ code}/// Whether the slot belongs to the executable whose head is at `owner`.