Skip to content

Truly independent web browser

LibJS: Keep what only linking reads in one block of memory

Andreas Klingea4ba76ca501
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/mod.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/mod.rs
index b912f2c347e..b201244e084 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/mod.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/mod.rs
@@ -124,7 +124,7 @@ pub fn compile_program(input: &ProgramInput<'_>) -> Result<ExecutableData, OutOf
};
tree.give_back_memory();
let mut executable = Arc::into_inner(compiled?.executable).expect("nothing else holds on to new code");
- for pending in &mut executable.shared_function_data {
+ for pending in executable.shared_function_data.iter_mut() {
if pending.should_eager_compile {
pending.precompiled_function =
crate::compile::compile_function(&pending.description.payload, false, input.stack_limit)
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs
index be56b85b172..19ad9087295 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/names.rs
@@ -422,14 +422,21 @@ impl NameTables {
+ SliceBlock::room_for::<Utf16FlyString>(self.strings.entries.len())
}
- /// Hands out the tables, as an executable keeps them: identifiers, property keys and strings.
+ /// How much of a block the table that only linking reads takes.
+ pub(super) fn room_for_link_tables(&self) -> usize {
+ SliceBlock::room_for::<Utf16FlyString>(self.property_keys.entries.len())
+ }
+
+ /// Hands out the tables, as an executable keeps them: identifiers and strings from `block`, and property keys from
+ /// `link_block`.
pub(super) fn take_tables(
&mut self,
tree: &SyntaxTree,
block: &mut SliceBlockBuilder,
+ link_block: &mut SliceBlockBuilder,
) -> (
BlockSlice<Utf16FlyString>,
- Box<[Utf16FlyString]>,
+ BlockSlice<Utf16FlyString>,
BlockSlice<Utf16FlyString>,
) {
let script_names = &mut self.script_names;
@@ -439,7 +446,7 @@ impl NameTables {
};
(
block.slice(self.identifiers.entries.iter().map(&mut fly_string_of)),
- self.property_keys.entries.iter().map(&mut fly_string_of).collect(),
+ link_block.slice(self.property_keys.entries.iter().map(&mut fly_string_of)),
block.slice(self.strings.entries.iter().map(&mut fly_string_of)),
)
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index de675087c40..7f27211c0b2 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -21,9 +21,11 @@ use crate::ast::LocalType;
use crate::ast::Name;
use crate::ast::Resolution;
use crate::ast::SyntaxTree;
+use crate::bytecode::executable_data::ConstantValue;
use crate::bytecode::executable_data::ExecutableData;
use crate::bytecode::executable_data::FunctionSfdMetadata;
use crate::bytecode::executable_data::LocalVariableMetadata;
+use crate::bytecode::executable_data::PendingClassBlueprint;
use crate::bytecode::executable_data::PendingSharedFunctionData;
use crate::bytecode::executable_data::PrecompiledFunction;
use crate::bytecode::instruction::typed as op;
@@ -1912,8 +1914,14 @@ impl Generator<'_> {
self.context.scope.scope_resolutions = entries;
enclosing_code
};
- let mut shared_function_data: Vec<PendingSharedFunctionData> = Vec::with_capacity(nested_count);
- for nested in 0..nested_count {
+ self.seal_stream();
+ let mut link_block = SliceBlock::builder(
+ SliceBlock::room_for::<PendingSharedFunctionData>(nested_count)
+ + SliceBlock::room_for::<PendingClassBlueprint>(self.class_blueprints.len())
+ + SliceBlock::room_for::<ConstantValue>(self.context.constants.values.len())
+ + self.context.names.room_for_link_tables(),
+ );
+ let shared_function_data = link_block.slice((0..nested_count).map(|nested| {
let nested_function = &mut self.context.scope.nested_functions[nested];
let row = nested_function.row;
let is_eager = nested_function.is_eager;
@@ -1939,19 +1947,19 @@ impl Generator<'_> {
let mut pending = crate::compile::pending_function_of_row(script, row, name, environment, &enclosing_code);
pending.class_field_initializer_name = field_name;
pending.should_eager_compile = is_eager;
- shared_function_data.push(pending);
- }
+ pending
+ }));
- self.seal_stream();
let scope = &self.context.scope;
let variable_name_count = scope.locals.len() + scope.parameters.len();
let other_tables_size = self.context.names.room_for_tables()
+ SliceBlock::room_for::<ak::Utf16FlyString>(variable_name_count)
+ SliceBlock::room_for::<LocalVariableMetadata>(scope.locals.len());
let (assembled, mut block) = self.finish_stream(other_tables_size);
- let (identifier_table, property_key_table, string_table) = self.context.names.take_tables(tree, &mut block);
+ let (identifier_table, property_key_table, string_table) =
+ self.context.names.take_tables(tree, &mut block, &mut link_block);
let context = &mut *self.context;
- let constants: Box<[_]> = context.constants.values.drain(..).collect();
+ let constants = link_block.slice(context.constants.values.drain(..));
let scope = &context.scope;
let local_variable_metadata = block.slice(scope.locals.iter().map(|local| LocalVariableMetadata {
is_mutable: local.is_mutable,
@@ -1999,10 +2007,11 @@ impl Generator<'_> {
length_identifier: self.length_identifier,
property_key_table,
constants,
- shared_function_data: shared_function_data.into(),
- class_blueprints: std::mem::take(&mut self.class_blueprints).into(),
+ shared_function_data,
+ class_blueprints: link_block.slice(std::mem::take(&mut self.class_blueprints).into_iter()),
position_source: Some(script.positions.clone()),
tables: block.finish(),
+ link_tables: link_block.finish(),
};
let this_from_environment = this_comes_from_environment(flags);
PrecompiledFunction {
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 82095f83711..7b5e85adfed 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -1387,7 +1387,8 @@ impl Executable {
}
/// Links what the frontend compiled from `source_code` into a new executable, with the functions and classes it
- /// declares and caches of its own. `data` stays as it is, so it can be linked again, into any realm.
+ /// declares and caches of its own. While something else holds `data`, it stays as it is and can be linked again,
+ /// into any realm. Without another holder nothing can link it again, so what only linking reads goes away.
pub fn create_with_source_code(
vm: &Vm,
data: Arc<ExecutableData>,
@@ -1443,12 +1444,7 @@ impl Executable {
.map(|blueprint| ClassBlueprint::create(vm, blueprint, source_code, defer_gc))
.collect();
let property_keys = match Arc::get_mut(&mut data) {
- Some(code) => tables.slice(
- std::mem::take(&mut code.property_key_table)
- .into_vec()
- .into_iter()
- .map(PropertyKey::from),
- ),
+ Some(code) => tables.slice(code.property_key_table.take_elements().map(PropertyKey::from)),
None => tables.slice(data.property_key_table.iter().cloned().map(PropertyKey::from)),
};
let constants = tables.slice(data.constants.iter().map(|constant| constant_value(vm, constant)));
@@ -1604,6 +1600,7 @@ impl Executable {
}
/// What this executable runs. Executables that link the same code have the same one.
+ /// This is not something to link again, as the last holder of code has nothing left to link it from.
pub fn code(&self) -> &Arc<ExecutableData> {
&self.code
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable_data.rs b/Libraries/LibJS/Rust/src/bytecode/executable_data.rs
index faba5054cdb..346f973d205 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable_data.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable_data.rs
@@ -194,14 +194,16 @@ pub struct ExecutableData {
pub local_variable_metadata: BlockSlice<LocalVariableMetadata>,
pub length_identifier: Option<PropertyKeyTableIndex>,
/// What linking reads, and nothing after it (see [`ExecutableData::release_link_inputs()`]).
- pub property_key_table: Box<[ak::Utf16FlyString]>,
- pub constants: Box<[ConstantValue]>,
- pub shared_function_data: Box<[PendingSharedFunctionData]>,
- pub class_blueprints: Box<[PendingClassBlueprint]>,
+ pub property_key_table: BlockSlice<ak::Utf16FlyString>,
+ pub constants: BlockSlice<ConstantValue>,
+ pub shared_function_data: BlockSlice<PendingSharedFunctionData>,
+ pub class_blueprints: BlockSlice<PendingClassBlueprint>,
/// What turns the source offsets of a source map that has them into lines and columns.
pub position_source: Option<Arc<crate::parser::SourcePositions>>,
/// The memory of everything above that running the code reads, which gets dropped before it.
pub tables: SliceBlock,
+ /// The memory of what only linking reads.
+ pub link_tables: SliceBlock,
}
fn constant_memory_size(constant: &ConstantValue) -> usize {
@@ -270,9 +272,10 @@ impl ExecutableData {
/// Frees what only linking reads, for an executable that is the last to hold this code, so that code nothing can
/// link again costs no more than what running it reads.
pub fn release_link_inputs(&mut self) {
- self.property_key_table = Box::default();
- self.constants = Box::default();
- self.shared_function_data = Box::default();
- self.class_blueprints = Box::default();
+ self.property_key_table = BlockSlice::empty();
+ self.constants = BlockSlice::empty();
+ self.shared_function_data = BlockSlice::empty();
+ self.class_blueprints = BlockSlice::empty();
+ self.link_tables = SliceBlock::empty();
}
}
diff --git a/Libraries/LibJS/Rust/src/slice_block.rs b/Libraries/LibJS/Rust/src/slice_block.rs
index 62318e07f89..5221b10c2b4 100644
--- a/Libraries/LibJS/Rust/src/slice_block.rs
+++ b/Libraries/LibJS/Rust/src/slice_block.rs
@@ -172,6 +172,14 @@ impl<T> BlockSlice<T> {
// SAFETY: A non-null slice has its length in front of its initialized elements, which it owns.
unsafe { ak::slice_from_raw_parts_mut(self.data, self.data.cast::<usize>().sub(1).read()) }
}
+
+ /// Moves the elements out and leaves the slice empty. Elements the caller does not get to stay undropped.
+ pub fn take_elements(&mut self) -> impl ExactSizeIterator<Item = T> {
+ let length = self.len();
+ let data = core::mem::replace(&mut self.data, core::ptr::null_mut());
+ // SAFETY: The slice let go of the elements, and each gets read once.
+ (0..length).map(move |index| unsafe { data.add(index).read() })
+ }
}
impl<T> core::ops::Deref for BlockSlice<T> {