Skip to content

Truly independent web browser

LibJS: Let functions with JIT code keep it when a bytecode cache comes

Andreas Kling873124cee4d3
diff --git a/Libraries/LibJS/Rust/src/bytecode/bytecode_cache.rs b/Libraries/LibJS/Rust/src/bytecode/bytecode_cache.rs
index 3629648b0a0..ca9b13a7e65 100644
--- a/Libraries/LibJS/Rust/src/bytecode/bytecode_cache.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/bytecode_cache.rs
@@ -10,7 +10,8 @@
//! The executables made from a blob run their bytecode in place in it and keep it alive. A function's executable stays
//! in the blob until the function is first called. Installing a blob into a running record matches every function the
//! record has created to a function of the blob, then gives each one that ran an executable from the blob, which takes
-//! over the inline caches of the one it replaces, and each one that did not its executable in the blob.
+//! over the inline caches, feedback and tier of the one it replaces, and each one that did not its executable in the
+//! blob. A function whose executable has JIT code keeps it.
use core::cell::{Ref, RefCell};
use std::collections::{HashMap, HashSet};
@@ -192,6 +193,8 @@ pub fn materialize_cached_function_executable(
enum Replacement {
CachedExecutable(DecodedCachedExecutableRecord),
Executable(Gc<Executable>),
+ /// The function keeps the executable it has, which has JIT code, or a compile on its way.
+ KeepExecutable,
}
/// What installing a blob gives one function, once every function turned out to have its counterpart in the blob.
@@ -276,6 +279,16 @@ impl<'vm, 'source> BytecodeCacheInstall<'vm, 'source> {
let existing_function = self.take_matching_function(function, outer_strict)?;
let replacement = match existing_function.executable() {
None => Replacement::CachedExecutable(function.cached_executable()),
+ // NB: JIT code is tied to the executable it was compiled for, and an executable from the blob would compile
+ // again from scratch, so a function with code keeps its executable. The functions it creates still
+ // match functions of the blob, and get what they would otherwise.
+ Some(existing_executable) if existing_executable.has_jit_code_or_compile() => {
+ let record = function.cached_executable().decode_executable()?;
+ for nested_function in record.functions()? {
+ self.prepare_function(&nested_function, record.is_strict())?;
+ }
+ Replacement::KeepExecutable
+ }
Some(existing_executable) => {
let record = function.cached_executable().decode_executable()?;
Replacement::Executable(self.prepare_executable(&record, Some(existing_executable))?)
@@ -320,6 +333,7 @@ impl<'vm, 'source> BytecodeCacheInstall<'vm, 'source> {
Replacement::Executable(executable) => install
.function
.install_bytecode_cache_executable(executable, &install.metadata),
+ Replacement::KeepExecutable => install.function.clear_compile_inputs(),
}
}
drop(self.new_executables);
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 2c644108ddb..55acf4bbd25 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -29,7 +29,6 @@ use crate::gc::root::MarkedVec;
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::jit::InterpreterTier;
-use crate::jit::code::CompileState;
use crate::jit::code::ExecutableJitState;
use crate::layout::buffer::InterpreterBuffer;
use crate::layout::cell::{CellHeader, Gc};
@@ -1823,11 +1822,11 @@ impl Executable {
matches!(self.bytecode, ExecutableBytecode::InBytecodeCacheBlob(_))
}
- /// Takes over the feedback and the tier of `other`, an executable with the same bytecode that this one replaces, so
- /// that a function that warmed up goes on from where it was rather than start over. Its JIT code is tied to the
- /// executable it was compiled for, so an executable that had code compiles again at its next tier-up check, from the
- /// feedback.
+ /// Takes over the feedback, the tier and the tier-up budget of `other`, an executable with the same bytecode that
+ /// this one replaces, so that a function that warmed up goes on from where it was rather than start over, and
+ /// whether the JIT refused it.
fn take_over_warm_up_from(executable: Gc<Executable>, other: &Executable) {
+ executable.take_over_jit_refusal_from(other);
let Some(other_feedback) = other.feedback() else {
return;
};
@@ -1836,12 +1835,7 @@ impl Executable {
}
executable.ensure_feedback().copy_from(other_feedback);
executable.set_interpreter_tier(other.interpreter_tier());
- let budget = if other.jit_compile_state() == CompileState::None {
- other.head.tier_up_budget.get()
- } else {
- 1
- };
- executable.head.tier_up_budget.set(budget);
+ executable.head.tier_up_budget.set(other.head.tier_up_budget.get());
}
/// Executable::copy_runtime_caches_from(): makes `executable` take over what the inline caches of `other`, an
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rs
index 0597a4bcfd4..bbcc9a62151 100644
--- a/Libraries/LibJS/Rust/src/jit/code.rs
+++ b/Libraries/LibJS/Rust/src/jit/code.rs
@@ -428,6 +428,25 @@ impl Executable {
self.head.jit_compile_state.set(state as u8);
}
+ /// Whether the executable has JIT code, or a compile on its way, which is tied to the executable.
+ pub fn has_jit_code_or_compile(&self) -> bool {
+ matches!(
+ self.jit_compile_state(),
+ CompileState::Queued | CompileState::Installed | CompileState::Discarding
+ )
+ }
+
+ /// Makes the executable never compile, with the tier and the tier-up budget of `other`, if `other` was refused,
+ /// as replacing an executable with one of the same bytecode does not change whether it can compile.
+ pub fn take_over_jit_refusal_from(&self, other: &Executable) {
+ if other.jit_compile_state() != CompileState::Refused {
+ return;
+ }
+ self.set_jit_compile_state(CompileState::Refused);
+ self.set_interpreter_tier(other.interpreter_tier());
+ self.head.tier_up_budget.set(other.head.tier_up_budget.get());
+ }
+
/// The executable's slot in the JIT entry table of `vm`, the VM whose heap the executable lives in, and the table,
/// if it has one.
fn jit_entry_table_slot<'vm>(&self, vm: &'vm Vm) -> Option<(&'vm JitEntryTable, u32)> {
diff --git a/Libraries/LibJS/Rust/src/jit/disabled.rs b/Libraries/LibJS/Rust/src/jit/disabled.rs
index 3a373d91054..ce698e38af5 100644
--- a/Libraries/LibJS/Rust/src/jit/disabled.rs
+++ b/Libraries/LibJS/Rust/src/jit/disabled.rs
@@ -121,6 +121,12 @@ pub mod code {
CompileState::None
}
+ pub fn has_jit_code_or_compile(&self) -> bool {
+ false
+ }
+
+ pub fn take_over_jit_refusal_from(&self, _other: &Executable) {}
+
pub fn discard_jit_code(&self, _vm: &Vm) {
unreachable!("no executable has JIT code");
}