Unverified Commit 409fa64d authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Use an arena for `JSValue`s (#94297)

This PR takes the arena introduced in
https://github.com/vercel/next.js/pull/94296 and uses it! It's large but
shouldn't change functionality beyond the use of the arena. Some syntax
related to pattern-matching had to change because `bumpalo`'s `Box`
doesn't support the same pattern matching that the standard library's
`Box` does but the code should be equivalent.

`bumpalo`'s `Bump` type implements `Send` but not `Sync`, to allow us to
pass a `Bump` into async-methods we wrap it in `ThreadLocal` (see
https://docs.rs/thread_local/latest/thread_local/). `ThreadLocal`
implements `Sync` by creating a new bumpalo allocator for each thread.
We've tried to minimise the usage of `ThreadLocal` to just where it was
needed. The downside of `ThreadLocal` is that we may find ourselves
allocating memory for multiple arenas instead of just one. This is more
time allocating if there are multiple threads compared to one shared
allocation, however, it still appears to be a performance improvement
over individually allocating memory for each `JSValue`

---------
Co-authored-by: 's avatarBenjamin Woodruff <benjamin.woodruff@vercel.com>
parent 7dfd4648
...@@ -10398,6 +10398,7 @@ dependencies = [ ...@@ -10398,6 +10398,7 @@ dependencies = [
"strsim 0.11.1", "strsim 0.11.1",
"swc_core", "swc_core",
"swc_sourcemap", "swc_sourcemap",
"thread_local",
"tokio", "tokio",
"tracing", "tracing",
"turbo-bincode", "turbo-bincode",
......
...@@ -30,7 +30,9 @@ use turbopack_core::{ ...@@ -30,7 +30,9 @@ use turbopack_core::{
}; };
use turbopack_ecmascript::{ use turbopack_ecmascript::{
EcmascriptInputTransforms, EcmascriptModuleAssetType, EcmascriptInputTransforms, EcmascriptModuleAssetType,
analyzer::{ConstantNumber, ConstantValue, JsValue, ObjectPart, graph::EvalContext}, analyzer::{
Bump, ConstantNumber, ConstantValue, JsValue, ObjectPart, ThreadLocal, graph::EvalContext,
},
parse::{ParseResult, parse}, parse::{ParseResult, parse},
}; };
...@@ -413,12 +415,25 @@ pub async fn parse_segment_config_from_source( ...@@ -413,12 +415,25 @@ pub async fn parse_segment_config_from_source(
return Ok(Default::default()); return Ok(Default::default());
}; };
// Arena for the `JsValue`s produced while evaluating config expressions;
// freed when this function returns.
let arena = ThreadLocal::new();
let config = WrapFuture::new( let config = WrapFuture::new(
async { async {
let mut config = NextSegmentConfig::default(); let mut config = NextSegmentConfig::default();
let mut parse = async |ident, init, span| { let mut parse = async |ident, init, span| {
parse_config_value(source, mode, &mut config, eval_context, ident, init, span).await parse_config_value(
source,
mode,
&mut config,
eval_context,
&arena,
ident,
init,
span,
)
.await
}; };
for item in &module_ast.body { for item in &module_ast.body {
...@@ -575,7 +590,7 @@ async fn invalid_config( ...@@ -575,7 +590,7 @@ async fn invalid_config(
key: &str, key: &str,
span: Span, span: Span,
error: RcStr, error: RcStr,
value: Option<&JsValue>, value: Option<&JsValue<'_>>,
severity: IssueSeverity, severity: IssueSeverity,
) -> Result<()> { ) -> Result<()> {
let detail = if let Some(value) = value { let detail = if let Some(value) = value {
...@@ -604,6 +619,7 @@ async fn parse_config_value( ...@@ -604,6 +619,7 @@ async fn parse_config_value(
mode: ParseSegmentMode, mode: ParseSegmentMode,
config: &mut NextSegmentConfig, config: &mut NextSegmentConfig,
eval_context: &EvalContext, eval_context: &EvalContext,
arena: &ThreadLocal<Bump>,
key: Cow<'_, str>, key: Cow<'_, str>,
init: Option<Cow<'_, Expr>>, init: Option<Cow<'_, Expr>>,
span: Span, span: Span,
...@@ -619,7 +635,8 @@ async fn parse_config_value( ...@@ -619,7 +635,8 @@ async fn parse_config_value(
| Some(Expr::TsSatisfies(TsSatisfiesExpr { expr, .. })) => Some(&**expr), | Some(Expr::TsSatisfies(TsSatisfiesExpr { expr, .. })) => Some(&**expr),
_ => init, _ => init,
}; };
init.map(|init| eval_context.eval(init)).map(|v| { init.map(|init| eval_context.eval(arena.get_or_default(), init))
.map(|v| {
// Special case, as we don't call `link` here: assume that `undefined` is a free // Special case, as we don't call `link` here: assume that `undefined` is a free
// variable. // variable.
if let JsValue::FreeVar(name) = &v if let JsValue::FreeVar(name) = &v
...@@ -1001,7 +1018,7 @@ async fn parse_static_string_or_array_from_js_value( ...@@ -1001,7 +1018,7 @@ async fn parse_static_string_or_array_from_js_value(
span: Span, span: Span,
key: &str, key: &str,
sub_key: &str, sub_key: &str,
value: &JsValue, value: &JsValue<'_>,
) -> Result<Option<Vec<RcStr>>> { ) -> Result<Option<Vec<RcStr>>> {
Ok(match value { Ok(match value {
// Single value is turned into a single-element Vec. // Single value is turned into a single-element Vec.
...@@ -1054,9 +1071,10 @@ async fn parse_static_string_or_array_from_js_value( ...@@ -1054,9 +1071,10 @@ async fn parse_static_string_or_array_from_js_value(
async fn parse_route_matcher_from_js_value( async fn parse_route_matcher_from_js_value(
source: ResolvedVc<Box<dyn Source>>, source: ResolvedVc<Box<dyn Source>>,
span: Span, span: Span,
value: &JsValue, value: &JsValue<'_>,
) -> Result<Option<Vec<MiddlewareMatcherKind>>> { ) -> Result<Option<Vec<MiddlewareMatcherKind>>> {
let parse_matcher_kind_matcher = async |value: &JsValue, sub_key: &str, matcher_idx: usize| { let parse_matcher_kind_matcher =
async |value: &JsValue<'_>, sub_key: &str, matcher_idx: usize| {
let mut route_has = vec![]; let mut route_has = vec![];
if let JsValue::Array { items, .. } = value { if let JsValue::Array { items, .. } = value {
for (i, item) in items.iter().enumerate() { for (i, item) in items.iter().enumerate() {
...@@ -1083,8 +1101,8 @@ async fn parse_route_matcher_from_js_value( ...@@ -1083,8 +1101,8 @@ async fn parse_route_matcher_from_js_value(
span, span,
format!( format!(
"`matcher[{matcher_idx}].{sub_key}[{i}].type` \ "`matcher[{matcher_idx}].{sub_key}[{i}].type` \
must be one of the strings: 'header', 'cookie', \ must be one of the strings: 'header', \
'query', 'host'" 'cookie', 'query', 'host'"
) )
.into(), .into(),
Some(part_value), Some(part_value),
...@@ -1102,8 +1120,8 @@ async fn parse_route_matcher_from_js_value( ...@@ -1102,8 +1120,8 @@ async fn parse_route_matcher_from_js_value(
"config", "config",
span, span,
format!( format!(
"`matcher[{matcher_idx}].{sub_key}[{i}].key` must \ "`matcher[{matcher_idx}].{sub_key}[{i}].key` \
be a string" must be a string"
) )
.into(), .into(),
Some(part_value), Some(part_value),
...@@ -1121,8 +1139,8 @@ async fn parse_route_matcher_from_js_value( ...@@ -1121,8 +1139,8 @@ async fn parse_route_matcher_from_js_value(
"config", "config",
span, span,
format!( format!(
"`matcher[{matcher_idx}].{sub_key}[{i}].value` \ "`matcher[{matcher_idx}].{sub_key}[{i}].\
must be a string" value` must be a string"
) )
.into(), .into(),
Some(part_value), Some(part_value),
......
...@@ -46,6 +46,7 @@ serde_json = { workspace = true, features = ["raw_value"] } ...@@ -46,6 +46,7 @@ serde_json = { workspace = true, features = ["raw_value"] }
swc_sourcemap = { workspace = true } swc_sourcemap = { workspace = true }
smallvec = { workspace = true } smallvec = { workspace = true }
strsim = { workspace = true } strsim = { workspace = true }
thread_local = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
turbo-bincode = { workspace = true } turbo-bincode = { workspace = true }
......
...@@ -25,6 +25,7 @@ use turbopack_core::{ ...@@ -25,6 +25,7 @@ use turbopack_core::{
use turbopack_ecmascript::{ use turbopack_ecmascript::{
AnalyzeMode, AnalyzeMode,
analyzer::{ analyzer::{
Bump, ThreadLocal,
graph::{EvalContext, VarGraph, create_graph}, graph::{EvalContext, VarGraph, create_graph},
imports::ImportAttributes, imports::ImportAttributes,
linker::link, linker::link,
...@@ -72,7 +73,11 @@ pub fn benchmark(c: &mut Criterion) { ...@@ -72,7 +73,11 @@ pub fn benchmark(c: &mut Criterion) {
Default::default(), Default::default(),
None, None,
); );
// Leak a per-benchmark arena so the stored `VarGraph` can be `'static` (benches are
// short-lived processes, so the leak is inconsequential).
let arena: &'static ThreadLocal<Bump> = Box::leak(Box::new(ThreadLocal::new()));
let var_graph = Arc::new(create_graph( let var_graph = Arc::new(create_graph(
arena.get_or_default(),
&program, &program,
&eval_context, &eval_context,
AnalyzeMode::CodeGenerationAndTracing, AnalyzeMode::CodeGenerationAndTracing,
...@@ -83,6 +88,7 @@ pub fn benchmark(c: &mut Criterion) { ...@@ -83,6 +88,7 @@ pub fn benchmark(c: &mut Criterion) {
program, program,
eval_context, eval_context,
var_graph, var_graph,
arena,
}; };
group.bench_with_input( group.bench_with_input(
...@@ -99,17 +105,20 @@ pub fn benchmark(c: &mut Criterion) { ...@@ -99,17 +105,20 @@ pub fn benchmark(c: &mut Criterion) {
struct BenchInput { struct BenchInput {
program: Program, program: Program,
eval_context: EvalContext, eval_context: EvalContext,
var_graph: Arc<VarGraph>, var_graph: Arc<VarGraph<'static>>,
arena: &'static ThreadLocal<Bump>,
} }
fn bench_create_graph(b: &mut Bencher, input: &BenchInput) { fn bench_create_graph(b: &mut Bencher, input: &BenchInput) {
b.iter(|| { b.iter(|| {
create_graph( let arena = ThreadLocal::new();
criterion::black_box(create_graph(
arena.get_or_default(),
&input.program, &input.program,
&input.eval_context, &input.eval_context,
AnalyzeMode::CodeGenerationAndTracing, AnalyzeMode::CodeGenerationAndTracing,
true, true,
) ));
}); });
} }
...@@ -117,6 +126,8 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) { ...@@ -117,6 +126,8 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
let rt = tokio::runtime::Builder::new_current_thread() let rt = tokio::runtime::Builder::new_current_thread()
.build() .build()
.unwrap(); .unwrap();
let arena = input.arena;
let var_graph = input.var_graph.clone(); let var_graph = input.var_graph.clone();
b.to_async(rt).iter_custom(move |iters| { b.to_async(rt).iter_custom(move |iters| {
...@@ -150,10 +161,18 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) { ...@@ -150,10 +161,18 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
let var_cache = Default::default(); let var_cache = Default::default();
for value in var_graph.values.values() { for value in var_graph.values.values() {
link( link(
arena,
&var_graph, &var_graph,
value.clone(), value.clone_in(arena.get_or_default()),
&early_visitor, &(|val| early_visitor(arena, val)),
&(|val| visitor(val, compile_time_info, ImportAttributes::empty_ref())), &(|val| {
visitor(
arena,
val,
compile_time_info,
ImportAttributes::empty_ref(),
)
}),
&Default::default(), &Default::default(),
&var_cache, &var_cache,
) )
......
...@@ -168,6 +168,19 @@ impl<'a, T> BumpVec<'a, T> { ...@@ -168,6 +168,19 @@ impl<'a, T> BumpVec<'a, T> {
Some(unsafe { self.ptr.as_ptr().add(self.len).read() }) Some(unsafe { self.ptr.as_ptr().add(self.len).read() })
} }
/// Remove the element at `index` and return it, moving the last element into its slot. This
/// does not preserve element order but is O(1). Panics if `index` is out of bounds.
pub fn swap_remove(&mut self, index: usize) -> T {
assert!(index < self.len, "swap_remove index out of bounds");
// Move the last item into the slot and return the displaced one.
let last = self.pop().unwrap();
if index < self.len {
std::mem::replace(&mut self[index], last)
} else {
last
}
}
/// Split the vec in two at `at`: `self` retains the prefix `[0, at)` and the returned vec owns /// Split the vec in two at `at`: `self` retains the prefix `[0, at)` and the returned vec owns
/// the suffix `[at, len)`, moved into a fresh arena allocation. /// the suffix `[at, len)`, moved into a fresh arena allocation.
pub fn split_off(&mut self, bump: &'a Bump, at: usize) -> Self { pub fn split_off(&mut self, bump: &'a Bump, at: usize) -> Self {
...@@ -280,8 +293,15 @@ impl<T> Iterator for IntoIter<'_, T> { ...@@ -280,8 +293,15 @@ impl<T> Iterator for IntoIter<'_, T> {
self.idx += 1; self.idx += 1;
Some(value) Some(value)
} }
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len - self.idx;
(remaining, Some(remaining))
}
} }
impl<T> ExactSizeIterator for IntoIter<'_, T> {}
impl<T> Drop for IntoIter<'_, T> { impl<T> Drop for IntoIter<'_, T> {
fn drop(&mut self) { fn drop(&mut self) {
// SAFETY: indices `[idx, len)` are still initialized; drop them in place exactly once. // SAFETY: indices `[idx, len)` are still initialized; drop them in place exactly once.
...@@ -405,6 +425,45 @@ mod tests { ...@@ -405,6 +425,45 @@ mod tests {
assert!(v.is_empty()); assert!(v.is_empty());
} }
#[test]
fn swap_remove() {
let bump = Bump::new();
let mut v = BumpVec::from_iter_in(&bump, [1, 2, 3, 4, 5]);
// Removing a middle element moves the last element into its slot.
assert_eq!(v.swap_remove(1), 2);
assert_eq!(&*v, &[1, 5, 3, 4][..]);
// Removing the last element is just a pop.
assert_eq!(v.swap_remove(3), 4);
assert_eq!(&*v, &[1, 5, 3][..]);
// Removing the first element.
assert_eq!(v.swap_remove(0), 1);
assert_eq!(&*v, &[3, 5][..]);
}
#[test]
#[should_panic(expected = "swap_remove index out of bounds")]
fn swap_remove_out_of_bounds() {
let bump = Bump::new();
let mut v = BumpVec::from_iter_in(&bump, [1, 2, 3]);
v.swap_remove(3);
}
#[test]
fn swap_remove_does_not_double_free() {
let bump = Bump::new();
let counter = Rc::new(Cell::new(0));
let mut v = BumpVec::new();
for _ in 0..3 {
v.push(&bump, DropCounter(counter.clone()));
}
let removed = v.swap_remove(0);
drop(removed);
assert_eq!(counter.get(), 1);
// The two remaining drop exactly once; the removed slot must not be dropped again.
drop(v);
assert_eq!(counter.get(), 3);
}
#[test] #[test]
fn split_off_prefix_and_suffix() { fn split_off_prefix_and_suffix() {
let bump = Bump::new(); let bump = Bump::new();
......
use bumpalo::boxed::Box as BumpBox;
use swc_core::{atoms::Atom, common::Span, ecma::visit::fields::*}; use swc_core::{atoms::Atom, common::Span, ecma::visit::fields::*};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbopack_core::resolve::ExportUsage; use turbopack_core::resolve::ExportUsage;
use crate::{analyzer::JsValue, utils::AstPathRange}; use crate::{
analyzer::{Bump, JsValue},
utils::AstPathRange,
};
#[derive(Debug)] #[derive(Debug)]
pub struct EffectsBlock { pub struct EffectsBlock<'a> {
pub effects: Vec<Effect>, pub effects: Vec<Effect<'a>>,
pub range: AstPathRange, pub range: AstPathRange,
} }
impl EffectsBlock { impl EffectsBlock<'_> {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.effects.is_empty() self.effects.is_empty()
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ConditionalKind { pub enum ConditionalKind<'a> {
/// The blocks of an `if` statement without an `else` block. /// The blocks of an `if` statement without an `else` block.
If { then: Box<EffectsBlock> }, If { then: Box<EffectsBlock<'a>> },
/// The blocks of an `if ... else` or `if { ... return ... } ...` statement. /// The blocks of an `if ... else` or `if { ... return ... } ...` statement.
IfElse { IfElse {
then: Box<EffectsBlock>, then: Box<EffectsBlock<'a>>,
r#else: Box<EffectsBlock>, r#else: Box<EffectsBlock<'a>>,
}, },
/// The blocks of an `if ... else` statement. /// The blocks of an `if ... else` statement.
Else { r#else: Box<EffectsBlock> }, Else { r#else: Box<EffectsBlock<'a>> },
/// The blocks of an `if { ... return ... } else { ... } ...` or `if { ... } /// The blocks of an `if { ... return ... } else { ... } ...` or `if { ... }
/// else { ... return ... } ...` statement. /// else { ... return ... } ...` statement.
IfElseMultiple { IfElseMultiple {
then: Vec<Box<EffectsBlock>>, then: Vec<Box<EffectsBlock<'a>>>,
r#else: Vec<Box<EffectsBlock>>, r#else: Vec<Box<EffectsBlock<'a>>>,
}, },
/// The expressions on the right side of the `?:` operator. /// The expressions on the right side of the `?:` operator.
Ternary { Ternary {
then: Box<EffectsBlock>, then: Box<EffectsBlock<'a>>,
r#else: Box<EffectsBlock>, r#else: Box<EffectsBlock<'a>>,
}, },
/// The expression on the right side of the `&&` operator. /// The expression on the right side of the `&&` operator.
And { expr: Box<EffectsBlock> }, And { expr: Box<EffectsBlock<'a>> },
/// The expression on the right side of the `||` operator. /// The expression on the right side of the `||` operator.
Or { expr: Box<EffectsBlock> }, Or { expr: Box<EffectsBlock<'a>> },
/// The expression on the right side of the `??` operator. /// The expression on the right side of the `??` operator.
NullishCoalescing { expr: Box<EffectsBlock> }, NullishCoalescing { expr: Box<EffectsBlock<'a>> },
/// The expression on the right side of a labeled statement. /// The expression on the right side of a labeled statement.
Labeled { body: Box<EffectsBlock> }, Labeled { body: Box<EffectsBlock<'a>> },
} }
impl ConditionalKind { impl<'a> ConditionalKind<'a> {
/// Normalizes all contained values. /// Normalizes all contained values.
pub fn normalize(&mut self) { pub fn normalize(&mut self, arena: &'a Bump) {
match self { match self {
ConditionalKind::If { then: block } ConditionalKind::If { then: block }
| ConditionalKind::Else { r#else: block } | ConditionalKind::Else { r#else: block }
...@@ -58,28 +62,28 @@ impl ConditionalKind { ...@@ -58,28 +62,28 @@ impl ConditionalKind {
| ConditionalKind::Or { expr: block, .. } | ConditionalKind::Or { expr: block, .. }
| ConditionalKind::NullishCoalescing { expr: block, .. } => { | ConditionalKind::NullishCoalescing { expr: block, .. } => {
for effect in &mut block.effects { for effect in &mut block.effects {
effect.normalize(); effect.normalize(arena);
} }
} }
ConditionalKind::IfElse { then, r#else, .. } ConditionalKind::IfElse { then, r#else, .. }
| ConditionalKind::Ternary { then, r#else, .. } => { | ConditionalKind::Ternary { then, r#else, .. } => {
for effect in &mut then.effects { for effect in &mut then.effects {
effect.normalize(); effect.normalize(arena);
} }
for effect in &mut r#else.effects { for effect in &mut r#else.effects {
effect.normalize(); effect.normalize(arena);
} }
} }
ConditionalKind::IfElseMultiple { then, r#else, .. } => { ConditionalKind::IfElseMultiple { then, r#else, .. } => {
for block in then.iter_mut().chain(r#else.iter_mut()) { for block in then.iter_mut().chain(r#else.iter_mut()) {
for effect in &mut block.effects { for effect in &mut block.effects {
effect.normalize(); effect.normalize(arena);
} }
} }
} }
ConditionalKind::Labeled { body } => { ConditionalKind::Labeled { body } => {
for effect in &mut body.effects { for effect in &mut body.effects {
effect.normalize(); effect.normalize(arena);
} }
} }
} }
...@@ -87,21 +91,21 @@ impl ConditionalKind { ...@@ -87,21 +91,21 @@ impl ConditionalKind {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum EffectArg { pub enum EffectArg<'a> {
Value(JsValue), Value(JsValue<'a>),
Closure(JsValue, Box<EffectsBlock>), Closure(JsValue<'a>, Box<EffectsBlock<'a>>),
Spread, Spread,
} }
impl EffectArg { impl<'a> EffectArg<'a> {
/// Normalizes all contained values. /// Normalizes all contained values.
pub fn normalize(&mut self) { pub fn normalize(&mut self, arena: &'a Bump) {
match self { match self {
EffectArg::Value(value) => value.normalize(), EffectArg::Value(value) => value.normalize(arena),
EffectArg::Closure(value, effects) => { EffectArg::Closure(value, effects) => {
value.normalize(); value.normalize(arena);
for effect in &mut effects.effects { for effect in &mut effects.effects {
effect.normalize(); effect.normalize(arena);
} }
} }
EffectArg::Spread => {} EffectArg::Spread => {}
...@@ -110,21 +114,21 @@ impl EffectArg { ...@@ -110,21 +114,21 @@ impl EffectArg {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Effect { pub enum Effect<'a> {
/// Some condition which affects which effects might be executed. If the /// Some condition which affects which effects might be executed. If the
/// condition evaluates to some compile-time constant, we can use that /// condition evaluates to some compile-time constant, we can use that
/// to determine which effects are executed and remove the others. /// to determine which effects are executed and remove the others.
Conditional { Conditional {
condition: Box<JsValue>, condition: BumpBox<'a, JsValue<'a>>,
kind: Box<ConditionalKind>, kind: Box<ConditionalKind<'a>>,
/// The ast path to the condition. /// The ast path to the condition.
ast_path: Vec<AstParentKind>, ast_path: Vec<AstParentKind>,
span: Span, span: Span,
}, },
/// A function call or a new call of a function. /// A function call or a new call of a function.
Call { Call {
func: Box<JsValue>, func: BumpBox<'a, JsValue<'a>>,
args: Vec<EffectArg>, args: Vec<EffectArg<'a>>,
ast_path: Vec<AstParentKind>, ast_path: Vec<AstParentKind>,
span: Span, span: Span,
in_try: bool, in_try: bool,
...@@ -132,9 +136,9 @@ pub enum Effect { ...@@ -132,9 +136,9 @@ pub enum Effect {
}, },
/// A function call or a new call of a property of an object. /// A function call or a new call of a property of an object.
MemberCall { MemberCall {
obj: Box<JsValue>, obj: BumpBox<'a, JsValue<'a>>,
prop: Box<JsValue>, prop: BumpBox<'a, JsValue<'a>>,
args: Vec<EffectArg>, args: Vec<EffectArg<'a>>,
ast_path: Vec<AstParentKind>, ast_path: Vec<AstParentKind>,
span: Span, span: Span,
in_try: bool, in_try: bool,
...@@ -142,8 +146,8 @@ pub enum Effect { ...@@ -142,8 +146,8 @@ pub enum Effect {
}, },
/// A property access. /// A property access.
Member { Member {
obj: Box<JsValue>, obj: BumpBox<'a, JsValue<'a>>,
prop: Box<JsValue>, prop: BumpBox<'a, JsValue<'a>>,
ast_path: Vec<AstParentKind>, ast_path: Vec<AstParentKind>,
span: Span, span: Span,
}, },
...@@ -162,7 +166,7 @@ pub enum Effect { ...@@ -162,7 +166,7 @@ pub enum Effect {
}, },
/// A typeof expression /// A typeof expression
TypeOf { TypeOf {
arg: Box<JsValue>, arg: BumpBox<'a, JsValue<'a>>,
ast_path: Vec<AstParentKind>, ast_path: Vec<AstParentKind>,
span: Span, span: Span,
}, },
...@@ -182,7 +186,7 @@ pub enum Effect { ...@@ -182,7 +186,7 @@ pub enum Effect {
/// - `import(/* webpackExports: ["a"] */ './lib')` (magic comment) /// - `import(/* webpackExports: ["a"] */ './lib')` (magic comment)
/// - `import(/* turbopackExports: ["a"] */ './lib')` (magic comment) /// - `import(/* turbopackExports: ["a"] */ './lib')` (magic comment)
DynamicImport { DynamicImport {
args: Vec<EffectArg>, args: Vec<EffectArg<'a>>,
ast_path: Vec<AstParentKind>, ast_path: Vec<AstParentKind>,
span: Span, span: Span,
in_try: bool, in_try: bool,
...@@ -193,43 +197,43 @@ pub enum Effect { ...@@ -193,43 +197,43 @@ pub enum Effect {
Unreachable { start_ast_path: Vec<AstParentKind> }, Unreachable { start_ast_path: Vec<AstParentKind> },
} }
impl Effect { impl<'a> Effect<'a> {
/// Normalizes all contained values. /// Normalizes all contained values.
pub fn normalize(&mut self) { pub fn normalize(&mut self, arena: &'a Bump) {
match self { match self {
Effect::Conditional { Effect::Conditional {
condition, kind, .. condition, kind, ..
} => { } => {
condition.normalize(); condition.normalize(arena);
kind.normalize(); kind.normalize(arena);
} }
Effect::Call { func, args, .. } => { Effect::Call { func, args, .. } => {
func.normalize(); func.normalize(arena);
for arg in args.iter_mut() { for arg in args.iter_mut() {
arg.normalize(); arg.normalize(arena);
} }
} }
Effect::MemberCall { Effect::MemberCall {
obj, prop, args, .. obj, prop, args, ..
} => { } => {
obj.normalize(); obj.normalize(arena);
prop.normalize(); prop.normalize(arena);
for arg in args.iter_mut() { for arg in args.iter_mut() {
arg.normalize(); arg.normalize(arena);
} }
} }
Effect::Member { obj, prop, .. } => { Effect::Member { obj, prop, .. } => {
obj.normalize(); obj.normalize(arena);
prop.normalize(); prop.normalize(arena);
} }
Effect::DynamicImport { args, .. } => { Effect::DynamicImport { args, .. } => {
for arg in args.iter_mut() { for arg in args.iter_mut() {
arg.normalize(); arg.normalize(arena);
} }
} }
Effect::ImportedBinding { .. } => {} Effect::ImportedBinding { .. } => {}
Effect::TypeOf { arg, .. } => { Effect::TypeOf { arg, .. } => {
arg.normalize(); arg.normalize(arena);
} }
Effect::FreeVar { .. } => {} Effect::FreeVar { .. } => {}
Effect::ImportMeta { .. } => {} Effect::ImportMeta { .. } => {}
......
...@@ -12,7 +12,7 @@ pub use crate::analyzer::graph::{ ...@@ -12,7 +12,7 @@ pub use crate::analyzer::graph::{
}; };
use crate::{ use crate::{
AnalyzeMode, AnalyzeMode,
analyzer::{JsValue, graph::visitor::Analyzer}, analyzer::{Bump, JsValue, graph::visitor::Analyzer},
code_gen::CodeGen, code_gen::CodeGen,
}; };
...@@ -21,8 +21,8 @@ mod eval_context; ...@@ -21,8 +21,8 @@ mod eval_context;
mod visitor; mod visitor;
#[derive(Debug)] #[derive(Debug)]
pub struct VarGraph { pub struct VarGraph<'a> {
pub values: FxHashMap<Id, JsValue>, pub values: FxHashMap<Id, JsValue<'a>>,
/// Map [`JsValue::FreeVar`] names to their [`Id`] to facilitate lookups into [`Self::values`]. /// Map [`JsValue::FreeVar`] names to their [`Id`] to facilitate lookups into [`Self::values`].
/// ///
...@@ -30,50 +30,50 @@ pub struct VarGraph { ...@@ -30,50 +30,50 @@ pub struct VarGraph {
/// non-trivial values. /// non-trivial values.
pub free_var_ids: FxHashMap<Atom, Id>, pub free_var_ids: FxHashMap<Atom, Id>,
pub effects: Vec<Effect>, pub effects: Vec<Effect<'a>>,
// Some unconditional codegens, usually for ESM items. // Some unconditional codegens, usually for ESM items.
pub code_gens: Vec<CodeGen>, pub code_gens: Vec<CodeGen>,
} }
impl VarGraph { impl<'a> VarGraph<'a> {
pub fn normalize(&mut self) { pub fn normalize(&mut self, arena: &'a Bump) {
for value in self.values.values_mut() { for value in self.values.values_mut() {
value.normalize(); value.normalize(arena);
} }
for effect in self.effects.iter_mut() { for effect in self.effects.iter_mut() {
effect.normalize(); effect.normalize(arena);
} }
} }
} }
pub fn create_graph( pub fn create_graph<'a>(
arena: &'a Bump,
m: &Program, m: &Program,
eval_context: &EvalContext, eval_context: &EvalContext,
analyze_mode: AnalyzeMode, analyze_mode: AnalyzeMode,
supports_block_scoping: bool, supports_block_scoping: bool,
) -> VarGraph { ) -> VarGraph<'a> {
let mut graph = VarGraph { let mut analyzer = Analyzer {
arena,
analyze_mode,
data: VarGraph {
values: Default::default(), values: Default::default(),
free_var_ids: Default::default(), free_var_ids: Default::default(),
effects: Default::default(), effects: Default::default(),
code_gens: Default::default(), code_gens: Default::default(),
}; },
m.visit_with_ast_path(
&mut Analyzer {
analyze_mode,
data: &mut graph,
eval_context, eval_context,
state: Default::default(), state: Default::default(),
effects: Default::default(), effects: Default::default(),
hoisted_effects: Default::default(), hoisted_effects: Default::default(),
code_gens: Default::default(), code_gens: Default::default(),
supports_block_scoping, supports_block_scoping,
}, };
&mut Default::default(),
); m.visit_with_ast_path(&mut analyzer, &mut Default::default());
graph.normalize(); let mut graph = analyzer.data;
graph.normalize(arena);
graph graph
} }
...@@ -27,7 +27,7 @@ use super::{JsValue, ModuleValue, top_level_await::has_top_level_await}; ...@@ -27,7 +27,7 @@ use super::{JsValue, ModuleValue, top_level_await::has_top_level_await};
use crate::{ use crate::{
SpecifiedModuleType, SpecifiedModuleType,
analyzer::{ analyzer::{
ConstantValue, ObjectPart, Bump, ConstantValue, ObjectPart,
graph::{AssignmentScope, AssignmentScopes, EvalContext}, graph::{AssignmentScope, AssignmentScopes, EvalContext},
is_unresolved, is_unresolved,
}, },
...@@ -160,7 +160,7 @@ impl ImportAnnotations { ...@@ -160,7 +160,7 @@ impl ImportAnnotations {
} }
} }
pub fn parse_dynamic(with: &JsValue) -> Option<ImportAnnotations> { pub fn parse_dynamic(with: &JsValue<'_>) -> Option<ImportAnnotations> {
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
let JsValue::Object { parts, .. } = with else { let JsValue::Object { parts, .. } = with else {
...@@ -524,15 +524,16 @@ impl ImportMap { ...@@ -524,15 +524,16 @@ impl ImportMap {
} }
} }
pub fn get_import(&self, id: &Id) -> Option<JsValue> { pub fn get_import<'a>(&self, arena: &'a Bump, id: &Id) -> Option<JsValue<'a>> {
if let Some((i, i_sym)) = self.imports.get(id) { if let Some((i, i_sym)) = self.imports.get(id) {
let r = &self.references[*i]; let r = &self.references[*i];
return Some(JsValue::member( return Some(JsValue::member(
Box::new(JsValue::Module(ModuleValue { arena,
JsValue::Module(ModuleValue {
module: r.module_path.clone(), module: r.module_path.clone(),
annotations: r.annotations.clone(), annotations: r.annotations.clone(),
})), }),
Box::new(i_sym.clone().into()), i_sym.clone().into(),
)); ));
} }
if let Some(i) = self.namespace_imports.get(id) { if let Some(i) = self.namespace_imports.get(id) {
......
...@@ -14,22 +14,34 @@ use swc_core::{ ...@@ -14,22 +14,34 @@ use swc_core::{
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use crate::{ use crate::{
analyzer::{JsValue, imports::ImportAnnotations}, analyzer::{Bump, JsValue, imports::ImportAnnotations},
utils::StringifyJs, utils::StringifyJs,
}; };
#[derive(Debug, Clone, Hash, PartialEq)] #[derive(Debug, Hash, PartialEq)]
pub enum ObjectPart { pub enum ObjectPart<'a> {
KeyValue(JsValue, JsValue), KeyValue(JsValue<'a>, JsValue<'a>),
Spread(JsValue), Spread(JsValue<'a>),
} }
impl Default for ObjectPart { impl Default for ObjectPart<'_> {
fn default() -> Self { fn default() -> Self {
ObjectPart::Spread(Default::default()) ObjectPart::Spread(Default::default())
} }
} }
impl<'a> ObjectPart<'a> {
/// Deep-clone this object part into `arena`. See [`JsValue::clone_in`].
pub(crate) fn clone_in(&self, arena: &'a Bump) -> Self {
match self {
ObjectPart::KeyValue(k, v) => {
ObjectPart::KeyValue(k.clone_in(arena), v.clone_in(arena))
}
ObjectPart::Spread(s) => ObjectPart::Spread(s.clone_in(arena)),
}
}
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct ConstantNumber(pub f64); pub struct ConstantNumber(pub f64);
......
...@@ -4,7 +4,7 @@ use either::Either; ...@@ -4,7 +4,7 @@ use either::Either;
use crate::analyzer::{JsValue, ModuleValue, ObjectPart}; use crate::analyzer::{JsValue, ModuleValue, ObjectPart};
impl Display for ObjectPart { impl Display for ObjectPart<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
ObjectPart::KeyValue(key, value) => write!(f, "{key}: {value}"), ObjectPart::KeyValue(key, value) => write!(f, "{key}: {value}"),
...@@ -13,7 +13,7 @@ impl Display for ObjectPart { ...@@ -13,7 +13,7 @@ impl Display for ObjectPart {
} }
} }
impl Display for JsValue { impl Display for JsValue<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
JsValue::Constant(v) => write!(f, "{v}"), JsValue::Constant(v) => write!(f, "{v}"),
......
...@@ -5,8 +5,12 @@ use either::Either; ...@@ -5,8 +5,12 @@ use either::Either;
use crate::analyzer::{JsValue, ModuleValue, ObjectPart, jsvalue::pretty_join}; use crate::analyzer::{JsValue, ModuleValue, ObjectPart, jsvalue::pretty_join};
// Methods for explaining a value // Methods for explaining a value
impl JsValue { impl JsValue<'_> {
pub fn explain_args(args: &[JsValue], depth: usize, unknown_depth: usize) -> (String, String) { pub fn explain_args(
args: &[JsValue<'_>],
depth: usize,
unknown_depth: usize,
) -> (String, String) {
let mut hints = Vec::new(); let mut hints = Vec::new();
let args = args let args = args
.iter() .iter()
......
...@@ -3,14 +3,14 @@ use std::{hash::BuildHasherDefault, mem::take}; ...@@ -3,14 +3,14 @@ use std::{hash::BuildHasherDefault, mem::take};
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use turbo_tasks::FxIndexSet; use turbo_tasks::FxIndexSet;
use crate::analyzer::{JsValue, jsvalue::similar::SimilarJsValue}; use crate::analyzer::{Bump, BumpVec, JsValue, jsvalue::similar::SimilarJsValue};
// Alternatives management // Alternatives management
impl JsValue { impl<'a> JsValue<'a> {
/// Add an alternative to the current value. Might be a no-op if the value /// Add an alternative to the current value. Might be a no-op if the value
/// already contains this alternative. Potentially expensive operation /// already contains this alternative. Potentially expensive operation
/// as it has to compare the value with all existing alternatives. /// as it has to compare the value with all existing alternatives.
pub(crate) fn add_alt(&mut self, v: Self) { pub(crate) fn add_alt(&mut self, arena: &'a Bump, v: Self) {
if self == &v { if self == &v {
return; return;
} }
...@@ -23,13 +23,13 @@ impl JsValue { ...@@ -23,13 +23,13 @@ impl JsValue {
{ {
if !values.contains(&v) { if !values.contains(&v) {
*c += v.total_nodes(); *c += v.total_nodes();
values.push(v); values.push(arena, v);
} }
} else { } else {
let l = take(self); let l = take(self);
*self = JsValue::Alternatives { *self = JsValue::Alternatives {
total_nodes: 1 + l.total_nodes() + v.total_nodes(), total_nodes: 1 + l.total_nodes() + v.total_nodes(),
values: vec![l, v], values: BumpVec::from_iter_in(arena, [l, v]),
logical_property: None, logical_property: None,
}; };
} }
...@@ -37,10 +37,10 @@ impl JsValue { ...@@ -37,10 +37,10 @@ impl JsValue {
} }
// Normalization // Normalization
impl JsValue { impl<'a> JsValue<'a> {
/// Normalizes only the current node. Nested alternatives, concatenations, /// Normalizes only the current node. Nested alternatives, concatenations,
/// or operations are collapsed. /// or operations are collapsed.
pub fn normalize_shallow(&mut self) { pub fn normalize_shallow(&mut self, arena: &'a Bump) {
match self { match self {
JsValue::Alternatives { JsValue::Alternatives {
total_nodes: _, total_nodes: _,
...@@ -54,7 +54,9 @@ impl JsValue { ...@@ -54,7 +54,9 @@ impl JsValue {
values.len(), values.len(),
BuildHasherDefault::<FxHasher>::default(), BuildHasherDefault::<FxHasher>::default(),
); );
for v in take(values) { // Take the children out so we can rebuild `values` in place.
let taken = take(values);
for v in taken {
match v { match v {
JsValue::Alternatives { JsValue::Alternatives {
total_nodes: _, total_nodes: _,
...@@ -73,32 +75,34 @@ impl JsValue { ...@@ -73,32 +75,34 @@ impl JsValue {
if set.len() == 1 { if set.len() == 1 {
*self = set.into_iter().next().unwrap().0; *self = set.into_iter().next().unwrap().0;
} else { } else {
*values = set.into_iter().map(|v| v.0).collect(); *values = BumpVec::from_iter_in(arena, set.into_iter().map(|v| v.0));
self.update_total_nodes(); self.update_total_nodes();
} }
} }
} }
JsValue::Concat(_, v) => { JsValue::Concat(_, v) => {
// Remove empty strings
v.retain(|v| v.as_str() != Some(""));
// TODO(kdy1): Remove duplicate // TODO(kdy1): Remove duplicate
let mut new: Vec<JsValue> = vec![]; let taken = take(v);
for v in take(v) { let mut new: BumpVec<JsValue> = BumpVec::with_capacity_in(arena, taken.len());
for v in taken {
// Remove empty strings
if v.as_str() == Some("") {
continue;
}
if let Some(str) = v.as_str() { if let Some(str) = v.as_str() {
if let Some(last) = new.last_mut() { if let Some(last) = new.last_mut() {
if let Some(last_str) = last.as_str() { if let Some(last_str) = last.as_str() {
*last = [last_str, str].concat().into(); *last = [last_str, str].concat().into();
} else { } else {
new.push(v); new.push(arena, v);
} }
} else { } else {
new.push(v); new.push(arena, v);
} }
} else if let JsValue::Concat(_, v) = v { } else if let JsValue::Concat(_, v) = v {
new.extend(v); new.extend(arena, v);
} else { } else {
new.push(v); new.push(arena, v);
} }
} }
if new.len() == 1 { if new.len() == 1 {
...@@ -109,29 +113,31 @@ impl JsValue { ...@@ -109,29 +113,31 @@ impl JsValue {
} }
} }
JsValue::Add(_, v) => { JsValue::Add(_, v) => {
let mut added: Vec<JsValue> = Vec::new(); let taken = take(v);
let mut iter = take(v).into_iter(); let mut added: BumpVec<JsValue> = BumpVec::with_capacity_in(arena, taken.len());
let mut iter = taken.into_iter();
while let Some(item) = iter.next() { while let Some(item) = iter.next() {
if item.is_string() == Some(true) { if item.is_string() == Some(true) {
let mut concat = match added.len() { let mut concat: BumpVec<JsValue> = match added.len() {
0 => Vec::new(), 0 => BumpVec::new(),
1 => vec![added.into_iter().next().unwrap()], 1 => BumpVec::from_iter_in(arena, [added.into_iter().next().unwrap()]),
_ => vec![JsValue::Add( _ => BumpVec::from_iter_in(
arena,
[JsValue::Add(
1 + added.iter().map(|v| v.total_nodes()).sum::<u32>(), 1 + added.iter().map(|v| v.total_nodes()).sum::<u32>(),
added, added,
)], )],
),
}; };
concat.push(item); concat.push(arena, item);
for item in iter.by_ref() { concat.extend(arena, iter);
concat.push(item);
}
*self = JsValue::Concat( *self = JsValue::Concat(
1 + concat.iter().map(|v| v.total_nodes()).sum::<u32>(), 1 + concat.iter().map(|v| v.total_nodes()).sum::<u32>(),
concat, concat,
); );
return; return;
} else { } else {
added.push(item); added.push(arena, item);
} }
} }
if added.len() == 1 { if added.len() == 1 {
...@@ -152,15 +158,16 @@ impl JsValue { ...@@ -152,15 +158,16 @@ impl JsValue {
} }
}) => { }) => {
// Taking the old list and constructing a new merged list // Taking the old list and constructing a new merged list
for mut v in take(list).into_iter() { let taken = take(list);
for mut v in taken {
if let JsValue::Logical(_, inner_op, inner_list) = &mut v { if let JsValue::Logical(_, inner_op, inner_list) = &mut v {
if inner_op == op { if inner_op == op {
list.append(inner_list); list.extend(arena, take(inner_list));
} else { } else {
list.push(v); list.push(arena, v);
} }
} else { } else {
list.push(v); list.push(arena, v);
} }
} }
self.update_total_nodes(); self.update_total_nodes();
...@@ -170,12 +177,12 @@ impl JsValue { ...@@ -170,12 +177,12 @@ impl JsValue {
} }
/// Normalizes the current node and all nested nodes. /// Normalizes the current node and all nested nodes.
pub fn normalize(&mut self) { pub fn normalize(&mut self, arena: &'a Bump) {
self.for_each_children_mut(&mut |child| { self.for_each_children_mut(&mut |child| {
child.normalize(); child.normalize(arena);
true true
}); });
self.normalize_shallow(); self.normalize_shallow(arena);
} }
} }
......
...@@ -3,8 +3,8 @@ use std::hash::Hash; ...@@ -3,8 +3,8 @@ use std::hash::Hash;
use crate::analyzer::{CallList, JsValue, MemberCallList, ModuleValue, ObjectPart}; use crate::analyzer::{CallList, JsValue, MemberCallList, ModuleValue, ObjectPart};
// Like equality, but with depth limit // Like equality, but with depth limit
impl JsValue { impl<'a> JsValue<'a> {
pub(super) fn all_similar(a: &[JsValue], b: &[JsValue], depth: usize) -> bool { pub(super) fn all_similar(a: &[JsValue<'a>], b: &[JsValue<'a>], depth: usize) -> bool {
if a.len() != b.len() { if a.len() != b.len() {
return false; return false;
} }
...@@ -12,12 +12,12 @@ impl JsValue { ...@@ -12,12 +12,12 @@ impl JsValue {
} }
/// Check if the values are equal up to the given depth. Might return false /// Check if the values are equal up to the given depth. Might return false
/// even if the values are equal when hitting the depth limit. /// even if the values are equal when hitting the depth limit.
fn similar(&self, other: &JsValue, depth: usize) -> bool { fn similar(&self, other: &JsValue<'a>, depth: usize) -> bool {
if depth == 0 { if depth == 0 {
return false; return false;
} }
fn all_parts_similar(a: &[ObjectPart], b: &[ObjectPart], depth: usize) -> bool { fn all_parts_similar<'b>(a: &[ObjectPart<'b>], b: &[ObjectPart<'b>], depth: usize) -> bool {
if a.len() != b.len() { if a.len() != b.len() {
return false; return false;
} }
...@@ -134,14 +134,18 @@ impl JsValue { ...@@ -134,14 +134,18 @@ impl JsValue {
return; return;
} }
fn all_similar_hash<H: std::hash::Hasher>(slice: &[JsValue], state: &mut H, depth: usize) { fn all_similar_hash<H: std::hash::Hasher>(
slice: &[JsValue<'_>],
state: &mut H,
depth: usize,
) {
for item in slice { for item in slice {
item.similar_hash(state, depth); item.similar_hash(state, depth);
} }
} }
fn all_parts_similar_hash<H: std::hash::Hasher>( fn all_parts_similar_hash<H: std::hash::Hasher>(
slice: &[ObjectPart], slice: &[ObjectPart<'_>],
state: &mut H, state: &mut H,
depth: usize, depth: usize,
) { ) {
...@@ -178,12 +182,12 @@ impl JsValue { ...@@ -178,12 +182,12 @@ impl JsValue {
| JsValue::Logical(_, _, v) => all_similar_hash(v, state, depth - 1), | JsValue::Logical(_, _, v) => all_similar_hash(v, state, depth - 1),
JsValue::Not(_, v) => v.similar_hash(state, depth - 1), JsValue::Not(_, v) => v.similar_hash(state, depth - 1),
JsValue::New(_, call) => { JsValue::New(_, call) => {
call.for_each_children(&mut |child: &JsValue| { call.for_each_children(&mut |child: &JsValue<'_>| {
child.similar_hash(state, depth - 1); child.similar_hash(state, depth - 1);
}); });
} }
JsValue::Call(_, call) => { JsValue::Call(_, call) => {
call.for_each_children(&mut |child: &JsValue| { call.for_each_children(&mut |child: &JsValue<'_>| {
child.similar_hash(state, depth - 1); child.similar_hash(state, depth - 1);
}); });
} }
...@@ -191,7 +195,7 @@ impl JsValue { ...@@ -191,7 +195,7 @@ impl JsValue {
all_similar_hash(args, state, depth - 1); all_similar_hash(args, state, depth - 1);
} }
JsValue::MemberCall(_, call) => { JsValue::MemberCall(_, call) => {
call.for_each_children(&mut |child: &JsValue| { call.for_each_children(&mut |child: &JsValue<'_>| {
child.similar_hash(state, depth - 1); child.similar_hash(state, depth - 1);
}); });
} }
...@@ -249,17 +253,17 @@ const SIMILAR_HASH_DEPTH: usize = 2; ...@@ -249,17 +253,17 @@ const SIMILAR_HASH_DEPTH: usize = 2;
/// A wrapper around `JsValue` that implements `PartialEq` and `Hash` by /// A wrapper around `JsValue` that implements `PartialEq` and `Hash` by
/// comparing the values with a depth of [SIMILAR_EQ_DEPTH] and hashing values /// comparing the values with a depth of [SIMILAR_EQ_DEPTH] and hashing values
/// with a depth of [SIMILAR_HASH_DEPTH]. /// with a depth of [SIMILAR_HASH_DEPTH].
pub(super) struct SimilarJsValue(pub(super) JsValue); pub(super) struct SimilarJsValue<'a>(pub(super) JsValue<'a>);
impl PartialEq for SimilarJsValue { impl PartialEq for SimilarJsValue<'_> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.0.similar(&other.0, SIMILAR_EQ_DEPTH) self.0.similar(&other.0, SIMILAR_EQ_DEPTH)
} }
} }
impl Eq for SimilarJsValue {} impl Eq for SimilarJsValue<'_> {}
impl Hash for SimilarJsValue { impl Hash for SimilarJsValue<'_> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.similar_hash(state, SIMILAR_HASH_DEPTH) self.0.similar_hash(state, SIMILAR_HASH_DEPTH)
} }
......
use crate::analyzer::{JsValue, ObjectPart}; use crate::analyzer::{JsValue, ObjectPart};
// Visiting // Visiting
impl JsValue { impl<'a> JsValue<'a> {
/// Calls a function for each child of the node. Allows mutating the node. /// Calls a function for each child of the node. Allows mutating the node.
/// Updates the total nodes count after mutation. /// Updates the total nodes count after mutation.
pub fn for_each_children_mut( pub fn for_each_children_mut(
&mut self, &mut self,
visitor: &mut impl FnMut(&mut JsValue) -> bool, visitor: &mut impl FnMut(&mut JsValue<'a>) -> bool,
) -> bool { ) -> bool {
match self { match self {
JsValue::Alternatives { JsValue::Alternatives {
...@@ -158,7 +158,7 @@ impl JsValue { ...@@ -158,7 +158,7 @@ impl JsValue {
/// node. Updates the total nodes count after mutation. /// node. Updates the total nodes count after mutation.
pub fn for_each_early_children_mut( pub fn for_each_early_children_mut(
&mut self, &mut self,
visitor: &mut impl FnMut(&mut JsValue) -> bool, visitor: &mut impl FnMut(&mut JsValue<'a>) -> bool,
) -> bool { ) -> bool {
match self { match self {
JsValue::New(_, call) if !call.args().is_empty() => { JsValue::New(_, call) if !call.args().is_empty() => {
...@@ -199,7 +199,7 @@ impl JsValue { ...@@ -199,7 +199,7 @@ impl JsValue {
/// node. Updates the total nodes count after mutation. /// node. Updates the total nodes count after mutation.
pub fn for_each_late_children_mut( pub fn for_each_late_children_mut(
&mut self, &mut self,
visitor: &mut impl FnMut(&mut JsValue) -> bool, visitor: &mut impl FnMut(&mut JsValue<'a>) -> bool,
) -> bool { ) -> bool {
match self { match self {
JsValue::New(_, call) if !call.args().is_empty() => { JsValue::New(_, call) if !call.args().is_empty() => {
...@@ -250,13 +250,13 @@ impl JsValue { ...@@ -250,13 +250,13 @@ impl JsValue {
} }
/// Visit the node and all its children with a function. /// Visit the node and all its children with a function.
pub fn visit(&self, visitor: &mut impl FnMut(&JsValue)) { pub fn visit(&self, visitor: &mut impl FnMut(&JsValue<'a>)) {
self.for_each_children(&mut |value| value.visit(visitor)); self.for_each_children(&mut |value| value.visit(visitor));
visitor(self); visitor(self);
} }
/// Calls a function for all children of the node. /// Calls a function for all children of the node.
pub fn for_each_children(&self, visitor: &mut impl FnMut(&JsValue)) { pub fn for_each_children(&self, visitor: &mut impl FnMut(&JsValue<'a>)) {
match self { match self {
JsValue::Alternatives { JsValue::Alternatives {
total_nodes: _, total_nodes: _,
......
...@@ -6,24 +6,27 @@ use rustc_hash::{FxHashMap, FxHashSet}; ...@@ -6,24 +6,27 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::ecma::ast::Id; use swc_core::ecma::ast::Id;
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use super::{JsValue, graph::VarGraph}; use super::{Bump, JsValue, ThreadLocal, graph::VarGraph};
use crate::analyzer::BumpVec;
pub async fn link<'a, B, RB, F, RF>( pub async fn link<'a, 'l, B, RB, F, RF>(
graph: &VarGraph, arena: &'a ThreadLocal<Bump>,
mut val: JsValue, graph: &'l VarGraph<'a>,
early_visitor: &B, mut val: JsValue<'a>,
visitor: &F, early_visitor: &'l B,
fun_args_values: &Mutex<FxHashMap<u32, Vec<JsValue>>>, visitor: &'l F,
var_cache: &Mutex<FxHashMap<Id, JsValue>>, fun_args_values: &Mutex<FxHashMap<u32, BumpVec<'a, JsValue<'a>>>>,
) -> Result<(JsValue, u32)> var_cache: &Mutex<FxHashMap<Id, JsValue<'a>>>,
) -> Result<(JsValue<'a>, u32)>
where where
RB: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RB: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
B: 'a + Fn(JsValue) -> RB + Sync, B: 'l + Fn(JsValue<'a>) -> RB + Sync,
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RF: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync, F: 'l + Fn(JsValue<'a>) -> RF + Sync,
{ {
val.normalize(); val.normalize(arena.get_or_default());
let (val, steps) = link_internal_iterative( let (val, steps) = link_internal_iterative(
arena,
graph, graph,
val, val,
early_visitor, early_visitor,
...@@ -39,19 +42,19 @@ const LIMIT_NODE_SIZE: u32 = 100; ...@@ -39,19 +42,19 @@ const LIMIT_NODE_SIZE: u32 = 100;
const LIMIT_IN_PROGRESS_NODES: u32 = 500; const LIMIT_IN_PROGRESS_NODES: u32 = 500;
const LIMIT_LINK_STEPS: u32 = 1500; const LIMIT_LINK_STEPS: u32 = 1500;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, PartialEq)]
enum Step { enum Step<'a> {
/// Take all children out of the value (replacing temporarily with unknown) and queue them /// Take all children out of the value (replacing temporarily with unknown) and queue them
/// for processing using individual `Enter`s. /// for processing using individual `Enter`s.
Enter(JsValue), Enter(JsValue<'a>),
/// Pop however many children there are from `done` and reinsert them into the value /// Pop however many children there are from `done` and reinsert them into the value
Leave(JsValue), Leave(JsValue<'a>),
/// Remove the variable from `cycle_stack` which detects e.g. circular reassignments /// Remove the variable from `cycle_stack` which detects e.g. circular reassignments
LeaveVar(Id), LeaveVar(Id),
LeaveLate(JsValue), LeaveLate(JsValue<'a>),
/// Call the visitor callbacks, and requeue the value for further processing if it changed. /// Call the visitor callbacks, and requeue the value for further processing if it changed.
Visit(JsValue), Visit(JsValue<'a>),
EarlyVisit(JsValue), EarlyVisit(JsValue<'a>),
/// Remove the call from `fun_args_values` /// Remove the call from `fun_args_values`
LeaveCall(u32), LeaveCall(u32),
/// Placeholder that is used to momentarily reserve a slot that is only filled after /// Placeholder that is used to momentarily reserve a slot that is only filled after
...@@ -59,7 +62,7 @@ enum Step { ...@@ -59,7 +62,7 @@ enum Step {
TemporarySlot, TemporarySlot,
} }
impl Display for Step { impl Display for Step<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Step::Enter(val) => write!(f, "Enter({val})"), Step::Enter(val) => write!(f, "Enter({val})"),
...@@ -75,22 +78,23 @@ impl Display for Step { ...@@ -75,22 +78,23 @@ impl Display for Step {
} }
// If a variable was already visited in this linking call, don't visit it again. // If a variable was already visited in this linking call, don't visit it again.
pub(crate) async fn link_internal_iterative<'a, B, RB, F, RF>( pub(crate) async fn link_internal_iterative<'a, 'l, B, RB, F, RF>(
graph: &'a VarGraph, arena: &'a ThreadLocal<Bump>,
val: JsValue, graph: &'l VarGraph<'a>,
early_visitor: &'a B, val: JsValue<'a>,
visitor: &'a F, early_visitor: &'l B,
fun_args_values: &Mutex<FxHashMap<u32, Vec<JsValue>>>, visitor: &'l F,
var_cache: &Mutex<FxHashMap<Id, JsValue>>, fun_args_values: &Mutex<FxHashMap<u32, BumpVec<'a, JsValue<'a>>>>,
) -> Result<(JsValue, u32)> var_cache: &Mutex<FxHashMap<Id, JsValue<'a>>>,
) -> Result<(JsValue<'a>, u32)>
where where
RB: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RB: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
B: 'a + Fn(JsValue) -> RB + Sync, B: 'l + Fn(JsValue<'a>) -> RB + Sync,
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RF: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync, F: 'l + Fn(JsValue<'a>) -> RF + Sync,
{ {
let mut work_queue_stack: Vec<Step> = Vec::new(); let mut work_queue_stack: Vec<Step<'a>> = Vec::new();
let mut done: Vec<JsValue> = Vec::new(); let mut done: Vec<JsValue<'a>> = Vec::new();
// Tracks the number of nodes in the queue and done combined // Tracks the number of nodes in the queue and done combined
let mut total_nodes = 0; let mut total_nodes = 0;
let mut cycle_stack: FxHashSet<Id> = FxHashSet::default(); let mut cycle_stack: FxHashSet<Id> = FxHashSet::default();
...@@ -122,12 +126,12 @@ where ...@@ -122,12 +126,12 @@ where
.then(|| var_cache.lock()); .then(|| var_cache.lock());
if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) { if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) {
total_nodes += val.total_nodes(); total_nodes += val.total_nodes();
done.push(val.clone()); done.push(val.clone_in(arena.get_or_default()));
} else if let Some(value) = graph.values.get(&var) { } else if let Some(value) = graph.values.get(&var) {
cycle_stack.insert(var.clone()); cycle_stack.insert(var.clone());
work_queue_stack.push(Step::LeaveVar(var)); work_queue_stack.push(Step::LeaveVar(var));
total_nodes += value.total_nodes(); total_nodes += value.total_nodes();
work_queue_stack.push(Step::Enter(value.clone())); work_queue_stack.push(Step::Enter(value.clone_in(arena.get_or_default())));
} else { } else {
total_nodes += 1; total_nodes += 1;
done.push(JsValue::unknown( done.push(JsValue::unknown(
...@@ -142,7 +146,9 @@ where ...@@ -142,7 +146,9 @@ where
Step::LeaveVar(var) => { Step::LeaveVar(var) => {
cycle_stack.remove(&var); cycle_stack.remove(&var);
if cycle_stack.is_empty() && fun_args_values.lock().is_empty() { if cycle_stack.is_empty() && fun_args_values.lock().is_empty() {
var_cache.lock().insert(var, done.last().unwrap().clone()); var_cache
.lock()
.insert(var, done.last().unwrap().clone_in(arena.get_or_default()));
} }
} }
// Enter a function argument // Enter a function argument
...@@ -152,7 +158,7 @@ where ...@@ -152,7 +158,7 @@ where
if let Some(args) = fun_args_values.lock().get(&func_ident) { if let Some(args) = fun_args_values.lock().get(&func_ident) {
if let Some(val) = args.get(index) { if let Some(val) = args.get(index) {
total_nodes += val.total_nodes(); total_nodes += val.total_nodes();
done.push(val.clone()); done.push(val.clone_in(arena.get_or_default()));
} else { } else {
total_nodes += 1; total_nodes += 1;
done.push(JsValue::unknown_empty( done.push(JsValue::unknown_empty(
...@@ -176,7 +182,7 @@ where ...@@ -176,7 +182,7 @@ where
if matches!(call.callee(), JsValue::Function(..)) => if matches!(call.callee(), JsValue::Function(..)) =>
{ {
let (callee, args) = call.into_parts(); let (callee, args) = call.into_parts();
let JsValue::Function(function_nodes, func_ident, return_value) = callee else { let JsValue::Function(function_nodes, func_ident, mut return_value) = callee else {
unreachable!() unreachable!()
}; };
total_nodes -= 2; // Call + Function total_nodes -= 2; // Call + Function
...@@ -187,7 +193,7 @@ where ...@@ -187,7 +193,7 @@ where
} }
entry.insert(args); entry.insert(args);
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(take(&mut *return_value)));
} else { } else {
total_nodes -= return_value.total_nodes(); total_nodes -= return_value.total_nodes();
for arg in args.iter() { for arg in args.iter() {
...@@ -196,6 +202,7 @@ where ...@@ -196,6 +202,7 @@ where
total_nodes += 1; total_nodes += 1;
done.push(JsValue::unknown( done.push(JsValue::unknown(
JsValue::call_from_parts( JsValue::call_from_parts(
arena.get_or_default(),
JsValue::Function(function_nodes, func_ident, return_value), JsValue::Function(function_nodes, func_ident, return_value),
args, args,
), ),
...@@ -222,6 +229,7 @@ where ...@@ -222,6 +229,7 @@ where
Step::Enter(mut val) => { Step::Enter(mut val) => {
if !val.has_children() { if !val.has_children() {
visit( visit(
arena,
&mut total_nodes, &mut total_nodes,
&mut done, &mut done,
&mut work_queue_stack, &mut work_queue_stack,
...@@ -318,7 +326,7 @@ where ...@@ -318,7 +326,7 @@ where
done.push(JsValue::unknown_empty(true, rcstr!("node limit reached"))); done.push(JsValue::unknown_empty(true, rcstr!("node limit reached")));
continue; continue;
} }
val.normalize_shallow(); val.normalize_shallow(arena.get_or_default());
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
...@@ -341,7 +349,7 @@ where ...@@ -341,7 +349,7 @@ where
done.push(JsValue::unknown_empty(true, rcstr!("node limit reached"))); done.push(JsValue::unknown_empty(true, rcstr!("node limit reached")));
continue; continue;
} }
val.normalize_shallow(); val.normalize_shallow(arena.get_or_default());
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
...@@ -352,6 +360,7 @@ where ...@@ -352,6 +360,7 @@ where
// - visited value is put into done // - visited value is put into done
Step::Visit(val) => { Step::Visit(val) => {
visit( visit(
arena,
&mut total_nodes, &mut total_nodes,
&mut done, &mut done,
&mut work_queue_stack, &mut work_queue_stack,
...@@ -402,22 +411,23 @@ where ...@@ -402,22 +411,23 @@ where
Ok((final_value, steps)) Ok((final_value, steps))
} }
async fn visit<'a, F, RF>( async fn visit<'a, 'l, F, RF>(
arena: &'a ThreadLocal<Bump>,
total_nodes: &mut u32, total_nodes: &mut u32,
done: &mut Vec<JsValue>, done: &mut Vec<JsValue<'a>>,
work_queue_stack: &mut Vec<Step>, work_queue_stack: &mut Vec<Step<'a>>,
visitor: &'a F, visitor: &'l F,
val: JsValue, val: JsValue<'a>,
) -> Result<()> ) -> Result<()>
where where
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send, RF: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync, F: 'l + Fn(JsValue<'a>) -> RF + Sync,
{ {
*total_nodes -= val.total_nodes(); *total_nodes -= val.total_nodes();
let (mut val, visit_modified) = visitor(val).await?; let (mut val, visit_modified) = visitor(val).await?;
if visit_modified { if visit_modified {
val.normalize_shallow(); val.normalize_shallow(arena.get_or_default());
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
val.debug_assert_total_nodes_up_to_date(); val.debug_assert_total_nodes_up_to_date();
if val.total_nodes() > LIMIT_NODE_SIZE { if val.total_nodes() > LIMIT_NODE_SIZE {
......
...@@ -143,7 +143,7 @@ impl WellKnownObjectKind { ...@@ -143,7 +143,7 @@ impl WellKnownObjectKind {
/// A list of well-known functions that have special meaning in the analysis. /// A list of well-known functions that have special meaning in the analysis.
#[derive(Debug, Clone, Hash, PartialEq)] #[derive(Debug, Clone, Hash, PartialEq)]
pub enum WellKnownFunctionKind { pub enum WellKnownFunctionKind<'a> {
ArrayFilter, ArrayFilter,
ArrayForEach, ArrayForEach,
ArrayMap, ArrayMap,
...@@ -151,7 +151,7 @@ pub enum WellKnownFunctionKind { ...@@ -151,7 +151,7 @@ pub enum WellKnownFunctionKind {
PathJoin, PathJoin,
PathDirname, PathDirname,
/// `0` is the current working directory. /// `0` is the current working directory.
PathResolve(Box<JsValue>), PathResolve(&'a JsValue<'a>),
Import, Import,
Require, Require,
/// `0` is the path to resolve from (relative to the current module). /// `0` is the path to resolve from (relative to the current module).
...@@ -196,7 +196,7 @@ pub enum WellKnownFunctionKind { ...@@ -196,7 +196,7 @@ pub enum WellKnownFunctionKind {
ImportMetaGlob, ImportMetaGlob,
} }
impl WellKnownFunctionKind { impl WellKnownFunctionKind<'_> {
pub fn as_define_name(&self) -> Option<&[&str]> { pub fn as_define_name(&self) -> Option<&[&str]> {
match self { match self {
Self::Import { .. } => Some(&["import"]), Self::Import { .. } => Some(&["import"]),
......
...@@ -23,7 +23,7 @@ pub struct RequireContextOptions { ...@@ -23,7 +23,7 @@ pub struct RequireContextOptions {
/// Parse the arguments passed to a require.context invocation, validate them /// Parse the arguments passed to a require.context invocation, validate them
/// and convert them to the appropriate rust values. /// and convert them to the appropriate rust values.
pub fn parse_require_context(args: &[JsValue]) -> Result<RequireContextOptions> { pub fn parse_require_context(args: &[JsValue<'_>]) -> Result<RequireContextOptions> {
if !(1..=3).contains(&args.len()) { if !(1..=3).contains(&args.len()) {
// https://linear.app/vercel/issue/WEB-910/add-support-for-requirecontexts-mode-argument // https://linear.app/vercel/issue/WEB-910/add-support-for-requirecontexts-mode-argument
bail!("require.context() only supports 1-3 arguments (mode is not supported)"); bail!("require.context() only supports 1-3 arguments (mode is not supported)");
......
...@@ -88,7 +88,7 @@ pub struct ImportMetaGlobOptions { ...@@ -88,7 +88,7 @@ pub struct ImportMetaGlobOptions {
/// - **`as` option** (deprecated in Vite 5 in favor of `query`) is not supported. Use `query: /// - **`as` option** (deprecated in Vite 5 in favor of `query`) is not supported. Use `query:
/// '?raw'` or `query: '?url'` instead. /// '?raw'` or `query: '?url'` instead.
pub fn parse_import_meta_glob( pub fn parse_import_meta_glob(
args: &[JsValue], args: &[JsValue<'_>],
handler: &Handler, handler: &Handler,
span: Span, span: Span,
diagnostic_id: DiagnosticId, diagnostic_id: DiagnosticId,
......
...@@ -29,7 +29,7 @@ pub fn unparen(expr: &Expr) -> &Expr { ...@@ -29,7 +29,7 @@ pub fn unparen(expr: &Expr) -> &Expr {
} }
/// Converts a js-value into a Pattern for matching resources. /// Converts a js-value into a Pattern for matching resources.
pub fn js_value_to_pattern(value: &JsValue) -> Pattern { pub fn js_value_to_pattern(value: &JsValue<'_>) -> Pattern {
match value { match value {
JsValue::Constant(v) => Pattern::Constant(match v { JsValue::Constant(v) => Pattern::Constant(match v {
ConstantValue::Str(str) => { ConstantValue::Str(str) => {
...@@ -198,7 +198,7 @@ pub enum AstPathRange { ...@@ -198,7 +198,7 @@ pub enum AstPathRange {
/// Converts a module value (ie an import) to a well known object, /// Converts a module value (ie an import) to a well known object,
/// which we specifically handle. /// which we specifically handle.
pub fn module_value_to_well_known_object(module_value: &ModuleValue) -> Option<JsValue> { pub fn module_value_to_well_known_object<'a>(module_value: &ModuleValue) -> Option<JsValue<'a>> {
Some(match module_value.module.as_bytes() { Some(match module_value.module.as_bytes() {
b"node:path" | b"path" => JsValue::WellKnownObject(WellKnownObjectKind::PathModule), b"node:path" | b"path" => JsValue::WellKnownObject(WellKnownObjectKind::PathModule),
b"node:fs/promises" | b"fs/promises" => { b"node:fs/promises" | b"fs/promises" => {
...@@ -300,12 +300,13 @@ mod tests { ...@@ -300,12 +300,13 @@ mod tests {
use turbopack_core::resolve::pattern::Pattern; use turbopack_core::resolve::pattern::Pattern;
use crate::{ use crate::{
analyzer::{ConstantString, ConstantValue, JsValue}, analyzer::{BumpVec, ConstantString, ConstantValue, JsValue, ThreadLocal},
utils::js_value_to_pattern, utils::js_value_to_pattern,
}; };
#[test] #[test]
fn test_path_normalization_in_pattern() { fn test_path_normalization_in_pattern() {
let arena = ThreadLocal::new();
assert_eq!( assert_eq!(
Pattern::Constant(rcstr!("hello/world")), Pattern::Constant(rcstr!("hello/world")),
js_value_to_pattern(&JsValue::Constant(ConstantValue::Str( js_value_to_pattern(&JsValue::Constant(ConstantValue::Str(
...@@ -317,11 +318,14 @@ mod tests { ...@@ -317,11 +318,14 @@ mod tests {
Pattern::Constant(rcstr!("hello/world")), Pattern::Constant(rcstr!("hello/world")),
js_value_to_pattern(&JsValue::Concat( js_value_to_pattern(&JsValue::Concat(
1, 1,
vec![ BumpVec::from_iter_in(
arena.get_or_default(),
[
rcstr!("hello").into(), rcstr!("hello").into(),
rcstr!("\\").into(), rcstr!("\\").into(),
rcstr!("world").into() rcstr!("world").into()
] ]
)
)) ))
); );
} }
......
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