Truly independent web browser
LibJS: Build executables while collections are deferred
Shannon Booth7ae6128894f9
diff --git a/Libraries/LibJS/Rust/src/bytecode/class_blueprint.rs b/Libraries/LibJS/Rust/src/bytecode/class_blueprint.rsindex e9a7da5aad1..74118d45e47 100644--- a/Libraries/LibJS/Rust/src/bytecode/class_blueprint.rs+++ b/Libraries/LibJS/Rust/src/bytecode/class_blueprint.rs@@ -13,7 +13,7 @@ use libjs_abi::ClassElementKind;use libjs_runtime_macros::Trace;use crate::bytecode::generator::{PendingClassBlueprint, PendingClassElement, PendingLiteralValueKind};-use crate::gc::root::MarkedVec;+use crate::gc::defer_gc::DeferGc;use crate::interpreter::vm::Vm;use crate::layout::value::Value;use crate::runtime::primitive_string::PrimitiveString;@@ -56,14 +56,12 @@ fn class_element_kind(kind: u8) -> ClassElementKind {}impl ClassBlueprint {- /// Creates the blueprint of a class the frontend compiled, as rust_create_class_blueprint does. The literal values- /// of its elements are also pushed to `rooted_literal_values`, which keeps them alive until something that traces- /// the blueprint holds it.+ /// Creates the blueprint of a class the frontend compiled, as rust_create_class_blueprint does.pub fn create(vm: &Vm,blueprint: &PendingClassBlueprint,source_code: Option<&Rc<SourceCode>>,- rooted_literal_values: &MarkedVec<'_, Value>,+ _defer_gc: &DeferGc<'_>,) -> ClassBlueprint {let name = match &blueprint.name {Some(name) if !name.is_empty() => Utf16FlyString::from_utf16(name),@@ -72,7 +70,7 @@ impl ClassBlueprint {let elements = blueprint.elements.iter()- .map(|element| Self::create_element(vm, element, rooted_literal_values))+ .map(|element| Self::create_element(vm, element)).collect();ClassBlueprint {constructor_shared_function_data_index: blueprint.constructor_sfd_index,@@ -86,11 +84,7 @@ impl ClassBlueprint {}}- fn create_element(- vm: &Vm,- element: &PendingClassElement,- rooted_literal_values: &MarkedVec<'_, Value>,- ) -> ClassElementDescriptor {+ fn create_element(vm: &Vm, element: &PendingClassElement) -> ClassElementDescriptor {let private_identifier = element.private_identifier.as_ref()@@ -107,9 +101,7 @@ impl ClassBlueprint {.literal_value_string.as_ref().map_or_else(Utf16String::default, |string| Utf16String::from_utf16(string));- let value = Value::from_string(PrimitiveString::create(vm, string));- rooted_literal_values.push(value);- Some(value)+ Some(Value::from_string(PrimitiveString::create(vm, string)))}};ClassElementDescriptor {diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rsindex 392306b473e..c90a8433a52 100644--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs@@ -24,6 +24,7 @@ use crate::bytecode::generator::{ConstantValue, ExceptionHandler};use crate::bytecode::generator::{LocalVariable, PendingClassBlueprint};use crate::bytecode_cache::{DecodedBytecodeBytes, DecodedExecutableRecord};use crate::gc::class::{Finalize, GcCell, define_cell};+use crate::gc::defer_gc::DeferGc;use crate::gc::heap::cell_is_dead;use crate::gc::root::MarkedVec;use crate::gc::visitor::{Trace, Visitor};@@ -1326,13 +1327,6 @@ fn interpreter_buffer<T>(elements: &[T]) -> InterpreterBuffer<T> {}impl Executable {- /// Moves `executable` into the heap and has the VM prune its inline caches after every collection.- pub fn create_from_parts(vm: &Vm, executable: Executable) -> Gc<Executable> {- let executable = vm.heap().allocate(executable);- vm.register_executable(executable);- executable- }-pub fn property_lookup_cache(&self, index: usize) -> &PropertyLookupCache {&self.property_lookup_caches[index]}@@ -1404,6 +1398,7 @@ impl Executable {}}+ #[allow(clippy::too_many_arguments)]pub fn new_with_bytecode(bytecode: ExecutableBytecode,number_of_registers: u32,@@ -1412,6 +1407,7 @@ impl Executable {constants: Box<[Value]>,cache_counts: &ExecutableCacheCounts,is_strict_mode: bool,+ _defer_gc: &DeferGc<'_>,) -> Self {let property_lookup_caches: Box<[PropertyLookupCache]> = (0..cache_counts.property_lookup_caches).map(|_| PropertyLookupCache::new())@@ -1586,41 +1582,61 @@ impl Executable {source_code: Option<&Rc<SourceCode>>,replaced_executable: Option<Gc<Executable>>,) -> Gc<Executable> {- // The literal values of class elements stay rooted until the executable that holds them is allocated.- let rooted_literal_values = MarkedVec::new(vm);+ let executable = vm.heap().allocate_deferring_gc(|defer_gc| {+ Self::build(+ vm,+ parts,+ rooted_shared_function_data,+ source_code,+ replaced_executable,+ defer_gc,+ )+ });+ vm.register_executable(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);+ }++ if let Some(debugger) = vm.debugger() {+ debugger.register_executable(vm, executable);+ }++ executable+ }++ fn build(+ vm: &Vm,+ parts: ExecutableParts,+ rooted_shared_function_data: &MarkedVec<'_, Gc<SharedFunctionInstanceData>>,+ source_code: Option<&Rc<SourceCode>>,+ replaced_executable: Option<Gc<Executable>>,+ defer_gc: &DeferGc<'_>,+ ) -> Executable {let class_blueprints: Box<[ClassBlueprint]> = parts.class_blueprints.iter()- .map(|blueprint| ClassBlueprint::create(vm, blueprint, source_code, &rooted_literal_values))+ .map(|blueprint| ClassBlueprint::create(vm, blueprint, source_code, defer_gc)).collect();let shared_function_data: Box<[Gc<SharedFunctionInstanceData>]> =- rooted_shared_function_data.to_vec().into_boxed_slice();-- // The constants stay rooted until the executable that holds them is allocated.- let rooted_constants = MarkedVec::with_capacity(vm, parts.constants.len());- for constant in &parts.constants {- rooted_constants.push(constant_value(vm, constant));- }- let constants: Box<[Value]> = rooted_constants.to_vec().into_boxed_slice();- // The template object caches stay rooted until the executable that holds them is allocated. An executable this- // one replaces shares its own, so that both hand out the same template objects.- let rooted_template_object_caches = MarkedVec::with_capacity(vm, parts.cache_counts.template_object as usize);- match replaced_executable {+ (*rooted_shared_function_data.values()).into();+ let constants: Box<[Value]> = parts+ .constants+ .iter()+ .map(|constant| constant_value(vm, constant))+ .collect();+ // An executable this one replaces shares its template object caches, so that both hand out the same template+ // objects.+ let template_object_caches: Box<[Gc<TemplateObjectCache>]> = match replaced_executable {Some(replaced_executable)if replaced_executable.template_object_caches.len() == parts.cache_counts.template_object as usize =>{- for cache in &replaced_executable.template_object_caches {- rooted_template_object_caches.push(*cache);- }+ replaced_executable.template_object_caches.clone()}- _ => {- for _ in 0..parts.cache_counts.template_object {- rooted_template_object_caches.push(TemplateObjectCache::create(vm));- }- }- }- let template_object_caches: Box<[Gc<TemplateObjectCache>]> =- rooted_template_object_caches.to_vec().into_boxed_slice();+ _ => (0..parts.cache_counts.template_object)+ .map(|_| TemplateObjectCache::create(vm))+ .collect(),+ };let counts = ExecutableCacheCounts {property_lookup_caches: parts.cache_counts.property_lookup,global_variable_caches: parts.cache_counts.global_variable,@@ -1636,6 +1652,7 @@ impl Executable {constants,&counts,parts.is_strict,+ defer_gc,);executable.identifier_table = parts.identifier_table;executable.set_property_key_table(parts.property_key_table);@@ -1680,19 +1697,6 @@ impl Executable {.collect();executable.argument_variable_names = parts.argument_variable_names.into_boxed_slice();executable.source_code = source_code.cloned();- 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);- drop(rooted_literal_values);-- if let Some(debugger) = vm.debugger() {- debugger.register_executable(vm, executable);- }-executable}diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rsindex 2f83c45b23e..a49a8dc4191 100644--- a/Libraries/LibJS/Rust/src/gc/heap.rs+++ b/Libraries/LibJS/Rust/src/gc/heap.rs@@ -13,6 +13,7 @@ use std::collections::HashMap;use super::capi::{self, GCAllocator, GCGatherRootsCallback, GCHeap, GCLayout};use super::class::{CellTypeInfo, Class, GcCell};use super::class_id::{CLASS_COUNT, ClassId};+use super::defer_gc::DeferGc;use super::weak::WEAK_IMPL_POINTER_OFFSET;use crate::build_configuration::{HEAP_REGION_OFFSET_MASK, PRIMITIVE_STORAGE_CAGE_OFFSET_MASK};use crate::layout::cell::{CellHeader, CellState, Gc};@@ -202,6 +203,15 @@ impl Heap {unsafe { self.allocate_with(allocator, size_of::<T>(), AllocationKind::Cell, cell, |_| {}) }}+ /// Moves the cell that `build` returns into the heap, with collections deferred from before `build` runs until+ /// the cell is in the heap.+ pub fn allocate_deferring_gc<T: GcCell>(&self, build: impl FnOnce(&DeferGc<'_>) -> T) -> Gc<T> {+ let defer_gc = DeferGc::of_heap(self);+ let cell = self.allocate(build(&defer_gc));+ drop(defer_gc);+ cell+ }+/// Moves `cell`, whose class is the one `allocator` was created for or a class derived from it, into the storage/// of that allocator. A derived class shares its ancestor's allocator when its cells may share heap blocks with the/// ancestor's, as a host class that shares its parent's allocator does.diff --git a/Libraries/LibJS/Rust/src/runtime/source_text_module.rs b/Libraries/LibJS/Rust/src/runtime/source_text_module.rsindex 45ff09d395e..b5f306ab049 100644--- a/Libraries/LibJS/Rust/src/runtime/source_text_module.rs+++ b/Libraries/LibJS/Rust/src/runtime/source_text_module.rs@@ -444,13 +444,6 @@ impl SourceTextModule {(Some(parts.executable), None)};- let functions_to_initialize = rooted_shared_data- .to_vec()- .into_iter()- .zip(parts.function_names)- .map(|(shared_data, name)| FunctionToInitialize { shared_data, name })- .collect();-let module = vm.heap().allocate(SourceTextModule {base: CyclicModule::new(Self::CLASS,@@ -469,7 +462,12 @@ impl SourceTextModule {imported_bindings: GcRefCell::new(Vec::new()),var_declared_names: parts.var_declared_names,lexical_bindings: parts.lexical_bindings,- functions_to_initialize,+ functions_to_initialize: rooted_shared_data+ .values()+ .iter()+ .zip(parts.function_names)+ .map(|(&shared_data, name)| FunctionToInitialize { shared_data, name })+ .collect(),default_export_binding_name: parts.default_export_binding_name,executable: Cell::new(executable),tla_shared_data,diff --git a/Libraries/LibJS/Rust/src/script.rs b/Libraries/LibJS/Rust/src/script.rsindex 4f0b3ed633e..5f14f8afb55 100644--- a/Libraries/LibJS/Rust/src/script.rs+++ b/Libraries/LibJS/Rust/src/script.rs@@ -356,16 +356,7 @@ impl Script {filename: &str,host_defined: ForeignCellSlot,) -> Gc<Script> {- let functions_to_initialize: Vec<FunctionToInitialize> = rooted_shared_data- .to_vec()- .into_iter()- .zip(parts.function_names)- .map(|(shared_data, name)| FunctionToInitialize { shared_data, name })- .collect();- let declared_function_names = functions_to_initialize- .iter()- .map(|function| function.name.clone())- .collect();+ let declared_function_names = parts.function_names.iter().cloned().collect();let script = vm.heap().allocate(Script {header: CellHeader::for_class(Self::CLASS),@@ -377,7 +368,12 @@ impl Script {source_code,lexical_names: parts.lexical_names,var_names: parts.var_names,- functions_to_initialize,+ functions_to_initialize: rooted_shared_data+ .values()+ .iter()+ .zip(parts.function_names)+ .map(|(&shared_data, name)| FunctionToInitialize { shared_data, name })+ .collect(),declared_function_names,var_scoped_names: parts.var_scoped_names,annex_b_candidate_names: parts.annex_b_candidate_names,