Skip to content

Truly independent web browser

LibJS: Let the columns of a large tree grow in one step

Andreas Kling2714de659ecc
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index 48551e5ce5f..fd72b4f25d5 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -395,9 +395,12 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
/// `source` must be followed by at least 64 units of zeros within the same allocation.
pub unsafe fn new(zone: &'a Zone, source: &'a [U], module: bool, mode: Mode) -> Self {
let interner = Interner::new(zone, source.len());
+ // NB: A lazy parse skips the code of functions, which is most of the code of most sources, so a tree of the
+ // whole source is only the most it is likely to have.
+ let whole_source_nodes = source.len() / 3 + 16;
let estimated_nodes = match mode {
- Mode::Lazy => (source.len() / 3 + 16).min(1 << 16),
- Mode::Full => source.len() / 3 + 16,
+ Mode::Lazy => whole_source_nodes.min(1 << 16),
+ Mode::Full => whole_source_nodes,
};
let estimated_functions = source.len() / 128;
let estimated_uses = (source.len() / 8 + 16).min(1 << 16);
@@ -419,6 +422,7 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
estimated_nodes,
estimated_uses,
);
+ parser.ast.likely_most_nodes = whole_source_nodes;
parser.pending_captures.reserve(estimated_functions * 2);
if source.len() >= u32::MAX as usize - 64 {
parser.len = 0;
@@ -632,8 +636,9 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {
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());
+ // NB: The rows are those of the tree, and marks, which some code has many of.
+ let nodes = self.ast.tag.len();
+ let rows = Ast::with_capacities(self.zone, nodes + nodes / 2, self.ast.extra.len());
let tree = std::mem::replace(&mut self.ast, rows);
let private_names = self.eval_referenced_private_names.take();
let (first_token_start, strict, has_top_level_await) =
diff --git a/Libraries/LibJS/Rust/src/parser/columns.rs b/Libraries/LibJS/Rust/src/parser/columns.rs
index 0586f7ff827..8e9781447e8 100644
--- a/Libraries/LibJS/Rust/src/parser/columns.rs
+++ b/Libraries/LibJS/Rust/src/parser/columns.rs
@@ -134,20 +134,28 @@ pub struct Ast<'z> {
/// One row per scope that has bindings: where its bindings start, and its flags.
pub scope_first_binding: ZoneVec<'z, u32>,
pub scope_flags: ZoneVec<'z, u32>,
+ /// The most nodes that the tree is likely to have, which the columns make room for at once if they outgrow what
+ /// they started with (see `grow()`).
+ pub likely_most_nodes: usize,
}
impl<'z> Ast<'z> {
pub fn with_capacity(zone: &'z Zone, nodes: usize) -> Self {
+ Self::with_capacities(zone, nodes, nodes / 2)
+ }
+
+ pub fn with_capacities(zone: &'z Zone, nodes: usize, extra: usize) -> Self {
let mut ast = Self {
tag: ZoneVec::with_capacity_in(nodes, zone),
pos: ZoneVec::with_capacity_in(nodes, zone),
a: ZoneVec::with_capacity_in(nodes, zone),
b: ZoneVec::with_capacity_in(nodes, zone),
- extra: ZoneVec::with_capacity_in(nodes / 2, zone),
+ extra: ZoneVec::with_capacity_in(extra, zone),
binding_name: ZoneVec::new_in(zone),
binding_flags: ZoneVec::new_in(zone),
scope_first_binding: ZoneVec::new_in(zone),
scope_flags: ZoneVec::new_in(zone),
+ likely_most_nodes: 0,
};
// Node zero stands for "no node" and for what failed to parse, and so does offset zero for "no list".
ast.push(tag::INVALID, 0, 0, 0);
@@ -177,12 +185,22 @@ impl<'z> Ast<'z> {
#[cold]
fn grow(&mut self) {
- let wanted = self.tag.capacity().max(1024);
+ // NB: Columns that grow in steps leave a copy in the zone at each step, which is memory the job has used. A
+ // tree that outgrows its first room takes room for the most nodes it is likely to have in one step, and
+ // room that is never written takes no memory.
+ let likely_most_nodes = core::mem::take(&mut self.likely_most_nodes);
+ let wanted = self
+ .tag
+ .capacity()
+ .max(1024)
+ .max(likely_most_nodes.saturating_sub(self.tag.len()));
self.tag.reserve(wanted);
let capacity = self.tag.capacity();
self.pos.reserve(capacity - self.pos.len());
self.a.reserve(capacity - self.a.len());
self.b.reserve(capacity - self.b.len());
+ // NB: A list of the tree takes one row of `extra` for each of its items, which are nodes.
+ self.extra.reserve(likely_most_nodes.saturating_sub(self.extra.len()));
}
pub fn truncate(&mut self, marks: &AstMarks) {
diff --git a/Libraries/LibJS/Rust/src/parser/mod.rs b/Libraries/LibJS/Rust/src/parser/mod.rs
index 0147da347ea..6cb46d03c48 100644
--- a/Libraries/LibJS/Rust/src/parser/mod.rs
+++ b/Libraries/LibJS/Rust/src/parser/mod.rs
@@ -265,9 +265,11 @@ impl ScriptData {
) -> Result<(Ast<'z>, u32), OutOfStack> {
let row_index = row.0 as usize;
// NB: A tree of the whole function is the most it is likely to have, as the tree skips the code of nested
- // functions. It starts with room for at most as many nodes as the work zone keeps memory for.
+ // functions. It starts with room for at most as many nodes as the work zone keeps memory for, and takes
+ // room for the rest in one step if it outgrows that.
let length = (self.index.end[row_index] - self.index.start[row_index]) as usize;
- let estimated_nodes = (length / 3 + 16).min(1 << 16);
+ let whole_function_nodes = length / 3 + 16;
+ let estimated_nodes = whole_function_nodes.min(1 << 16);
let needs_padding = self.index.flags[row_index] & function_flag::REACHES_SOURCE_END != 0;
let name_count = context.names.len();
// SAFETY: The index of this ScriptData stays where it is, since the ScriptData is shared by an Arc, and never
@@ -284,6 +286,7 @@ impl ScriptData {
estimated_nodes,
)
};
+ parser.ast.likely_most_nodes = whole_function_nodes;
parser.tracks_scopes = false;
parser.marks = true;
parser.inline_argument_limit = inline_argument_limit;