Skip to content

Truly independent web browser

LibJS: Keep the warm-up of functions that a bytecode cache replaces

Andreas Kling19c182a10d0a
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index f0a4a5e8655..2c644108ddb 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -29,6 +29,7 @@ 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};
@@ -1686,6 +1687,7 @@ impl Executable {
let executable = Self::create_from_parts(vm, executable);
if let Some(replaced_executable) = replaced_executable {
Self::copy_runtime_caches_from(executable, &replaced_executable);
+ Self::take_over_warm_up_from(executable, &replaced_executable);
}
drop(rooted_template_object_caches);
drop(rooted_constants);
@@ -1821,6 +1823,27 @@ 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.
+ fn take_over_warm_up_from(executable: Gc<Executable>, other: &Executable) {
+ let Some(other_feedback) = other.feedback() else {
+ return;
+ };
+ if executable.feedback_slot_counts != other.feedback_slot_counts {
+ return;
+ }
+ 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::copy_runtime_caches_from(): makes `executable` take over what the inline caches of `other`, an
/// executable compiled from the same code, learned, as installing a bytecode cache into a running program does.
/// Each kind of cache is only taken over if the two executables have the same number of them. The template object
diff --git a/Libraries/LibJS/Rust/src/bytecode/feedback.rs b/Libraries/LibJS/Rust/src/bytecode/feedback.rs
index 136f48d3f62..48f9821adc8 100644
--- a/Libraries/LibJS/Rust/src/bytecode/feedback.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/feedback.rs
@@ -25,7 +25,7 @@ use crate::layout::object::Object;
use crate::runtime::ecmascript_function_object::as_ecmascript_function_object;
/// How many feedback slots of each kind an executable's bytecode indexes into.
-#[derive(Clone, Copy, Default)]
+#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct FeedbackSlotCounts {
pub arith: u32,
pub value: u32,
@@ -79,6 +79,18 @@ impl ExecutableFeedback {
unsafe { core::slice::from_raw_parts(storage.data().add(offset).cast::<T>(), count as usize) }
}
+ /// Takes over what `other`, the feedback of an executable with the same bytecode, observed.
+ pub fn copy_from(&self, other: &ExecutableFeedback) {
+ assert!(self.counts == other.counts, "the feedback has the same slots");
+ if let (Some(storage), Some(other_storage)) = (&self.storage, &other.storage) {
+ // SAFETY: Both storages hold `layout.size` bytes laid out for the same counts, and are distinct.
+ unsafe { core::ptr::copy_nonoverlapping(other_storage.data(), storage.data(), self.layout.size) };
+ }
+ for (value, other_value) in self.value.iter().zip(&other.value) {
+ value.set(other_value.get());
+ }
+ }
+
pub fn arith(&self) -> &[Cell<u8>] {
self.array(0, self.counts.arith)
}