Skip to content

Truly independent web browser

LibJS: Count the collections an executable stays unentered for

Andreas Kling7b386ac7ac52
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index 7a31a22ee1d..a7ce635d14d 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -1654,38 +1654,42 @@ asm_handler_fallback_cold_end:
.p2align 4
asm_handler_Enter:
- ldr x2, [x28, #88]
- cbz x2, .Lasm_Enter.assert_predicate_failure_3
- ldr w1, [x2, #12]
- movz x3, #0x7ffb, lsl #48
- mov w0, #5
-.Lasm_Enter.ssa_block_3:
- add x4, x0, #1
- cmp x4, x1
- b.hs .Lasm_Enter.ssa_block_2
- add x10, x27, x0, lsl #3
- stp x3, x3, [x10]
- add x0, x0, #2
- b .Lasm_Enter.ssa_block_3
+ ldr x0, [x28, #88]
+ cbz x0, .Lasm_Enter.assert_predicate_failure_3
+ ldrb w9, [x0, #109]
+ cbz w9, .Lasm_Enter.ssa_block_2
+ strb wzr, [x0, #109]
.Lasm_Enter.ssa_block_2:
- cmp x0, x1
- b.hs .Lasm_Enter.ssa_block_5
- str x3, [x27, x0, lsl #3]
+ ldr w2, [x0, #12]
+ movz x3, #0x7ffb, lsl #48
+ mov w1, #5
.Lasm_Enter.ssa_block_5:
- ldr w3, [x2, #16]
- ldr x2, [x2, #32]
- subs x3, x3, x1
+ add x4, x1, #1
+ cmp x4, x2
+ b.hs .Lasm_Enter.ssa_block_4
+ add x10, x27, x1, lsl #3
+ stp x3, x3, [x10]
+ add x1, x1, #2
+ b .Lasm_Enter.ssa_block_5
+.Lasm_Enter.ssa_block_4:
+ cmp x1, x2
+ b.hs .Lasm_Enter.ssa_block_7
+ str x3, [x27, x1, lsl #3]
+.Lasm_Enter.ssa_block_7:
+ ldr w3, [x0, #16]
+ ldr x1, [x0, #32]
+ subs x3, x3, x2
mov x0, #0
-.Lasm_Enter.ssa_block_8:
+.Lasm_Enter.ssa_block_10:
cmp x0, x3
- b.hs .Lasm_Enter.ssa_block_7
- cbz x2, .Lasm_Enter.assert_predicate_failure_25
- ldr x4, [x2, x0, lsl #3]
- str x4, [x27, x1, lsl #3]
+ b.hs .Lasm_Enter.ssa_block_9
+ cbz x1, .Lasm_Enter.assert_predicate_failure_29
+ ldr x4, [x1, x0, lsl #3]
+ str x4, [x27, x2, lsl #3]
add x0, x0, #1
- add x1, x1, #1
- b .Lasm_Enter.ssa_block_8
-.Lasm_Enter.ssa_block_7:
+ add x2, x2, #1
+ b .Lasm_Enter.ssa_block_10
+.Lasm_Enter.ssa_block_9:
mov w9, #1
strb w9, [x28, #79]
ldrb w9, [x21, #1]
@@ -20916,7 +20920,7 @@ asm_handler_JumpLooselyInequalsLoopRhsInt32_cold_end:
asm_assertion_failure_traps:
.Lasm_Enter.assert_predicate_failure_3:
brk #0
-.Lasm_Enter.assert_predicate_failure_25:
+.Lasm_Enter.assert_predicate_failure_29:
brk #0
.Lasm_JumpIf.assert_predicate_failure_7:
brk #0
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index 0b9750b45e4..f7f01275f5c 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -1634,40 +1634,44 @@ asm_handler_fallback_cold_end:
.p2align 6
asm_handler_Enter:
- mov rdx, QWORD PTR [rbx - 40]
- test rdx, rdx
+ mov rax, QWORD PTR [rbx - 40]
+ test rax, rax
jz .Lasm_Enter.assert_predicate_failure_3
- mov ecx, DWORD PTR [rdx + 12]
- movabs rsi, 9221964661971222528
- mov rax, 5
-.Lasm_Enter.ssa_block_3:
- lea rdi, [rax + 1]
- cmp rdi, rcx
- jae .Lasm_Enter.ssa_block_2
- mov QWORD PTR [rbx + rax * 8], rsi
- mov QWORD PTR [rbx + rax * 8 + 8], rsi
- lea rax, [rax + 2]
- jmp .Lasm_Enter.ssa_block_3
+ cmp BYTE PTR [rax + 109], 0
+ jz .Lasm_Enter.ssa_block_2
+ mov BYTE PTR [rax + 109], 0
.Lasm_Enter.ssa_block_2:
- cmp rax, rcx
- jae .Lasm_Enter.ssa_block_5
- mov QWORD PTR [rbx + rax * 8], rsi
+ mov edx, DWORD PTR [rax + 12]
+ movabs rsi, 9221964661971222528
+ mov rcx, 5
.Lasm_Enter.ssa_block_5:
- mov esi, DWORD PTR [rdx + 16]
- mov rdx, QWORD PTR [rdx + 32]
- sub rsi, rcx
+ lea rdi, [rcx + 1]
+ cmp rdi, rdx
+ jae .Lasm_Enter.ssa_block_4
+ mov QWORD PTR [rbx + rcx * 8], rsi
+ mov QWORD PTR [rbx + rcx * 8 + 8], rsi
+ lea rcx, [rcx + 2]
+ jmp .Lasm_Enter.ssa_block_5
+.Lasm_Enter.ssa_block_4:
+ cmp rcx, rdx
+ jae .Lasm_Enter.ssa_block_7
+ mov QWORD PTR [rbx + rcx * 8], rsi
+.Lasm_Enter.ssa_block_7:
+ mov esi, DWORD PTR [rax + 16]
+ mov rcx, QWORD PTR [rax + 32]
+ sub rsi, rdx
xor rax, rax
-.Lasm_Enter.ssa_block_8:
+.Lasm_Enter.ssa_block_10:
cmp rax, rsi
- jae .Lasm_Enter.ssa_block_7
- test rdx, rdx
- jz .Lasm_Enter.assert_predicate_failure_25
- mov rdi, QWORD PTR [rdx + rax * 8]
- mov QWORD PTR [rbx + rcx * 8], rdi
+ jae .Lasm_Enter.ssa_block_9
+ test rcx, rcx
+ jz .Lasm_Enter.assert_predicate_failure_29
+ mov rdi, QWORD PTR [rcx + rax * 8]
+ mov QWORD PTR [rbx + rdx * 8], rdi
lea rax, [rax + 1]
- lea rcx, [rcx + 1]
- jmp .Lasm_Enter.ssa_block_8
-.Lasm_Enter.ssa_block_7:
+ lea rdx, [rdx + 1]
+ jmp .Lasm_Enter.ssa_block_10
+.Lasm_Enter.ssa_block_9:
mov BYTE PTR [rbx - 49], 1
add r13d, 1
movzx eax, BYTE PTR [r14 + r13]
@@ -20308,7 +20312,7 @@ asm_handler_JumpLooselyInequalsLoopRhsInt32_cold_end:
asm_assertion_failure_traps:
.Lasm_Enter.assert_predicate_failure_3:
ud2
-.Lasm_Enter.assert_predicate_failure_25:
+.Lasm_Enter.assert_predicate_failure_29:
ud2
.Lasm_JumpIf.assert_predicate_failure_7:
ud2
diff --git a/Libraries/LibJS/Flap/tests/interpreter-layout.conf b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
index 831dc0a7078..1cc74993581 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-layout.conf
+++ b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
@@ -124,6 +124,8 @@ const EXECUTABLE_TIER_UP_BUDGET = 96
field Executable.tier_up_budget i32 EXECUTABLE_TIER_UP_BUDGET nullable scalar
const EXECUTABLE_JIT_COMPILE_STATE = 108
field Executable.jit_compile_state u8 EXECUTABLE_JIT_COMPILE_STATE nullable scalar
+const EXECUTABLE_IDLE_COLLECTION_COUNT = 109
+field Executable.idle_collection_count u8 EXECUTABLE_IDLE_COLLECTION_COUNT nullable scalar
const JIT_COMPILE_STATE_INSTALLED = 2
const TIER_UP_FUNCTION_ENTRY_COST = 16
const TIER_UP_LOOP_ITERATION_COST = 1
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index 8b2b9b1cea9..31f0c3fbba8 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -1272,6 +1272,10 @@ handler Enter() {
consume_tier_up_budget(TIER_UP_FUNCTION_ENTRY_COST, pc, 0);
let executable = exec_ctx.executable;
assert(executable != 0);
+ # NB: Only a collection makes the count nonzero, so the store is rare.
+ if executable.idle_collection_count != 0 {
+ executable.idle_collection_count = 0;
+ }
let registers_and_locals_count = executable.registers_and_locals_count;
let empty_tag: Value = Value<Empty>;
let mut slot_index: u64 = RESERVED_REGISTER_COUNT;
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
index d897cadbcdd..b197c0f1058 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
@@ -971,6 +971,24 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.emit_prologue(&frame);
// NB: The VM is in its pinned register already.
self.masm.move64(self.pinned.frame, M::ARGUMENT_GPRS[1]);
+ // Note that the executable was entered, like the interpreter's Enter does.
+ // NB: Only a collection makes the count nonzero, so the store is rare. Storing on every entry made deeply
+ // recursive code 15% slower.
+ let offsets = self.runtime.offsets;
+ if offsets.executable_idle_collection_count != 0 {
+ let executable = self.pinned.scratch;
+ let count = M::RETURN_GPRS[1];
+ self.masm.load64(
+ executable,
+ &Address::new(self.pinned.frame, offsets.execution_context_executable as i32),
+ );
+ let idle_collection_count = Address::new(executable, offsets.executable_idle_collection_count as i32);
+ self.masm.load8(count, &idle_collection_count);
+ let entered = self.masm.new_label();
+ self.masm.branch32_imm(Condition::Equal, count, 0, entered);
+ self.masm.store_imm8(&idle_collection_count, 0);
+ self.masm.bind(entered);
+ }
let root_frame = self.local_address(self.locals.root_frame);
self.masm.store64(&root_frame, self.pinned.frame);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
index 930ae076349..4e9d8c80fa2 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
@@ -38,6 +38,7 @@ fn offsets() -> RuntimeOffsets {
execution_context_private_environment: 16,
execution_context_frame_initialized: 24,
execution_context_executable: 32,
+ executable_idle_collection_count: 0,
execution_context_slots: (8 * FRAME_HEADER_WORDS) as u32,
vm_running_execution_context: 0,
vm_jit_native_stack_limit: 8,
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index c31be317e6b..f6ad908ebbf 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -1482,6 +1482,9 @@ pub struct RuntimeOffsets {
pub execution_context_frame_initialized: u32,
/// `ExecutionContext::executable` (cell pointer).
pub execution_context_executable: u32,
+ /// `Executable::idle_collection_count` (u8), which JIT code clears at its entry like the interpreter's Enter, or 0
+ /// for code that does not.
+ pub executable_idle_collection_count: u32,
/// `sizeof(ExecutionContext)`: where the slots start.
pub execution_context_slots: u32,
/// `VmHead::running_execution_context`.
diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rs
index 28648b6bed5..1770c9070cc 100644
--- a/Libraries/LibJS/Rust/generate/layout_table.rs
+++ b/Libraries/LibJS/Rust/generate/layout_table.rs
@@ -262,6 +262,7 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {
field!(w, "EXECUTABLE_REGISTERS_AND_LOCALS_AND_CONSTANTS_COUNT", "Executable.registers_and_locals_and_constants_count", "u32", ExecutableHead, registers_and_locals_and_constants_count, 4, "nullable", "scalar", "slot_counts");
field!(w, "EXECUTABLE_CONSTANTS_DATA", "Executable.constants_data", "Sequence<Value>", ExecutableHead, constants, 8, "nullable", "scalar");
field!(w, "EXECUTABLE_TIER_UP_BUDGET", "Executable.tier_up_budget", "i32", ExecutableHead, tier_up_budget, 4, "nullable", "scalar");
+ field!(w, "EXECUTABLE_IDLE_COLLECTION_COUNT", "Executable.idle_collection_count", "u8", ExecutableHead, idle_collection_count, 1, "nullable", "scalar");
field!(w, "EXECUTABLE_JIT_COMPILE_STATE", "Executable.jit_compile_state", "u8", ExecutableHead, jit_compile_state, 1, "nullable", "scalar");
w.constant("JIT_COMPILE_STATE_INSTALLED", JIT_COMPILE_STATE_INSTALLED);
w.constant("TIER_UP_FUNCTION_ENTRY_COST", tier_up_costs::FUNCTION_ENTRY);
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 8247f317d13..5e6b32944e6 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -1432,6 +1432,7 @@ impl Executable {
bytecode_data: Cell::new(code.bytecode.as_ptr()),
jit_entry_slot: Cell::new(0),
jit_compile_state: Cell::new(0),
+ idle_collection_count: Cell::new(0),
feedback: ExecutableFeedbackHead::empty(),
constants,
property_lookup_caches,
diff --git a/Libraries/LibJS/Rust/src/interpreter/run.rs b/Libraries/LibJS/Rust/src/interpreter/run.rs
index 3713066f509..3f67232c772 100644
--- a/Libraries/LibJS/Rust/src/interpreter/run.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/run.rs
@@ -246,6 +246,8 @@ impl Vm {
unsafe { context.as_ref() }
.register(register::ACCUMULATOR)
.set(initial_accumulator_value);
+ // NB: A resumed frame continues after its Enter, so the resume counts as entering the executable.
+ executable.head.idle_collection_count.set(0);
self.run_executable(context, executable, entry_point)
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index dc4d2a4c79b..fbc247e42cc 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -1522,6 +1522,17 @@ impl Vm {
});
}
+ /// Clears the idle count of the executables that frames still run: they are not idle, even when no frame entered
+ /// them for many collections (as in a long loop).
+ fn clear_idle_collection_counts_of_running_executables(&self) {
+ self.for_each_live_execution_context(|frame| {
+ if let Some(head) = frame.executable.get() {
+ Executable::from_head(head).head.idle_collection_count.set(0);
+ }
+ ControlFlow::Continue(())
+ });
+ }
+
/// Drops the weak cache entries of strings that died in this collection.
fn remove_dead_strings_from_weak_caches(&self, sweep: &Sweep<'_>) {
let is_dead = |string: Gc<PrimitiveString>| sweep.is_dead(string);
@@ -1546,6 +1557,8 @@ impl Vm {
return false;
}
executable.remove_dead_cells(sweep);
+ let idle_collection_count = &executable.head.idle_collection_count;
+ idle_collection_count.set(idle_collection_count.get().saturating_add(1));
true
});
self.static_property_lookup_caches.remove_dead_entries(sweep);
@@ -2223,6 +2236,7 @@ unsafe extern "C" fn sweep(context: *mut c_void) {
vm.remove_dead_cells_from_weak_containers(&sweep);
vm.remove_dead_strings_from_weak_caches(&sweep);
vm.remove_dead_cells_from_property_lookup_caches(&sweep);
+ vm.clear_idle_collection_counts_of_running_executables();
vm.remove_dead_objects_from_intrinsic_accessors_and_unimplemented_properties(&sweep);
if let Some(debugger) = vm.debugger() {
debugger.remove_dead_executables(&sweep);
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index e680b46e0ba..aa664204c62 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -123,6 +123,7 @@ fn runtime_offsets() -> RuntimeOffsets {
execution_context_private_environment: offset(offset_of!(ExecutionContext, private_environment)),
execution_context_frame_initialized: offset(offset_of!(ExecutionContext, frame_initialized)),
execution_context_executable: offset(offset_of!(ExecutionContext, executable)),
+ executable_idle_collection_count: offset(offset_of!(ExecutableHead, idle_collection_count)),
execution_context_slots: offset(size_of::<ExecutionContext>()),
vm_running_execution_context: offset(offset_of!(VmHead, running_execution_context)),
vm_jit_native_stack_limit: offset(offset_of!(Vm, jit_native_stack_limit)),
diff --git a/Libraries/LibJS/Rust/src/layout/executable.rs b/Libraries/LibJS/Rust/src/layout/executable.rs
index 56ad363a3e0..acf1f5edf4c 100644
--- a/Libraries/LibJS/Rust/src/layout/executable.rs
+++ b/Libraries/LibJS/Rust/src/layout/executable.rs
@@ -34,6 +34,9 @@ pub struct ExecutableHead {
/// The executable's `jit::CompileState`. The interpreter runs new frames of the executable in its JIT code while
/// it is `JIT_COMPILE_STATE_INSTALLED`.
pub jit_compile_state: Cell<u8>,
+ /// How many collections ran since a frame last entered the executable: entering it sets this to 0, and each
+ /// collection counts it up to 255. Code that nothing entered for long gets flushed (see `bytecode::flushing`).
+ pub idle_collection_count: Cell<u8>,
/// Where the profiling interpreter records feedback for the optimizing JIT.
pub feedback: ExecutableFeedbackHead,
pub constants: BlockSlice<Value>,