Skip to content

Truly independent web browser

LibJS: Build the function index of a parse in the work zone

Andreas Klingc7255a367bdb
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index 4e4f5ac269e..48551e5ce5f 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -256,8 +256,6 @@ pub struct Parser<'a, 'k, U: Unit> {
pub(crate) inline_argument_limit: usize,
/// Whether anything uses what scopes declare and what code refers to, which a sweep with a summary does not.
pub(crate) tracks_scopes: bool,
- /// Whether the first parse leaves a summary of the bindings of each function.
- records_summaries: bool,
/// How many bindings of the function being parsed have a place in its summary.
pub(crate) summary_ordinal: u32,
pub(crate) src: *const U,
@@ -278,7 +276,7 @@ pub struct Parser<'a, 'k, U: Unit> {
pub interner: Interner<'k>,
pub ast: Ast<'a>,
- pub index: FunctionIndex,
+ pub index: FunctionIndexBuilder<'a>,
pub(crate) scratch: ZoneVec<'a, u32>,
pub(crate) mode: Mode,
pub(crate) retain: bool,
@@ -408,25 +406,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
} else {
1 << 12
};
- let index = FunctionIndex {
- start: Vec::with_capacity(estimated_functions),
- params_start: Vec::with_capacity(estimated_functions),
- end: Vec::with_capacity(estimated_functions),
- flags: Vec::with_capacity(estimated_functions),
- length: Vec::with_capacity(estimated_functions),
- parameter_count: Vec::with_capacity(estimated_functions),
- ctx: Vec::with_capacity(estimated_functions),
- name: Vec::with_capacity(estimated_functions),
- capture_start: Vec::with_capacity(estimated_functions),
- capture_count: Vec::with_capacity(estimated_functions),
- capture_names: Vec::new(),
- summaries: Vec::new(),
- declared_names: Vec::new(),
- summary_start: Vec::new(),
- program_summary_end: 0,
- program_has_direct_eval: false,
- ..FunctionIndex::default()
- };
+ let index = FunctionIndexBuilder::with_capacity(zone, estimated_functions);
let mut parser = Self::with_capacities(
zone,
source.as_ptr(),
@@ -473,7 +453,6 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
interner: Interner<'k>,
names: &'k mut [NameState],
stamp: u32,
- index: FunctionIndex,
first_pass_index: &FunctionIndex,
source_length: u32,
estimated_nodes: usize,
@@ -486,7 +465,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
Mode::Lazy,
interner,
names,
- index,
+ FunctionIndexBuilder::with_capacity(zone, 0),
estimated_nodes,
256,
);
@@ -504,7 +483,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
mode: Mode,
interner: Interner<'k>,
names: &'k mut [NameState],
- index: FunctionIndex,
+ index: FunctionIndexBuilder<'a>,
estimated_nodes: usize,
estimated_uses: usize,
) -> Self {
@@ -519,7 +498,6 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
arguments_in_array: false,
inline_argument_limit: crate::bytecode::operand::MAX_OPERAND_LIST_LENGTH,
tracks_scopes: true,
- records_summaries: true,
summary_ordinal: 0,
src: source,
len: length as u32,
@@ -652,6 +630,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
pub(crate) fn parse_top_level_rows(
&mut self,
context: ProgramContext,
+ index: &FunctionIndex,
) -> Result<(Ast<'a>, u32), super::OutOfStack> {
// NB: The rows are those of the tree, and marks.
let rows = Ast::with_capacity(self.zone, self.ast.tag.len());
@@ -661,7 +640,8 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
(self.first_token_start, self.strict, self.has_top_level_await);
self.finish_reparse();
self.export_names.clear();
- self.first_pass_index = &raw const self.index;
+ // NB: The first pass index is only read until the end of this function.
+ self.first_pass_index = index;
self.tracks_scopes = false;
self.marks = true;
let node = self.reparse_program(context);
@@ -735,14 +715,14 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
/// Writes out the summaries of all functions, now that all of their scopes closed.
fn group_summaries(&mut self) {
- fn write_varint(bytes: &mut Vec<u8>, mut value: u32) {
+ fn write_varint(bytes: &mut ZoneVec<'_, u8>, mut value: u32) {
while value >= 0x80 {
bytes.push(value as u8 | 0x80);
value >>= 7;
}
bytes.push(value as u8);
}
- fn write_scope_bindings(bytes: &mut Vec<u8>, bindings: &[(u32, u32, u32)]) {
+ fn write_scope_bindings(bytes: &mut ZoneVec<'_, u8>, bindings: &[(u32, u32, u32)]) {
let scope_count = bindings.chunk_by(|a, b| a.1 == b.1).count();
write_varint(bytes, scope_count as u32);
let mut last_position = 0;
@@ -819,9 +799,6 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
#[inline(always)]
fn note_new_binding_of(&mut self, d: usize, name: u32, scope_index: usize, bind: Bind) {
- if !self.records_summaries {
- return;
- }
let scope = &self.scopes[scope_index];
let head = self.names[name as usize].use_head;
let is_function_level = scope.flags & scope_flag::VAR_SCOPE != 0;
@@ -1304,17 +1281,13 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
if row == NONE {
if !is_parameter {
self.add_declaration(hoist.name, top, decl::ANNEX_B);
- if self.records_summaries {
- let d = self.decls.len() - 1;
- self.decls[d].summary_slot = SUMMARY_SLOT_EARLY;
- }
+ let d = self.decls.len() - 1;
+ self.decls[d].summary_slot = SUMMARY_SLOT_EARLY;
}
} else if flags & !decl::FUNCTION_NAME == 0 {
// NB: The name of a function expression is bound outside of the function, which gets a `var` of its own.
self.decls[row as usize].flags |= decl::ANNEX_B;
- if self.records_summaries {
- self.decls[row as usize].summary_slot = SUMMARY_SLOT_EARLY;
- }
+ self.decls[row as usize].summary_slot = SUMMARY_SLOT_EARLY;
} else {
self.decls[row as usize].flags |= decl::ANNEX_B;
if !is_parameter {
@@ -1342,9 +1315,6 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
/// Makes declaration `d` of the scope at `top` one that the code of its function knows from the start, and not only
/// from where it comes up. The declarations after it in the order of the summary move up into its place.
fn make_binding_early(&mut self, d: u32, top: usize) {
- if !self.records_summaries {
- return;
- }
let slot = self.decls[d as usize].summary_slot;
if slot & (SUMMARY_SLOT_EARLY | SUMMARY_SLOT_INNER) != 0 {
return;
@@ -1451,10 +1421,8 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
if used_in_with {
flags |= decl::USED_IN_WITH;
}
- if self.records_summaries {
- let summary_slot = self.decls[d as usize].summary_slot;
- self.note_closed_binding(summary_slot, name, captured || used_in_with, scope);
- }
+ let summary_slot = self.decls[d as usize].summary_slot;
+ self.note_closed_binding(summary_slot, name, captured || used_in_with, scope);
}
self.names[name as usize].decl_head = declaration.next_same_name;
if keep {
@@ -1632,7 +1600,8 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
}
fn add_declaration(&mut self, name: u32, scope_index: usize, flags: u32) {
- if self.records_summaries {
+ // NB: The first parse saw every declaration already.
+ if !self.is_reparsing() {
let word = name as usize / 64;
if word >= self.index.declared_names.len() {
self.index.declared_names.resize(word + 1, 0);
@@ -2215,11 +2184,11 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
self.len = length;
}
- /// Ends the parse of a function again, whether it went well or not. Gives back the tree, and the index and the stamp
- /// for the reparse context, whose name states are clean again.
- pub(crate) fn end_reparse(mut self) -> (Ast<'a>, FunctionIndex, u32) {
+ /// Ends the parse of a function again, whether it went well or not. Gives back the tree, and the stamp for the
+ /// reparse context, whose name states are clean again.
+ pub(crate) fn end_reparse(mut self) -> (Ast<'a>, u32) {
self.finish_reparse();
- (self.ast, self.index, self.stamp)
+ (self.ast, self.stamp)
}
/// Gets ready for the next function, whether this one went well or not.
diff --git a/Libraries/LibJS/Rust/src/parser/columns.rs b/Libraries/LibJS/Rust/src/parser/columns.rs
index 9a4f7137720..0586f7ff827 100644
--- a/Libraries/LibJS/Rust/src/parser/columns.rs
+++ b/Libraries/LibJS/Rust/src/parser/columns.rs
@@ -244,28 +244,29 @@ pub struct AstMarks {
pub scopes: u32,
}
-/// What a function leaves behind once its tree is gone, one row per function, as columns.
+/// What a function leaves behind once its tree is gone, one row per function, as columns. The first parse of a source
+/// builds it in its work zone (see [`FunctionIndexBuilder`]) and keeps a copy at its final size.
#[derive(Default)]
pub struct FunctionIndex {
- pub start: Vec<u32>,
+ pub start: Box<[u32]>,
/// Where parsing the function starts over. This is what tells two functions apart that start at the same offset,
/// and for the code of a class that runs as a function, the offset of the class element or the class.
- pub params_start: Vec<u32>,
- pub end: Vec<u32>,
- pub flags: Vec<u32>,
- pub length: Vec<u32>,
- pub parameter_count: Vec<u32>,
+ pub params_start: Box<[u32]>,
+ pub end: Box<[u32]>,
+ pub flags: Box<[u32]>,
+ pub length: Box<[u32]>,
+ pub parameter_count: Box<[u32]>,
/// What the function's code may do, as parser context bits.
- pub ctx: Vec<u16>,
+ pub ctx: Box<[u16]>,
/// The function's own name, if it has one.
- pub name: Vec<u32>,
+ pub name: Box<[u32]>,
/// Where each function's captures start in `capture_names`, and how many it has.
- pub capture_start: Vec<u32>,
- pub capture_count: Vec<u32>,
+ pub capture_start: Box<[u32]>,
+ pub capture_count: Box<[u32]>,
/// What each function captures of the function around it, which is all that reparsing that function needs to know
/// about a function it skips. A name that only passes through on its way further out is not in here: it is a capture
/// of the outer function that it passes through.
- pub capture_names: Vec<u32>,
+ pub capture_names: Box<[u32]>,
/// The rows by `params_start`, unless that is the order they are in. It often is not, as an arrow function only gets
/// its row after its parameters, and so after the functions in them. Worked out once functions get parsed again.
pub(super) rows_by_params_start: OnceLock<Vec<u32>>,
@@ -275,16 +276,83 @@ pub struct FunctionIndex {
/// which of the other bindings of the function itself are captured, in the order of their declarations. A scope is
/// the distance from the scope before it, the number of bindings and those. A function with nothing to say has no
/// bytes.
- pub summaries: Vec<u8>,
- pub summary_start: Vec<u32>,
+ pub summaries: Box<[u8]>,
+ pub summary_start: Box<[u32]>,
/// A bit for every name of the source that anything declares, which all other names are free everywhere.
- pub declared_names: Vec<u64>,
+ pub declared_names: Box<[u64]>,
/// Where the summary of the top level ends, which is the first thing in the pool.
pub program_summary_end: u32,
/// Whether the top level has a direct `eval`, so that names may mean more than they seem to there.
pub program_has_direct_eval: bool,
}
+/// The function index that the first parse of a source builds in its work zone. See [`FunctionIndex`] for the columns.
+pub struct FunctionIndexBuilder<'z> {
+ pub start: ZoneVec<'z, u32>,
+ pub params_start: ZoneVec<'z, u32>,
+ pub end: ZoneVec<'z, u32>,
+ pub flags: ZoneVec<'z, u32>,
+ pub length: ZoneVec<'z, u32>,
+ pub parameter_count: ZoneVec<'z, u32>,
+ pub ctx: ZoneVec<'z, u16>,
+ pub name: ZoneVec<'z, u32>,
+ pub capture_start: ZoneVec<'z, u32>,
+ pub capture_count: ZoneVec<'z, u32>,
+ pub capture_names: ZoneVec<'z, u32>,
+ pub summaries: ZoneVec<'z, u8>,
+ pub summary_start: ZoneVec<'z, u32>,
+ pub declared_names: ZoneVec<'z, u64>,
+ pub program_summary_end: u32,
+ pub program_has_direct_eval: bool,
+}
+
+impl<'z> FunctionIndexBuilder<'z> {
+ /// An index with room for `functions` rows.
+ pub fn with_capacity(zone: &'z Zone, functions: usize) -> Self {
+ Self {
+ start: ZoneVec::with_capacity_in(functions, zone),
+ params_start: ZoneVec::with_capacity_in(functions, zone),
+ end: ZoneVec::with_capacity_in(functions, zone),
+ flags: ZoneVec::with_capacity_in(functions, zone),
+ length: ZoneVec::with_capacity_in(functions, zone),
+ parameter_count: ZoneVec::with_capacity_in(functions, zone),
+ ctx: ZoneVec::with_capacity_in(functions, zone),
+ name: ZoneVec::with_capacity_in(functions, zone),
+ capture_start: ZoneVec::with_capacity_in(functions, zone),
+ capture_count: ZoneVec::with_capacity_in(functions, zone),
+ capture_names: ZoneVec::new_in(zone),
+ summaries: ZoneVec::new_in(zone),
+ summary_start: ZoneVec::with_capacity_in(functions, zone),
+ declared_names: ZoneVec::new_in(zone),
+ program_summary_end: 0,
+ program_has_direct_eval: false,
+ }
+ }
+
+ /// A copy of the index, at its final size.
+ pub fn to_kept(&self) -> FunctionIndex {
+ FunctionIndex {
+ start: Box::from(self.start.as_slice()),
+ params_start: Box::from(self.params_start.as_slice()),
+ end: Box::from(self.end.as_slice()),
+ flags: Box::from(self.flags.as_slice()),
+ length: Box::from(self.length.as_slice()),
+ parameter_count: Box::from(self.parameter_count.as_slice()),
+ ctx: Box::from(self.ctx.as_slice()),
+ name: Box::from(self.name.as_slice()),
+ capture_start: Box::from(self.capture_start.as_slice()),
+ capture_count: Box::from(self.capture_count.as_slice()),
+ capture_names: Box::from(self.capture_names.as_slice()),
+ rows_by_params_start: OnceLock::new(),
+ summaries: Box::from(self.summaries.as_slice()),
+ summary_start: Box::from(self.summary_start.as_slice()),
+ declared_names: Box::from(self.declared_names.as_slice()),
+ program_summary_end: self.program_summary_end,
+ program_has_direct_eval: self.program_has_direct_eval,
+ }
+ }
+}
+
impl FunctionIndex {
/// Gets the function at each offset ready to be found, for whatever parses a source off the main thread to do before
/// handing it over, as this takes a while for a large source.
@@ -401,25 +469,8 @@ impl FunctionIndex {
&self.capture_names,
];
let sorted_rows = self.rows_by_params_start.get().map_or(0, Vec::capacity);
- (u32_columns.iter().map(|column| column.capacity()).sum::<usize>() + sorted_rows) * size_of::<u32>()
- + self.ctx.capacity() * size_of::<u16>()
- }
-
- /// Gives back what parsing set aside for rows that it did not need, now that the index is all that stays.
- pub fn shrink_to_fit(&mut self) {
- self.start.shrink_to_fit();
- self.params_start.shrink_to_fit();
- self.end.shrink_to_fit();
- self.flags.shrink_to_fit();
- self.length.shrink_to_fit();
- self.parameter_count.shrink_to_fit();
- self.ctx.shrink_to_fit();
- self.name.shrink_to_fit();
- self.capture_start.shrink_to_fit();
- self.capture_count.shrink_to_fit();
- self.capture_names.shrink_to_fit();
- self.summaries.shrink_to_fit();
- self.summary_start.shrink_to_fit();
+ (u32_columns.iter().map(|column| column.len()).sum::<usize>() + sorted_rows) * size_of::<u32>()
+ + self.ctx.len() * size_of::<u16>()
}
}
diff --git a/Libraries/LibJS/Rust/src/parser/mod.rs b/Libraries/LibJS/Rust/src/parser/mod.rs
index e0c1795e4a5..0147da347ea 100644
--- a/Libraries/LibJS/Rust/src/parser/mod.rs
+++ b/Libraries/LibJS/Rust/src/parser/mod.rs
@@ -69,8 +69,6 @@ struct ReparseContext {
name_states: Box<[base::NameState]>,
/// The last stamp of a name state.
stamp: u32,
- /// What the parses write about the names they declare. Nothing reads it.
- index: FunctionIndex,
}
/// The sources that have a reparse context now, for [`release_idle_reparse_contexts()`].
@@ -250,7 +248,6 @@ impl ScriptData {
names,
name_states,
stamp: 0,
- index: FunctionIndex::default(),
}
}
@@ -282,7 +279,6 @@ impl ScriptData {
Interner::of_kept(zone, &mut context.names),
&mut context.name_states,
context.stamp,
- core::mem::take(&mut context.index),
&self.index,
u32_from_usize(source.len()),
estimated_nodes,
@@ -313,8 +309,7 @@ impl ScriptData {
name_count,
"a function has only names that the first parse of its source found"
);
- let (ast, index, stamp) = parser.end_reparse();
- context.index = index;
+ let (ast, stamp) = parser.end_reparse();
context.stamp = stamp;
match error {
None => Ok((ast, node)),
@@ -578,8 +573,9 @@ fn parse_code_units<'z, U: Unit + Default, R>(
return Ok(None);
}
let lines = LineTable::new(zone, units, options.initial_line_number);
+ let index = parser.index.to_kept();
let top_level_rows = parser
- .parse_top_level_rows(options.program_context())
+ .parse_top_level_rows(options.program_context(), &index)
.map(|(rows, node)| (rows.into_columns(), node));
let default_export_name = Name(parser.interner.intern_bytes(b"*default*"));
let default_constructor_parameter_name = Name(parser.interner.intern_bytes(b"args"));
@@ -590,8 +586,6 @@ fn parse_code_units<'z, U: Unit + Default, R>(
global_names_used_in_with.dedup();
let eval_referenced_private_names =
zone.alloc_slice_from_iter(parser.eval_referenced_private_names.iter().map(|&name| Name(name)));
- let mut index = parser.index;
- index.shrink_to_fit();
let script = Arc::new(ScriptData {
index,
names,