Commit e45dc0fc authored by Matt Mastracci's avatar Matt Mastracci

perf(ecmascript): shrink Call/New args from Vec to Box<[T]>

Swap `Vec<JsValue>` (24 B) for `Box<[JsValue]>` (16 B) on `JsValue::Call`
and `JsValue::New` args. Analysis never pushes to args after construction
— the `Vec`'s capacity field is dead weight. A few consumer sites that
want an owned `Vec` convert via `into_vec()` (O(1), reuses the backing
allocation).

Shrinks `Call`/`New` variant payloads from 40 B to 32 B. `JsValue`'s
overall enum size stays at 40 B (driven by `Unknown`, unchanged).

Measured net perf win of 2–6 % on the `references` criterion bench:
`Vec::capacity` is checked / decremented across the analyzer's hot
paths, and dropping those accesses pays for the O(1) conversion cost.
Snapshots stable. All 351 lib tests pass.
parent 9df34bcf
...@@ -491,7 +491,7 @@ pub fn replace_builtin(value: &mut JsValue) -> bool { ...@@ -491,7 +491,7 @@ pub fn replace_builtin(value: &mut JsValue) -> bool {
*value = JsValue::alternatives( *value = JsValue::alternatives(
take(values) take(values)
.into_iter() .into_iter()
.map(|alt| JsValue::call(Box::new(alt), args.clone())) .map(|alt| JsValue::call(Box::new(alt), args.to_vec()))
.collect(), .collect(),
); );
true true
......
...@@ -182,7 +182,7 @@ where ...@@ -182,7 +182,7 @@ where
for arg in args.iter() { for arg in args.iter() {
total_nodes -= arg.total_nodes(); total_nodes -= arg.total_nodes();
} }
entry.insert(args); entry.insert(args.into_vec());
work_queue_stack.push(Step::LeaveCall(func_ident)); work_queue_stack.push(Step::LeaveCall(func_ident));
work_queue_stack.push(Step::Enter(*return_value)); work_queue_stack.push(Step::Enter(*return_value));
} else { } else {
...@@ -194,7 +194,7 @@ where ...@@ -194,7 +194,7 @@ where
done.push(JsValue::unknown( done.push(JsValue::unknown(
JsValue::call( JsValue::call(
Box::new(JsValue::Function(function_nodes, func_ident, return_value)), Box::new(JsValue::Function(function_nodes, func_ident, return_value)),
args, args.into_vec(),
), ),
true, true,
"recursive function call", "recursive function call",
......
...@@ -460,10 +460,16 @@ pub enum JsValue { ...@@ -460,10 +460,16 @@ pub enum JsValue {
Binary(u32, Box<JsValue>, BinaryOperator, Box<JsValue>), Binary(u32, Box<JsValue>, BinaryOperator, Box<JsValue>),
/// A constructor call. /// A constructor call.
/// `(total_node_count, callee, args)` /// `(total_node_count, callee, args)`
New(u32, Box<JsValue>, Vec<JsValue>), ///
/// `Box<[JsValue]>` saves 8 B vs `Vec<JsValue>` (16 B fat pointer vs 24 B). Combined with
/// the `Call` variant below, this drops the payload from 40 B to 32 B (aligned).
New(u32, Box<JsValue>, Box<[JsValue]>),
/// A function call without a `this` context. /// A function call without a `this` context.
/// `(total_node_count, callee, args)` /// `(total_node_count, callee, args)`
Call(u32, Box<JsValue>, Vec<JsValue>), ///
/// `Box<[JsValue]>` saves 8 B vs `Vec<JsValue>` (16 B fat pointer vs 24 B). Combined with
/// the `New` variant above, this drops the payload from 40 B to 32 B (aligned).
Call(u32, Box<JsValue>, Box<[JsValue]>),
/// A super call to the parent constructor. /// A super call to the parent constructor.
/// `(total_node_count, args)` /// `(total_node_count, args)`
SuperCall(u32, Vec<JsValue>), SuperCall(u32, Vec<JsValue>),
...@@ -1157,11 +1163,19 @@ impl JsValue { ...@@ -1157,11 +1163,19 @@ impl JsValue {
} }
pub fn new(f: Box<JsValue>, args: Vec<JsValue>) -> Self { pub fn new(f: Box<JsValue>, args: Vec<JsValue>) -> Self {
Self::New(1 + f.total_nodes() + total_nodes(&args), f, args) Self::New(
1 + f.total_nodes() + total_nodes(&args),
f,
args.into_boxed_slice(),
)
} }
pub fn call(f: Box<JsValue>, args: Vec<JsValue>) -> Self { pub fn call(f: Box<JsValue>, args: Vec<JsValue>) -> Self {
Self::Call(1 + f.total_nodes() + total_nodes(&args), f, args) Self::Call(
1 + f.total_nodes() + total_nodes(&args),
f,
args.into_boxed_slice(),
)
} }
pub fn super_call(args: Vec<JsValue>) -> Self { pub fn super_call(args: Vec<JsValue>) -> Self {
......
...@@ -21,7 +21,7 @@ pub async fn replace_well_known( ...@@ -21,7 +21,7 @@ pub async fn replace_well_known(
well_known_function_call( well_known_function_call(
kind, kind,
JsValue::unknown_empty(false, "this is not analyzed yet"), JsValue::unknown_empty(false, "this is not analyzed yet"),
args, args.into_vec(),
compile_time_info, compile_time_info,
allow_project_root_tracing, allow_project_root_tracing,
) )
......
...@@ -3567,7 +3567,7 @@ async fn value_visitor_inner( ...@@ -3567,7 +3567,7 @@ async fn value_visitor_inner(
_, _,
box JsValue::WellKnownFunction(WellKnownFunctionKind::RequireResolve), box JsValue::WellKnownFunction(WellKnownFunctionKind::RequireResolve),
args, args,
) => require_resolve_visitor(origin, args).await?, ) => require_resolve_visitor(origin, args.into_vec()).await?,
JsValue::Call( JsValue::Call(
_, _,
box JsValue::WellKnownFunction(WellKnownFunctionKind::ImportMetaGlob), box JsValue::WellKnownFunction(WellKnownFunctionKind::ImportMetaGlob),
...@@ -3581,7 +3581,7 @@ async fn value_visitor_inner( ...@@ -3581,7 +3581,7 @@ async fn value_visitor_inner(
_, _,
box JsValue::WellKnownFunction(WellKnownFunctionKind::RequireContext), box JsValue::WellKnownFunction(WellKnownFunctionKind::RequireContext),
args, args,
) => require_context_visitor(origin, args).await?, ) => require_context_visitor(origin, args.into_vec()).await?,
JsValue::Call( JsValue::Call(
_, _,
box JsValue::WellKnownFunction( box JsValue::WellKnownFunction(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment