Truly independent web browser
LibJS: Keep the work lists of the parser in the work zone
Andreas Klingdd2017917454
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rsindex 07ea37cf41d..57dec8c967b 100644--- a/Libraries/LibJS/Rust/src/parser/base.rs+++ b/Libraries/LibJS/Rust/src/parser/base.rs@@ -22,6 +22,10 @@ use super::columns::*;use super::intern::Interner;use super::token::*;use super::unit::Unit;+use ak::Zone;+use ak::ZoneMap;+use ak::ZoneSet;+use ak::ZoneVec;pub const USE_DEAD: u32 = u32::MAX;/// On the function of a use: the use is inside a `with`.@@ -223,7 +227,18 @@ pub(crate) struct ProgramContext {pub(crate) has_no_globals: bool,}+/// The state of a name that no scope declares or uses.+pub(crate) const UNUSED_NAME: NameState = NameState {+ use_head: NONE,+ use_scope: NONE,+ decl_head: NONE,+ stamp: 0,+};++/// A parser, whose work data is in a zone. It lives as long as one parse job: the first parse of a source, or the+/// parse of one function again.pub struct Parser<'a, U: Unit> {+ pub(crate) zone: &'a Zone,/// Where in `rows_by_params_start` the last lookup ended up.row_cursor: core::cell::Cell<usize>,/// Whether the tree is one for a sweep: rows get marks, and change their tag where a mark is about them.@@ -258,19 +273,19 @@ pub struct Parser<'a, U: Unit> {pub(crate) prev_end: u32,pub(crate) prev_start: u32,pub(crate) regex_body_end: u32,- pub(crate) name_buffer: Vec<u8>,- pub interner: Interner,+ pub(crate) name_buffer: ZoneVec<'a, u8>,+ pub interner: &'a mut Interner,pub ast: Ast,pub index: FunctionIndex,- pub(crate) scratch: Vec<u32>,+ pub(crate) scratch: ZoneVec<'a, u32>,pub(crate) mode: Mode,pub(crate) retain: bool,pub module: bool,pub strict: bool,pub(crate) ctx: u32,- pub(crate) labels: Vec<Label>,+ pub(crate) labels: ZoneVec<'a, Label>,pub(crate) labels_base: u32,pub(crate) stack_limit: usize,recursion_depth: u32,@@ -289,18 +304,20 @@ pub struct Parser<'a, U: Unit> {/// Whether this is the left side of `for await`, where `async of` is no arrow function.pub(crate) for_await_head: bool,- pub(crate) names: Vec<NameState>,- pub(crate) uses: Vec<UseRow>,- pub(crate) decls: Vec<DeclRow>,- pub(crate) scopes: Vec<Scope>,+ /// The state of each name, by its id. A parse of a function again has the states of its reparse context, which+ /// are all clean when it starts and ends.+ pub(crate) names: &'a mut [NameState],+ pub(crate) uses: ZoneVec<'a, UseRow>,+ pub(crate) decls: ZoneVec<'a, DeclRow>,+ pub(crate) scopes: ZoneVec<'a, Scope>,pub(crate) cur_scope: u32,pub(crate) cur_func: u32,pub(crate) next_scope: u32,pub(crate) next_func: u32,/// The index row of each function id, or NONE for what only counts as a function.- pub(crate) function_rows: Vec<u32>,+ pub(crate) function_rows: ZoneVec<'a, u32>,/// The row whose summary tells of the scopes of each function id, which is that of the function around a `with`.- pub(crate) summary_rows: Vec<u32>,+ pub(crate) summary_rows: ZoneVec<'a, u32>,pub(crate) stamp: u32,pub has_top_level_await: bool,@@ -314,13 +331,21 @@ pub struct Parser<'a, U: Unit> {/// While reparsing: where the function being reparsed starts over.pub(crate) reparse_target: u32,pub(crate) with_depth: u32,- pub(crate) names_used_in_with: Vec<u32>,- pub(crate) free_names_scratch: Vec<u32>,+ pub(crate) names_used_in_with: ZoneVec<'a, u32>,+ pub(crate) free_names_scratch: ZoneVec<'a, u32>,/// The innermost label of each name, as its index in `labels`.- pub(crate) label_indices: std::collections::HashMap<u32, u32>,+ pub(crate) label_indices: ZoneMap<'a, u32, u32>,/// The undeclared names that something in a `with` uses, which are not simply globals.- pub global_names_used_in_with: Vec<u32>,- pub(crate) hoists: Vec<Hoist>,+ pub global_names_used_in_with: ZoneVec<'a, u32>,+ pub(crate) hoists: ZoneVec<'a, Hoist>,+ /// The captures in the order they got resolved, as (index row, name).+ pub(crate) pending_captures: ZoneVec<'a, (u32, u32)>,+ /// (index row, entry) for a binding that comes up early, and with bit 31 of the row set, (index row, ordinal) for+ /// one that is captured.+ pub(crate) pending_summaries: ZoneVec<'a, (u32, u32)>,+ /// (index row, where the scope starts, entry) for a binding of a scope inside a function that has to exist when the+ /// scope starts.+ pub(crate) pending_scope_bindings: ZoneVec<'a, (u32, u32, u32)>,pub(crate) uses_this: bool,pub(crate) uses_this_from_environment: bool,pub(crate) might_need_arguments: bool,@@ -338,17 +363,17 @@ pub struct Parser<'a, U: Unit> {pub(crate) in_arrow: bool,/// The private names of each open class, created when a class first declares one.- pub(crate) private_declared: Vec<std::collections::HashMap<u32, u8>>,- pub(crate) private_used: Vec<(u32, u32)>,- pub(crate) class_marks: Vec<(u32, u32)>,- pub(crate) export_names: std::collections::HashSet<u32>,- pub(crate) export_locals: Vec<(u32, u32)>,+ pub(crate) private_declared: ZoneVec<'a, ZoneMap<'a, u32, u8>>,+ pub(crate) private_used: ZoneVec<'a, (u32, u32)>,+ pub(crate) class_marks: ZoneVec<'a, (u32, u32)>,+ pub(crate) export_names: ZoneSet<'a, u32>,+ pub(crate) export_locals: ZoneVec<'a, (u32, u32)>,pub err: Option<PendingError>,/// Whether this is the code of an eval, whose private names the code around it may declare.pub initiated_by_eval: bool,/// The private names that no class in this code declares, for an eval.- pub eval_referenced_private_names: Vec<u32>,+ pub eval_referenced_private_names: ZoneVec<'a, u32>,_marker: core::marker::PhantomData<&'a [U]>,}@@ -365,24 +390,54 @@ pub(crate) const MAX_SUBSCRIPT_CHAIN_LENGTH: u32 = 10000;pub(crate) const RECURSION_LIMIT_MESSAGE: &str = "Maximum parser recursion depth exceeded";impl<'a, U: Unit> Parser<'a, U> {+ /// A parser for the first parse of `source`, with its work data in `zone` and the names it finds in `interner`.+ ////// # Safety/// `source` must be followed by at least 64 units of zeros within the same allocation.- pub unsafe fn new(source: &'a [U], module: bool, mode: Mode) -> Self {+ pub unsafe fn new(zone: &'a Zone, interner: &'a mut Interner, source: &'a [U], module: bool, mode: Mode) -> Self {let estimated_nodes = match mode {Mode::Lazy => (source.len() / 3 + 16).min(1 << 16),Mode::Full => source.len() / 3 + 16,};let estimated_functions = source.len() / 128;+ let estimated_uses = (source.len() / 8 + 16).min(1 << 16);+ let estimated_names = if estimated_uses < 1 << 16 {+ interner.len().next_power_of_two()+ } 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 mut parser = Self::with_capacities(+ zone,source.as_ptr(),source.len(),module,mode,- Interner::new(source.len()),+ interner,+ zone.alloc_slice_fill(estimated_names, UNUSED_NAME),+ index,estimated_nodes,- estimated_functions,- (source.len() / 8 + 16).min(1 << 16),+ estimated_uses,);+ parser.pending_captures.reserve(estimated_functions * 2);if source.len() >= u32::MAX as usize - 64 {parser.len = 0;parser.fail(0, "Source too long");@@ -402,48 +457,55 @@ impl<'a, U: Unit> Parser<'a, U> {}}- /// A parser that only parses functions again, one at a time, knowing what the first parse of their source found.- /// Its source comes with each function.+ /// A parser that parses one function again, knowing what the first parse of its source found, with its work data in+ /// `zone`. The names of the source, their states and the index the parser writes to come from the reparse context+ /// of the source, and go back to it when the parse ends (see `end_reparse()`). Its source comes with the function.////// # Safety/// `first_pass_index` must stay valid and unchanged for as long as the parser reparses functions.+ #[allow(clippy::too_many_arguments)]pub(crate) unsafe fn for_reparsing(+ zone: &'a Zone,module: bool,- interner: Interner,+ interner: &'a mut Interner,+ names: &'a mut [NameState],+ stamp: u32,+ index: FunctionIndex,first_pass_index: &FunctionIndex,source_length: u32,) -> Self {let mut parser = Self::with_capacities(+ zone,core::ptr::null(),source_length as usize,module,Mode::Lazy,interner,+ names,+ index,256,- 0,256,);+ parser.stamp = stamp;parser.first_pass_index = first_pass_index;parser}#[allow(clippy::too_many_arguments)]fn with_capacities(+ zone: &'a Zone,source: *const U,length: usize,module: bool,mode: Mode,- interner: Interner,+ interner: &'a mut Interner,+ names: &'a mut [NameState],+ index: FunctionIndex,estimated_nodes: usize,- estimated_functions: usize,estimated_uses: usize,) -> Self {- let estimated_names = if estimated_uses < 1 << 16 {- interner.len().next_power_of_two()- } else {- 1 << 12- };let mut parser = Self {+ zone,row_cursor: core::cell::Cell::new(0),marks: false,pseudo_function_row: NONE,@@ -468,38 +530,17 @@ impl<'a, U: Unit> Parser<'a, U> {prev_end: 0,prev_start: 0,regex_body_end: 0,- name_buffer: Vec::new(),+ name_buffer: ZoneVec::new_in(zone),interner,ast: Ast::with_capacity(estimated_nodes),- 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(),- pending_captures: Vec::with_capacity(estimated_functions * 2),- summaries: Vec::new(),- declared_names: Vec::new(),- summary_start: Vec::new(),- pending_summaries: Vec::new(),- pending_scope_bindings: Vec::new(),- program_summary_end: 0,- program_has_direct_eval: false,- ..FunctionIndex::default()- },- scratch: Vec::with_capacity(estimated_uses.min(4096)),+ index,+ scratch: ZoneVec::with_capacity_in(estimated_uses.min(4096), zone),mode,retain: true,module,strict: module,ctx: if module { ctx::CAN_AWAIT } else { 0 },- labels: Vec::new(),+ labels: ZoneVec::new_in(zone),labels_base: 0,stack_limit: 0,recursion_depth: 0,@@ -512,24 +553,16 @@ impl<'a, U: Unit> Parser<'a, U> {last_arrow: 0,parenthesized_expression_start: NONE,for_await_head: false,- names: vec![- NameState {- use_head: NONE,- use_scope: NONE,- decl_head: NONE,- stamp: 0- };- estimated_names- ],- uses: Vec::with_capacity(estimated_uses),- decls: Vec::with_capacity(estimated_uses / 16),- scopes: Vec::with_capacity((length / 64 + 1).min(256)),+ names,+ uses: ZoneVec::with_capacity_in(estimated_uses, zone),+ decls: ZoneVec::with_capacity_in(estimated_uses / 16, zone),+ scopes: ZoneVec::with_capacity_in((length / 64 + 1).min(256), zone),cur_scope: 0,cur_func: 0,next_scope: 1,next_func: 1,- function_rows: vec![NONE],- summary_rows: vec![PROGRAM_SUMMARY_ROW],+ function_rows: ZoneVec::from_slice_in(&[NONE], zone),+ summary_rows: ZoneVec::from_slice_in(&[PROGRAM_SUMMARY_ROW], zone),stamp: 0,has_top_level_await: false,first_token_start: 0,@@ -540,11 +573,14 @@ impl<'a, U: Unit> Parser<'a, U> {first_pass_index: core::ptr::null(),reparse_target: NONE,with_depth: 0,- names_used_in_with: Vec::new(),- free_names_scratch: Vec::new(),- label_indices: std::collections::HashMap::new(),- global_names_used_in_with: Vec::new(),- hoists: Vec::new(),+ names_used_in_with: ZoneVec::new_in(zone),+ free_names_scratch: ZoneVec::new_in(zone),+ label_indices: ZoneMap::new_in(zone),+ global_names_used_in_with: ZoneVec::new_in(zone),+ hoists: ZoneVec::new_in(zone),+ pending_captures: ZoneVec::new_in(zone),+ pending_summaries: ZoneVec::new_in(zone),+ pending_scope_bindings: ZoneVec::new_in(zone),uses_this: false,uses_this_from_environment: false,might_need_arguments: false,@@ -554,14 +590,14 @@ impl<'a, U: Unit> Parser<'a, U> {in_class_of_sloppy_function: false,in_arrow: false,- private_declared: Vec::new(),- private_used: Vec::new(),- class_marks: Vec::new(),- export_names: std::collections::HashSet::new(),- export_locals: Vec::new(),+ private_declared: ZoneVec::new_in(zone),+ private_used: ZoneVec::new_in(zone),+ class_marks: ZoneVec::new_in(zone),+ export_names: ZoneSet::new_in(zone),+ export_locals: ZoneVec::new_in(zone),err: None,initiated_by_eval: false,- eval_referenced_private_names: Vec::new(),+ eval_referenced_private_names: ZoneVec::new_in(zone),_marker: core::marker::PhantomData,};// NB: Code can start parsing with less stack left than the budget, as eval near the end of the stack does.@@ -611,7 +647,7 @@ impl<'a, U: Unit> Parser<'a, U> {/// Goes through the top level once more with marks for a sweep, past all functions, after a parse that went well.pub(crate) fn parse_top_level_rows(&mut self, context: ProgramContext) -> Result<(Ast, u32), super::OutOfStack> {let tree = std::mem::replace(&mut self.ast, Ast::with_capacity(256));- let private_names = std::mem::take(&mut self.eval_referenced_private_names);+ let private_names = self.eval_referenced_private_names.take();let (first_token_start, strict, has_top_level_await) =(self.first_token_start, self.strict, self.has_top_level_await);self.finish_reparse();@@ -669,8 +705,8 @@ impl<'a, U: Unit> Parser<'a, U> {/// Groups the captures by function, now that all of them are known.fn group_captures(&mut self) {+ let pending = self.pending_captures.take();let index = &mut self.index;- let pending = std::mem::take(&mut index.pending_captures);for &(row, _) in &pending {index.capture_count[row as usize] += 1;}@@ -680,7 +716,7 @@ impl<'a, U: Unit> Parser<'a, U> {next += index.capture_count[row];}index.capture_names.resize(next as usize, 0);- let mut filled = vec![0u32; index.capture_start.len()];+ let filled = self.zone.alloc_slice_fill(index.capture_start.len(), 0u32);for (row, name) in pending {let slot = index.capture_start[row as usize] + filled[row as usize];filled[row as usize] += 1;@@ -710,16 +746,16 @@ impl<'a, U: Unit> Parser<'a, U> {}}}+ let mut pending = self.pending_summaries.take();let index = &mut self.index;- let mut pending = std::mem::take(&mut index.pending_summaries);pending.sort_unstable();// NB: Rows with bit 31 set sort after all the others, so that each kind is a run of its own.let captured_start = pending.partition_point(|&(row, _)| row & SUMMARY_SLOT_EARLY == 0);let (mut early, mut captured) = (0, captured_start);- let mut scope_bindings = std::mem::take(&mut index.pending_scope_bindings);+ let mut scope_bindings = self.pending_scope_bindings.take();scope_bindings.sort_unstable();let mut scope_binding = 0;- let mut bitmap = Vec::new();+ let mut bitmap = ZoneVec::new_in(self.zone);// NB: The top level has no row of its own, so its summary comes first, and it has only scopes in it.let of_program = scope_bindings.partition_point(|binding| binding.0 != PROGRAM_SUMMARY_ROW);write_varint(&mut index.summaries, 0);@@ -818,13 +854,12 @@ impl<'a, U: Unit> Parser<'a, U> {if scope.flags & scope_flag::VAR_SCOPE == 0 {// NB: A `case` can be skipped, so that a declaration in a switch is no sign of having been initialized.if is_early || is_captured || scope.flags & scope_flag::SWITCH != 0 {- self.index.pending_scope_bindings.push((index_row, scope.start, entry));+ self.pending_scope_bindings.push((index_row, scope.start, entry));}} else if is_early {- self.index.pending_summaries.push((index_row, entry));+ self.pending_summaries.push((index_row, entry));} else if is_captured {- self.index- .pending_summaries+ self.pending_summaries.push((index_row | SUMMARY_SLOT_EARLY, summary_slot));}}@@ -889,11 +924,11 @@ impl<'a, U: Unit> Parser<'a, U> {return "Unexpected end of input".to_string();}let end = self.tok_end.min(self.tok_start + LONGEST_TOKEN_SHOWN);- let units: Vec<u16> = (self.tok_start..end)- .map(|index| self.at(index as usize) as u16)+ let token: String = char::decode_utf16((self.tok_start..end).map(|index| self.at(index as usize) as u16))+ .map(|unit| unit.unwrap_or(char::REPLACEMENT_CHARACTER)).collect();let ellipsis = if end < self.tok_end { "..." } else { "" };- format!("Unexpected token '{}{ellipsis}'", String::from_utf16_lossy(&units))+ format!("Unexpected token '{token}{ellipsis}'")}#[inline(always)]@@ -1137,15 +1172,9 @@ impl<'a, U: Unit> Parser<'a, U> {#[inline(never)]fn grow_names(&mut self, name: u32) {let new_len = ((name as usize + 1) * 2).next_power_of_two();- self.names.resize(- new_len,- NameState {- use_head: NONE,- use_scope: NONE,- decl_head: NONE,- stamp: 0,- },- );+ let names = self.zone.alloc_slice_fill(new_len, UNUSED_NAME);+ names[..self.names.len()].copy_from_slice(self.names);+ self.names = names;}#[inline(always)]@@ -1218,9 +1247,7 @@ impl<'a, U: Unit> Parser<'a, U> {if use_func != func {let index_row = self.function_rows[use_func as usize];if index_row != NONE {- self.index- .pending_captures- .push((index_row, name | (row.func & USE_IN_WITH)));+ self.pending_captures.push((index_row, name | (row.func & USE_IN_WITH)));}}use_row = row.prev;@@ -1388,7 +1415,7 @@ impl<'a, U: Unit> Parser<'a, U> {// SAFETY: Every function id has a row here.let index_row = unsafe { *self.function_rows.get_unchecked(func as usize) };if index_row != NONE {- self.index.pending_captures.push((index_row, name | in_with));+ self.pending_captures.push((index_row, name | in_with));}}head = row.prev;@@ -1564,7 +1591,7 @@ impl<'a, U: Unit> Parser<'a, U> {if use_func != func {let index_row = self.function_rows[use_func as usize];if index_row != NONE {- self.index.pending_captures.push((index_row, name | in_with));+ self.pending_captures.push((index_row, name | in_with));}}}@@ -2179,6 +2206,13 @@ impl<'a, U: Unit> Parser<'a, 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, FunctionIndex, u32) {+ self.finish_reparse();+ (self.ast, self.index, self.stamp)+ }+/// Gets ready for the next function, whether this one went well or not.pub(crate) fn finish_reparse(&mut self) {for row in &self.uses {@@ -2246,7 +2280,7 @@ impl<'a, U: Unit> Parser<'a, U> {self.open_scope_with(0, 0);// The class around the function, if any, already checked its private names.self.class_marks.push((0, 0));- self.private_declared.push(std::collections::HashMap::new());+ self.private_declared.push(ZoneMap::new_in(self.zone));self.pos = params_start;self.tok_end = params_start;self.prev_end = params_start;@@ -2374,7 +2408,7 @@ impl<'a, U: Unit> Parser<'a, U> {pub(crate) fn note_uses_of_nested_functions(&mut self, mark: u32, func: u32) {self.stamp += 1;let stamp = self.stamp;- let mut nested_names = std::mem::take(&mut self.free_names_scratch);+ let mut nested_names = self.free_names_scratch.take();nested_names.clear();for row in &self.uses[mark as usize..] {if row.name == USE_DEAD || row.func & !USE_IN_WITH == func {@@ -2414,7 +2448,7 @@ impl<'a, U: Unit> Parser<'a, U> {fn collapse_uses(&mut self, mark: u32, func: u32) {self.stamp += 1;let stamp = self.stamp;- let mut free_names = std::mem::take(&mut self.free_names_scratch);+ let mut free_names = self.free_names_scratch.take();free_names.clear();let mut i = self.uses.len();while i > mark as usize {diff --git a/Libraries/LibJS/Rust/src/parser/columns.rs b/Libraries/LibJS/Rust/src/parser/columns.rsindex 5a1de6f1d9c..2d12368a11c 100644--- a/Libraries/LibJS/Rust/src/parser/columns.rs+++ b/Libraries/LibJS/Rust/src/parser/columns.rs@@ -151,21 +151,6 @@ impl Ast {ast}- /// Columns with no rows and no memory, which are not a tree yet.- pub fn unallocated() -> Self {- Self {- tag: Vec::new(),- pos: Vec::new(),- a: Vec::new(),- b: Vec::new(),- extra: Vec::new(),- binding_name: Vec::new(),- binding_flags: Vec::new(),- scope_first_binding: Vec::new(),- scope_flags: Vec::new(),- }- }-/// Goes back to what a new tree starts with, keeping the memory.pub fn reset(&mut self) {self.truncate(&AstMarks {@@ -258,8 +243,6 @@ pub struct FunctionIndex {/// 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>,- /// While parsing: the captures in the order they got resolved, as (index row, name).- pub pending_captures: Vec<(u32, 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>>,@@ -273,12 +256,6 @@ pub struct FunctionIndex {pub summary_start: Vec<u32>,/// A bit for every name of the source that anything declares, which all other names are free everywhere.pub declared_names: Vec<u64>,- /// While parsing: (index row, entry) for a binding that comes up early, and with bit 31 of the row set, (index row,- /// ordinal) for one that is captured.- pub pending_summaries: Vec<(u32, u32)>,- /// While parsing: (index row, where the scope starts, entry) for a binding of a scope inside a function that has to- /// exist when the scope starts.- pub pending_scope_bindings: Vec<(u32, u32, u32)>,/// 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.@@ -418,11 +395,8 @@ impl FunctionIndex {self.capture_start.shrink_to_fit();self.capture_count.shrink_to_fit();self.capture_names.shrink_to_fit();- self.pending_captures = Vec::new();self.summaries.shrink_to_fit();self.summary_start.shrink_to_fit();- self.pending_summaries = Vec::new();- self.pending_scope_bindings = Vec::new();}}diff --git a/Libraries/LibJS/Rust/src/parser/lexer.rs b/Libraries/LibJS/Rust/src/parser/lexer.rsindex d7002fe4809..46a5ac22052 100644--- a/Libraries/LibJS/Rust/src/parser/lexer.rs+++ b/Libraries/LibJS/Rust/src/parser/lexer.rs@@ -488,7 +488,7 @@ impl<U: Unit> Parser<'_, U> {let mut p = start;let mut escaped = false;let mut first = true;- let mut buffer = std::mem::take(&mut self.name_buffer);+ let mut buffer = self.name_buffer.take();buffer.clear();loop {let (mut c, width) = self.code_point_at(p);@@ -883,12 +883,10 @@ impl<U: Unit> Parser<'_, U> {}p += width;}- let units_of = |parser: &Self, start: usize, end: usize| -> Vec<u16> {- (start..end).map(|i| parser.at(i) as u16).collect()- };- let flags = units_of(self, flags_start, p);+ let zone = self.zone;+ let flags: &[u16] = zone.alloc_slice_from_iter((flags_start..p).map(|i| self.at(i) as u16));let mut seen = [false; 128];- for &flag in &flags {+ for &flag in flags {if flag >= 128 || !b"dgimsuvy".contains(&(flag as u8)) {let flag = char::from_u32(flag as u32).unwrap_or('?');self.fail_with(self.tok_start, format!("Invalid RegExp flag '{flag}'"));@@ -901,8 +899,9 @@ impl<U: Unit> Parser<'_, U> {}seen[flag as usize] = true;}- let pattern = units_of(self, start + 1, self.regex_body_end as usize);- if let Err(message) = crate::host::validate_regex(&pattern, &flags) {+ let pattern: &[u16] =+ zone.alloc_slice_from_iter((start + 1..self.regex_body_end as usize).map(|i| self.at(i) as u16));+ if let Err(message) = crate::host::validate_regex(pattern, flags) {self.fail_with(start as u32, String::from_utf16_lossy(&message.to_utf16()));return;}diff --git a/Libraries/LibJS/Rust/src/parser/mod.rs b/Libraries/LibJS/Rust/src/parser/mod.rsindex fc916bcdcbd..3743a2367a2 100644--- a/Libraries/LibJS/Rust/src/parser/mod.rs+++ b/Libraries/LibJS/Rust/src/parser/mod.rs@@ -59,16 +59,19 @@ const SOURCE_PADDING: usize = 64;#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]pub struct FunctionRow(pub(crate) u32);-/// A parser that keeps reparsing the functions of one source: it knows the names of the source.-enum ReparseContext {- Narrow(Box<Parser<'static, u8>>),- Wide(Box<Parser<'static, u16>>),+/// What the parses of the functions of one source again hand on to each other. A parse of a function has its work data+/// in the work zone of its thread, and this on the heap.+struct ReparseContext {+ /// The names of the source.+ interner: Interner,+ /// The state of each name, which is clean between two parses.+ names: 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,}-// SAFETY: The parser of a context only points at what the ScriptData holding the context owns, or, during a reparse, into-// the memory of that reparse.-unsafe impl Send for ReparseContext {}-/// The sources that have a reparse context now, for [`release_idle_reparse_contexts()`].static SOURCES_WITH_REPARSE_CONTEXT: Mutex<Vec<Weak<ScriptData>>> = Mutex::new(Vec::new());@@ -143,6 +146,15 @@ struct SpareTreeMemory {function_scope_rows: Vec<(u32, u32)>,}+/// What a parse of a function again is for.+struct Reparse {+ /// The memory for the tree.+ ast: Ast,+ row: FunctionRow,+ stack_limit: usize,+ inline_argument_limit: usize,+}+/// The most nodes of a tree whose memory stays around.const MAXIMUM_SPARE_NODES: usize = 1 << 16;@@ -262,7 +274,7 @@ impl ScriptData {function_scope_rows: Vec::new(),});let source = self.positions.source.get();- let (ast, node) = {+ let (ast, node) = with_work_zone(|zone| {let mut context = self.reparse_context.lock().expect("a reparse does not panic");if context.is_none() {*context = Some(self.make_reparse_context());@@ -273,34 +285,22 @@ impl ScriptData {}self.reparse_context_idle_collections.store(0, Ordering::Relaxed);let context = context.as_mut().expect("the context was just filled in");- let result = match (context, source.as_units()) {- (- ReparseContext::Narrow(parser),- ak::Utf16StringUnits::Ascii(units) | ak::Utf16StringUnits::Latin1(units),- ) => {- parser.tracks_scopes = false;- parser.marks = true;- parser.inline_argument_limit = inline_argument_limit;- let result = self.reparse_code_units(parser, memory.ast, units, row, stack_limit);- parser.tracks_scopes = true;- parser.marks = false;- result- }- (ReparseContext::Wide(parser), ak::Utf16StringUnits::Utf16(units)) => {- parser.tracks_scopes = false;- parser.marks = true;- parser.inline_argument_limit = inline_argument_limit;- let result = self.reparse_code_units(parser, memory.ast, units, row, stack_limit);- parser.tracks_scopes = true;- parser.marks = false;- result+ let reparse = Reparse {+ ast: memory.ast,+ row,+ stack_limit,+ inline_argument_limit,+ };+ let result = match source.as_units() {+ ak::Utf16StringUnits::Ascii(units) | ak::Utf16StringUnits::Latin1(units) => {+ self.reparse_code_units(zone, context, units, reparse)}- _ => unreachable!("the storage of a source does not change"),+ ak::Utf16StringUnits::Utf16(units) => self.reparse_code_units(zone, context, units, reparse),};// NB: Collections counted on while the context was held, and it was not idle then.self.reparse_context_idle_collections.store(0, Ordering::Relaxed);result- }?;+ })?;let tree = SyntaxTree {script: self.clone(),source,@@ -320,18 +320,13 @@ impl ScriptData {}fn make_reparse_context(&self) -> ReparseContext {- let module = self.program_type == ProgramType::Module;- let length = u32_from_usize(self.positions.source.length_in_code_units());- let is_wide = self.positions.source.is_wide();let interner = Interner::with_names(self.names.parser_names());- // SAFETY: The context lives in this ScriptData, whose index stays where it is, since it is shared by an Arc,- // and never changes.- unsafe {- if is_wide {- ReparseContext::Wide(Box::new(Parser::for_reparsing(module, interner, &self.index, length)))- } else {- ReparseContext::Narrow(Box::new(Parser::for_reparsing(module, interner, &self.index, length)))- }+ let names = vec![base::UNUSED_NAME; interner.len().next_power_of_two()].into_boxed_slice();+ ReparseContext {+ interner,+ names,+ stamp: 0,+ index: FunctionIndex::default(),}}@@ -340,21 +335,37 @@ impl ScriptData {/// to have.fn reparse_code_units<U: Unit + Default>(&self,- parser: &mut Parser<'static, U>,- ast: Ast,+ zone: &Zone,+ context: &mut ReparseContext,source: &[U],- row: FunctionRow,- stack_limit: usize,+ reparse: Reparse,) -> Result<(Ast, u32), OutOfStack> {- let row_index = row.0 as usize;+ let row_index = reparse.row.0 as usize;let needs_padding = self.index.flags[row_index] & function_flag::REACHES_SOURCE_END != 0;- let mut tail = Vec::new();+ let name_count = context.interner.len();+ // SAFETY: The index of this ScriptData stays where it is, since the ScriptData is shared by an Arc, and never+ // changes.+ let mut parser = unsafe {+ Parser::for_reparsing(+ zone,+ self.program_type == ProgramType::Module,+ &mut context.interner,+ &mut context.names,+ context.stamp,+ core::mem::take(&mut context.index),+ &self.index,+ u32_from_usize(source.len()),+ )+ };+ parser.tracks_scopes = false;+ parser.marks = true;+ parser.inline_argument_limit = reparse.inline_argument_limit;// SAFETY: The source outlives the parse, and the first parse found out that the lexer stays clear of its end// unless the tail is what it reads.unsafe {if needs_padding {let first_read = self.index.params_start[row_index] as usize;- tail.reserve_exact(source.len() - first_read + SOURCE_PADDING);+ let mut tail = ZoneVec::with_capacity_in(source.len() - first_read + SOURCE_PADDING, zone);tail.extend_from_slice(&source[first_read..]);tail.resize(tail.capacity(), U::default());parser.set_reparse_source(tail.as_ptr(), u32_from_usize(first_read), u32_from_usize(source.len()));@@ -362,12 +373,19 @@ impl ScriptData {parser.set_reparse_source(source.as_ptr(), 0, u32_from_usize(source.len()));}}- parser.stack_limit = stack_limit;- parser.ast = ast;- let node = parser.reparse_function(row.0);+ parser.stack_limit = reparse.stack_limit;+ parser.ast = reparse.ast;+ let node = parser.reparse_function(reparse.row.0);let error = parser.err.take();- let ast = std::mem::replace(&mut parser.ast, Ast::unallocated());- parser.finish_reparse();+ let (ast, index, stamp) = parser.end_reparse();+ context.index = index;+ context.stamp = stamp;+ // NB: The name states of the context cover the names it has, and no others.+ assert_eq!(+ context.interner.len(),+ name_count,+ "a function has only names that the first parse of its source found"+ );match error {None => Ok((ast, node)),Some(error) => {@@ -596,7 +614,8 @@ struct ParserOutput {first_token_start: u32,is_strict_mode: bool,has_top_level_await: bool,- eval_referenced_private_names: Vec<u32>,+ eval_referenced_private_names: Vec<Name>,+ /// Sorted, without duplicates.global_names_used_in_with: Vec<u32>,lines: LineTable,}@@ -611,9 +630,12 @@ fn parse_code_units<U: Unit + Default>(let mut padded = ZoneVec::with_capacity_in(source.len() + SOURCE_PADDING, zone);padded.extend_from_slice(source);padded.resize(source.len() + SOURCE_PADDING, U::default());+ let mut interner = Interner::new(source.len());// SAFETY: The source is followed by the padding.let mut parser = unsafe {Parser::new(+ zone,+ &mut interner,&padded[..source.len()],options.program_type == ProgramType::Module,if options.keep_whole_tree {@@ -638,7 +660,10 @@ fn parse_code_units<U: Unit + Default>(}let top_level_rows =(purpose == ParsePurpose::Compile).then(|| parser.parse_top_level_rows(options.program_context()));- Ok(ParserOutput {+ let mut global_names_used_in_with = parser.global_names_used_in_with.take();+ global_names_used_in_with.sort_unstable();+ global_names_used_in_with.dedup();+ let output = ParserOutput {top_level_rows,root,first_token_start: parser.first_token_start,@@ -646,11 +671,16 @@ fn parse_code_units<U: Unit + Default>(has_top_level_await: parser.has_top_level_await,ast: parser.ast,index: parser.index,- interner: parser.interner,- eval_referenced_private_names: parser.eval_referenced_private_names,- global_names_used_in_with: parser.global_names_used_in_with,+ eval_referenced_private_names: parser+ .eval_referenced_private_names+ .iter()+ .map(|&name| Name(name))+ .collect(),+ global_names_used_in_with: global_names_used_in_with.to_vec(),lines,- })+ interner,+ };+ Ok(output)}/// Parses `source` at the width of its storage, with the work zone of the thread for the work data of the parse.@@ -674,9 +704,6 @@ pub fn parse_to_tree(source: &ak::Utf16String, options: &ParseOptions) -> Resultlet default_constructor_parameter_name = Name(output.interner.intern_bytes(b"args"));let names =NameTable::from_parser_names((0..output.interner.len()).map(|name| output.interner.text(u32_from_usize(name))));- let mut global_names_used_in_with = output.global_names_used_in_with;- global_names_used_in_with.sort_unstable();- global_names_used_in_with.dedup();output.index.shrink_to_fit();let script = Arc::new(ScriptData {index: output.index,@@ -690,7 +717,7 @@ pub fn parse_to_tree(source: &ak::Utf16String, options: &ParseOptions) -> Resulttop_level_rows: Mutex::new(output.top_level_rows),default_export_name,default_constructor_parameter_name,- global_names_used_in_with,+ global_names_used_in_with: output.global_names_used_in_with,reparse_context: Mutex::new(None),reparse_context_idle_collections: AtomicU8::new(0),retains_compiled_functions: AtomicBool::new(false),@@ -704,11 +731,7 @@ pub fn parse_to_tree(source: &ak::Utf16String, options: &ParseOptions) -> Resultfirst_token_start: output.first_token_start,is_strict_mode: output.is_strict_mode,has_top_level_await: options.program_type == ProgramType::Module && output.has_top_level_await,- eval_referenced_private_names: output- .eval_referenced_private_names- .iter()- .map(|&name| Name(name))- .collect(),+ eval_referenced_private_names: output.eval_referenced_private_names,resolutions: Vec::new(),scopes: ScopeTable::default(),program_scope_row: 0,diff --git a/Libraries/LibJS/Rust/src/parser/stmt.rs b/Libraries/LibJS/Rust/src/parser/stmt.rsindex 324cee40b75..7eb83459fe9 100644--- a/Libraries/LibJS/Rust/src/parser/stmt.rs+++ b/Libraries/LibJS/Rust/src/parser/stmt.rs@@ -12,6 +12,9 @@ use super::expr::is_name_like;use super::expr::starts_expression;use super::token::*;use super::unit::Unit;+use ak::ZoneMap;+use ak::ZoneSet;+use ak::ZoneVec;#[derive(Clone, Copy, PartialEq, Eq)]pub(crate) enum Context {@@ -1333,7 +1336,7 @@ impl<U: Unit> Parser<'_, U> {}self.class_marks.push((self.private_declared.len() as u32, self.private_used.len() as u32));- self.private_declared.push(std::collections::HashMap::new());+ self.private_declared.push(ZoneMap::new_in(self.zone));self.expect(T::LBrace);let mark = self.scratch.len();let mut had_constructor = false;@@ -1402,7 +1405,7 @@ impl<U: Unit> Parser<'_, U> {}pub(crate) fn check_unresolved_private_names(&mut self) {- for (name, pos) in std::mem::take(&mut self.private_used) {+ for (name, pos) in self.private_used.take() {self.report_undeclared_private_name(name, pos);}}@@ -1778,7 +1781,8 @@ impl<U: Unit> Parser<'_, U> {/// formed, which also rules out lone surrogates written as escapes.fn intern_string_literal(&mut self) -> u32 {let (start, end) = (self.tok_start as usize + 1, self.tok_end as usize - 1);- let mut buffer = Vec::with_capacity(end - start);+ let mut buffer = self.name_buffer.take();+ buffer.clear();let mut p = start;while p < end {let mut c = self.at(p);@@ -1831,7 +1835,9 @@ impl<U: Unit> Parser<'_, U> {}}}- self.interner.intern_bytes(&buffer)+ let name = self.interner.intern_bytes(&buffer);+ self.name_buffer = buffer;+ name}/// Returns the list of keys and values, as interned names, or zero if there are none.@@ -1842,7 +1848,7 @@ impl<U: Unit> Parser<'_, U> {self.next();self.expect(T::LBrace);let mark = self.scratch.len();- let mut seen = std::collections::HashSet::new();+ let mut seen = ZoneSet::new_in(self.zone);while self.tok != T::RBrace && self.tok != T::Eof {let key = if self.tok == T::Str {self.intern_string_literal()@@ -2051,7 +2057,7 @@ impl<U: Unit> Parser<'_, U> {let mark = self.scratch.len();let mut had_string_local = false;let locals_mark = self.export_locals.len();- let mut pending: Vec<(u32, u32)> = Vec::new();+ let mut pending = ZoneVec::new_in(self.zone);while self.tok != T::RBrace && self.tok != T::Eof {let local_start = self.tok_start;let local_kind = self.tok;