Skip to content

Truly independent web browser

LibJS: Allocate less for the names of a compile

Andreas Kling8b3766cd419f
diff --git a/Libraries/LibJS/Rust/src/ast.rs b/Libraries/LibJS/Rust/src/ast.rs
index 5c92f27e26a..214665ab095 100644
--- a/Libraries/LibJS/Rust/src/ast.rs
+++ b/Libraries/LibJS/Rust/src/ast.rs
@@ -566,7 +566,7 @@ pub struct SyntaxTree {
/// The row in `scopes` of each function node of the tree whose code the tree holds, sorted by node.
pub(crate) function_scope_rows: Vec<(u32, u32)>,
/// What scope analysis found out for the functions that the code of the tree goes on to compile.
- pub(crate) outer_resolutions: Option<Arc<OuterResolutions>>,
+ pub(crate) outer_resolutions: Option<OuterResolutions>,
}
impl SyntaxTree {
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 31024c9e32b..227b53fcf0e 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -1302,8 +1302,8 @@ impl Generator<'_> {
pub(super) fn sweep_parameter_names(&mut self, function: NodeId) {
let tree = self.tree;
let parameters = parameters_of(tree, function);
- let mut names = Vec::with_capacity(parameters.len());
- let mut shapes = Vec::new();
+ let mut names = std::mem::take(&mut self.context.scope.spare_parameter_names);
+ let mut shapes = std::mem::take(&mut self.context.scope.spare_parameter_shapes);
if self.context.scope.has_simple_parameters() {
names.extend(parameters.iter().map(|parameter| Name(tree.node_a(parameter))));
} else {
@@ -1322,6 +1322,10 @@ impl Generator<'_> {
}
}
self.declare_parameters(&names, &shapes);
+ names.clear();
+ shapes.clear();
+ self.context.scope.spare_parameter_names = names;
+ self.context.scope.spare_parameter_shapes = shapes;
}
/// The functions that a block declares exist from its start. `function` is the first one, and each knows the next.
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index d9e93770249..de675087c40 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -235,6 +235,9 @@ pub struct CodeScope {
/// What functions nested in scopes see of the bindings of those scopes, which are gone by the end of the function.
scope_resolutions: Vec<(u64, Resolution)>,
nested_functions: Vec<NestedFunction>,
+ /// The memory of what `sweep_parameter_names()` puts together.
+ pub(super) spare_parameter_names: Vec<Name>,
+ pub(super) spare_parameter_shapes: Vec<ParameterShape>,
/// What a name means that no scope around the function binds, once something asked.
unbound: Option<Resolution>,
}
@@ -396,8 +399,8 @@ fn read_varint(bytes: &[u8], at: &mut usize) -> u32 {
impl Generator<'_> {
/// Gets ready for the function at `row`, which the summary says the bindings of.
pub(super) fn begin_function_scope(&mut self, row: FunctionRow) {
- let script = self.tree.script.clone();
- let index = &script.index;
+ let tree = self.tree;
+ let index = &tree.script.index;
let flags = index.flags[row.0 as usize];
let (summary_start, summary_end) = index.summary_range(row.0);
let summary = &index.summaries[summary_start..summary_end];
@@ -1905,7 +1908,9 @@ impl Generator<'_> {
resolution,
));
}
- EnclosingCode::of_function(row, entries, &self.enclosing_code)
+ let enclosing_code = EnclosingCode::of_function(row, &mut entries, &self.enclosing_code);
+ 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 {
diff --git a/Libraries/LibJS/Rust/src/parser/resolve.rs b/Libraries/LibJS/Rust/src/parser/resolve.rs
index b8aadaa5077..20b6266bfcf 100644
--- a/Libraries/LibJS/Rust/src/parser/resolve.rs
+++ b/Libraries/LibJS/Rust/src/parser/resolve.rs
@@ -447,9 +447,9 @@ pub(super) struct ResolveOptions {
}
/// What a name means from inside a function that a tree did not hold the code of, by function row and name.
-#[derive(Default)]
+#[derive(Clone)]
pub struct OuterResolutions {
- entries: Vec<(u64, Resolution)>,
+ entries: Arc<[(u64, Resolution)]>,
}
impl OuterResolutions {
@@ -457,12 +457,15 @@ impl OuterResolutions {
u64::from(row) << 32 | u64::from(name.0)
}
- fn new(mut entries: Vec<(u64, Resolution)>) -> Self {
+ /// The resolutions of `entries`, which get sorted, and of which the first for a name is the one that counts.
+ fn new(entries: &mut Vec<(u64, Resolution)>) -> Self {
if !entries.is_sorted_by_key(|&(key, _)| key) {
- entries.sort_unstable_by_key(|&(key, _)| key);
+ entries.sort_by_key(|&(key, _)| key);
}
entries.dedup_by_key(|&mut (key, _)| key);
- Self { entries }
+ Self {
+ entries: Arc::from(entries.as_slice()),
+ }
}
fn get(&self, row: u32, name: Name) -> Option<Resolution> {
@@ -482,7 +485,7 @@ pub struct EnclosingCode {
/// The row of the function, or NONE for a program.
row: u32,
/// What the functions nested in this code see of it, unless it has none.
- resolutions: Option<Arc<OuterResolutions>>,
+ resolutions: Option<OuterResolutions>,
parent: Option<Arc<EnclosingCode>>,
}
@@ -496,12 +499,10 @@ impl EnclosingCode {
}
/// The code of the function at `row`, from what it found out about what is nested in it itself.
- pub(crate) fn of_function(row: u32, mut entries: Vec<(u64, Resolution)>, parent: &Arc<Self>) -> Arc<Self> {
- // NB: The first entry for a name is the one that counts.
- entries.sort_by_key(|&(key, _)| key);
+ pub(crate) fn of_function(row: u32, entries: &mut Vec<(u64, Resolution)>, parent: &Arc<Self>) -> Arc<Self> {
Arc::new(Self {
row,
- resolutions: Some(Arc::new(OuterResolutions::new(entries))),
+ resolutions: Some(OuterResolutions::new(entries)),
parent: Some(parent.clone()),
})
}
@@ -520,7 +521,7 @@ impl EnclosingCode {
while let Some(enclosing) = code {
let resolutions = enclosing
.resolutions
- .as_deref()
+ .as_ref()
.expect("code that a function is nested in has resolutions for it");
if let Some(mut resolution) = resolutions.get(function, name) {
if is_inside_scope_with_eval {
@@ -644,17 +645,17 @@ impl<'t> Resolver<'t> {
}
/// Hands the results over to the tree.
- fn finish(mut self) -> (ResolverResults, Option<Arc<OuterResolutions>>) {
+ fn finish(mut self) -> (ResolverResults, Option<OuterResolutions>) {
self.give_back_memory();
self.function_scope_rows.sort_unstable_by_key(|&(node, _)| node);
// NB: Every function nested in the code leaves a reference behind, so that without any there are none.
let outer_resolutions = (!self.outer_references.is_empty()).then(|| {
- let entries = self
+ let mut entries = self
.outer_references
.iter()
.map(|&(row, name, node)| (OuterResolutions::key(row, name), self.resolutions[node as usize]))
.collect();
- Arc::new(OuterResolutions::new(entries))
+ OuterResolutions::new(&mut entries)
});
(
ResolverResults {