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 = [
"strsim 0.11.1",
"swc_core",
"swc_sourcemap",
"thread_local",
"tokio",
"tracing",
"turbo-bincode",
......
This diff is collapsed.
......@@ -46,6 +46,7 @@ serde_json = { workspace = true, features = ["raw_value"] }
swc_sourcemap = { workspace = true }
smallvec = { workspace = true }
strsim = { workspace = true }
thread_local = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
turbo-bincode = { workspace = true }
......
......@@ -25,6 +25,7 @@ use turbopack_core::{
use turbopack_ecmascript::{
AnalyzeMode,
analyzer::{
Bump, ThreadLocal,
graph::{EvalContext, VarGraph, create_graph},
imports::ImportAttributes,
linker::link,
......@@ -72,7 +73,11 @@ pub fn benchmark(c: &mut Criterion) {
Default::default(),
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(
arena.get_or_default(),
&program,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
......@@ -83,6 +88,7 @@ pub fn benchmark(c: &mut Criterion) {
program,
eval_context,
var_graph,
arena,
};
group.bench_with_input(
......@@ -99,17 +105,20 @@ pub fn benchmark(c: &mut Criterion) {
struct BenchInput {
program: Program,
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) {
b.iter(|| {
create_graph(
let arena = ThreadLocal::new();
criterion::black_box(create_graph(
arena.get_or_default(),
&input.program,
&input.eval_context,
AnalyzeMode::CodeGenerationAndTracing,
true,
)
));
});
}
......@@ -117,6 +126,8 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
let arena = input.arena;
let var_graph = input.var_graph.clone();
b.to_async(rt).iter_custom(move |iters| {
......@@ -150,10 +161,18 @@ fn bench_link(b: &mut Bencher, input: &BenchInput) {
let var_cache = Default::default();
for value in var_graph.values.values() {
link(
arena,
&var_graph,
value.clone(),
&early_visitor,
&(|val| visitor(val, compile_time_info, ImportAttributes::empty_ref())),
value.clone_in(arena.get_or_default()),
&(|val| early_visitor(arena, val)),
&(|val| {
visitor(
arena,
val,
compile_time_info,
ImportAttributes::empty_ref(),
)
}),
&Default::default(),
&var_cache,
)
......
......@@ -168,6 +168,19 @@ impl<'a, T> BumpVec<'a, T> {
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
/// the suffix `[at, len)`, moved into a fresh arena allocation.
pub fn split_off(&mut self, bump: &'a Bump, at: usize) -> Self {
......@@ -280,8 +293,15 @@ impl<T> Iterator for IntoIter<'_, T> {
self.idx += 1;
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> {
fn drop(&mut self) {
// SAFETY: indices `[idx, len)` are still initialized; drop them in place exactly once.
......@@ -405,6 +425,45 @@ mod tests {
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]
fn split_off_prefix_and_suffix() {
let bump = Bump::new();
......
use bumpalo::boxed::Box as BumpBox;
use swc_core::{atoms::Atom, common::Span, ecma::visit::fields::*};
use turbo_rcstr::RcStr;
use turbopack_core::resolve::ExportUsage;
use crate::{analyzer::JsValue, utils::AstPathRange};
use crate::{
analyzer::{Bump, JsValue},
utils::AstPathRange,
};
#[derive(Debug)]
pub struct EffectsBlock {
pub effects: Vec<Effect>,
pub struct EffectsBlock<'a> {
pub effects: Vec<Effect<'a>>,
pub range: AstPathRange,
}
impl EffectsBlock {
impl EffectsBlock<'_> {
pub fn is_empty(&self) -> bool {
self.effects.is_empty()
}
}
#[derive(Debug)]
pub enum ConditionalKind {
pub enum ConditionalKind<'a> {
/// 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.
IfElse {
then: Box<EffectsBlock>,
r#else: Box<EffectsBlock>,
then: Box<EffectsBlock<'a>>,
r#else: Box<EffectsBlock<'a>>,
},
/// 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 { ... }
/// else { ... return ... } ...` statement.
IfElseMultiple {
then: Vec<Box<EffectsBlock>>,
r#else: Vec<Box<EffectsBlock>>,
then: Vec<Box<EffectsBlock<'a>>>,
r#else: Vec<Box<EffectsBlock<'a>>>,
},
/// The expressions on the right side of the `?:` operator.
Ternary {
then: Box<EffectsBlock>,
r#else: Box<EffectsBlock>,
then: Box<EffectsBlock<'a>>,
r#else: Box<EffectsBlock<'a>>,
},
/// 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.
Or { expr: Box<EffectsBlock> },
Or { expr: Box<EffectsBlock<'a>> },
/// 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.
Labeled { body: Box<EffectsBlock> },
Labeled { body: Box<EffectsBlock<'a>> },
}
impl ConditionalKind {
impl<'a> ConditionalKind<'a> {
/// Normalizes all contained values.
pub fn normalize(&mut self) {
pub fn normalize(&mut self, arena: &'a Bump) {
match self {
ConditionalKind::If { then: block }
| ConditionalKind::Else { r#else: block }
......@@ -58,28 +62,28 @@ impl ConditionalKind {
| ConditionalKind::Or { expr: block, .. }
| ConditionalKind::NullishCoalescing { expr: block, .. } => {
for effect in &mut block.effects {
effect.normalize();
effect.normalize(arena);
}
}
ConditionalKind::IfElse { then, r#else, .. }
| ConditionalKind::Ternary { then, r#else, .. } => {
for effect in &mut then.effects {
effect.normalize();
effect.normalize(arena);
}
for effect in &mut r#else.effects {
effect.normalize();
effect.normalize(arena);
}
}
ConditionalKind::IfElseMultiple { then, r#else, .. } => {
for block in then.iter_mut().chain(r#else.iter_mut()) {
for effect in &mut block.effects {
effect.normalize();
effect.normalize(arena);
}
}
}
ConditionalKind::Labeled { body } => {
for effect in &mut body.effects {
effect.normalize();
effect.normalize(arena);
}
}
}
......@@ -87,21 +91,21 @@ impl ConditionalKind {
}
#[derive(Debug)]
pub enum EffectArg {
Value(JsValue),
Closure(JsValue, Box<EffectsBlock>),
pub enum EffectArg<'a> {
Value(JsValue<'a>),
Closure(JsValue<'a>, Box<EffectsBlock<'a>>),
Spread,
}
impl EffectArg {
impl<'a> EffectArg<'a> {
/// Normalizes all contained values.
pub fn normalize(&mut self) {
pub fn normalize(&mut self, arena: &'a Bump) {
match self {
EffectArg::Value(value) => value.normalize(),
EffectArg::Value(value) => value.normalize(arena),
EffectArg::Closure(value, effects) => {
value.normalize();
value.normalize(arena);
for effect in &mut effects.effects {
effect.normalize();
effect.normalize(arena);
}
}
EffectArg::Spread => {}
......@@ -110,21 +114,21 @@ impl EffectArg {
}
#[derive(Debug)]
pub enum Effect {
pub enum Effect<'a> {
/// Some condition which affects which effects might be executed. If the
/// condition evaluates to some compile-time constant, we can use that
/// to determine which effects are executed and remove the others.
Conditional {
condition: Box<JsValue>,
kind: Box<ConditionalKind>,
condition: BumpBox<'a, JsValue<'a>>,
kind: Box<ConditionalKind<'a>>,
/// The ast path to the condition.
ast_path: Vec<AstParentKind>,
span: Span,
},
/// A function call or a new call of a function.
Call {
func: Box<JsValue>,
args: Vec<EffectArg>,
func: BumpBox<'a, JsValue<'a>>,
args: Vec<EffectArg<'a>>,
ast_path: Vec<AstParentKind>,
span: Span,
in_try: bool,
......@@ -132,9 +136,9 @@ pub enum Effect {
},
/// A function call or a new call of a property of an object.
MemberCall {
obj: Box<JsValue>,
prop: Box<JsValue>,
args: Vec<EffectArg>,
obj: BumpBox<'a, JsValue<'a>>,
prop: BumpBox<'a, JsValue<'a>>,
args: Vec<EffectArg<'a>>,
ast_path: Vec<AstParentKind>,
span: Span,
in_try: bool,
......@@ -142,8 +146,8 @@ pub enum Effect {
},
/// A property access.
Member {
obj: Box<JsValue>,
prop: Box<JsValue>,
obj: BumpBox<'a, JsValue<'a>>,
prop: BumpBox<'a, JsValue<'a>>,
ast_path: Vec<AstParentKind>,
span: Span,
},
......@@ -162,7 +166,7 @@ pub enum Effect {
},
/// A typeof expression
TypeOf {
arg: Box<JsValue>,
arg: BumpBox<'a, JsValue<'a>>,
ast_path: Vec<AstParentKind>,
span: Span,
},
......@@ -182,7 +186,7 @@ pub enum Effect {
/// - `import(/* webpackExports: ["a"] */ './lib')` (magic comment)
/// - `import(/* turbopackExports: ["a"] */ './lib')` (magic comment)
DynamicImport {
args: Vec<EffectArg>,
args: Vec<EffectArg<'a>>,
ast_path: Vec<AstParentKind>,
span: Span,
in_try: bool,
......@@ -193,43 +197,43 @@ pub enum Effect {
Unreachable { start_ast_path: Vec<AstParentKind> },
}
impl Effect {
impl<'a> Effect<'a> {
/// Normalizes all contained values.
pub fn normalize(&mut self) {
pub fn normalize(&mut self, arena: &'a Bump) {
match self {
Effect::Conditional {
condition, kind, ..
} => {
condition.normalize();
kind.normalize();
condition.normalize(arena);
kind.normalize(arena);
}
Effect::Call { func, args, .. } => {
func.normalize();
func.normalize(arena);
for arg in args.iter_mut() {
arg.normalize();
arg.normalize(arena);
}
}
Effect::MemberCall {
obj, prop, args, ..
} => {
obj.normalize();
prop.normalize();
obj.normalize(arena);
prop.normalize(arena);
for arg in args.iter_mut() {
arg.normalize();
arg.normalize(arena);
}
}
Effect::Member { obj, prop, .. } => {
obj.normalize();
prop.normalize();
obj.normalize(arena);
prop.normalize(arena);
}
Effect::DynamicImport { args, .. } => {
for arg in args.iter_mut() {
arg.normalize();
arg.normalize(arena);
}
}
Effect::ImportedBinding { .. } => {}
Effect::TypeOf { arg, .. } => {
arg.normalize();
arg.normalize(arena);
}
Effect::FreeVar { .. } => {}
Effect::ImportMeta { .. } => {}
......
......@@ -12,7 +12,7 @@ pub use crate::analyzer::graph::{
};
use crate::{
AnalyzeMode,
analyzer::{JsValue, graph::visitor::Analyzer},
analyzer::{Bump, JsValue, graph::visitor::Analyzer},
code_gen::CodeGen,
};
......@@ -21,8 +21,8 @@ mod eval_context;
mod visitor;
#[derive(Debug)]
pub struct VarGraph {
pub values: FxHashMap<Id, JsValue>,
pub struct VarGraph<'a> {
pub values: FxHashMap<Id, JsValue<'a>>,
/// Map [`JsValue::FreeVar`] names to their [`Id`] to facilitate lookups into [`Self::values`].
///
......@@ -30,50 +30,50 @@ pub struct VarGraph {
/// non-trivial values.
pub free_var_ids: FxHashMap<Atom, Id>,
pub effects: Vec<Effect>,
pub effects: Vec<Effect<'a>>,
// Some unconditional codegens, usually for ESM items.
pub code_gens: Vec<CodeGen>,
}
impl VarGraph {
pub fn normalize(&mut self) {
impl<'a> VarGraph<'a> {
pub fn normalize(&mut self, arena: &'a Bump) {
for value in self.values.values_mut() {
value.normalize();
value.normalize(arena);
}
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,
eval_context: &EvalContext,
analyze_mode: AnalyzeMode,
supports_block_scoping: bool,
) -> VarGraph {
let mut graph = VarGraph {
values: Default::default(),
free_var_ids: Default::default(),
) -> VarGraph<'a> {
let mut analyzer = Analyzer {
arena,
analyze_mode,
data: VarGraph {
values: Default::default(),
free_var_ids: Default::default(),
effects: Default::default(),
code_gens: Default::default(),
},
eval_context,
state: Default::default(),
effects: Default::default(),
hoisted_effects: Default::default(),
code_gens: Default::default(),
supports_block_scoping,
};
m.visit_with_ast_path(
&mut Analyzer {
analyze_mode,
data: &mut graph,
eval_context,
state: Default::default(),
effects: Default::default(),
hoisted_effects: Default::default(),
code_gens: Default::default(),
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
}
......@@ -27,7 +27,7 @@ use super::{JsValue, ModuleValue, top_level_await::has_top_level_await};
use crate::{
SpecifiedModuleType,
analyzer::{
ConstantValue, ObjectPart,
Bump, ConstantValue, ObjectPart,
graph::{AssignmentScope, AssignmentScopes, EvalContext},
is_unresolved,
},
......@@ -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 JsValue::Object { parts, .. } = with else {
......@@ -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) {
let r = &self.references[*i];
return Some(JsValue::member(
Box::new(JsValue::Module(ModuleValue {
arena,
JsValue::Module(ModuleValue {
module: r.module_path.clone(),
annotations: r.annotations.clone(),
})),
Box::new(i_sym.clone().into()),
}),
i_sym.clone().into(),
));
}
if let Some(i) = self.namespace_imports.get(id) {
......
......@@ -14,22 +14,34 @@ use swc_core::{
use turbo_rcstr::RcStr;
use crate::{
analyzer::{JsValue, imports::ImportAnnotations},
analyzer::{Bump, JsValue, imports::ImportAnnotations},
utils::StringifyJs,
};
#[derive(Debug, Clone, Hash, PartialEq)]
pub enum ObjectPart {
KeyValue(JsValue, JsValue),
Spread(JsValue),
#[derive(Debug, Hash, PartialEq)]
pub enum ObjectPart<'a> {
KeyValue(JsValue<'a>, JsValue<'a>),
Spread(JsValue<'a>),
}
impl Default for ObjectPart {
impl Default for ObjectPart<'_> {
fn default() -> Self {
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)]
pub struct ConstantNumber(pub f64);
......
......@@ -4,7 +4,7 @@ use either::Either;
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 {
match self {
ObjectPart::KeyValue(key, value) => write!(f, "{key}: {value}"),
......@@ -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 {
match self {
JsValue::Constant(v) => write!(f, "{v}"),
......
......@@ -5,8 +5,12 @@ use either::Either;
use crate::analyzer::{JsValue, ModuleValue, ObjectPart, jsvalue::pretty_join};
// Methods for explaining a value
impl JsValue {
pub fn explain_args(args: &[JsValue], depth: usize, unknown_depth: usize) -> (String, String) {
impl JsValue<'_> {
pub fn explain_args(
args: &[JsValue<'_>],
depth: usize,
unknown_depth: usize,
) -> (String, String) {
let mut hints = Vec::new();
let args = args
.iter()
......
......@@ -3,14 +3,14 @@ use std::{hash::BuildHasherDefault, mem::take};
use rustc_hash::FxHasher;
use turbo_tasks::FxIndexSet;
use crate::analyzer::{JsValue, jsvalue::similar::SimilarJsValue};
use crate::analyzer::{Bump, BumpVec, JsValue, jsvalue::similar::SimilarJsValue};
// Alternatives management
impl JsValue {
impl<'a> JsValue<'a> {
/// Add an alternative to the current value. Might be a no-op if the value
/// already contains this alternative. Potentially expensive operation
/// 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 {
return;
}
......@@ -23,13 +23,13 @@ impl JsValue {
{
if !values.contains(&v) {
*c += v.total_nodes();
values.push(v);
values.push(arena, v);
}
} else {
let l = take(self);
*self = JsValue::Alternatives {
total_nodes: 1 + l.total_nodes() + v.total_nodes(),
values: vec![l, v],
values: BumpVec::from_iter_in(arena, [l, v]),
logical_property: None,
};
}
......@@ -37,10 +37,10 @@ impl JsValue {
}
// Normalization
impl JsValue {
impl<'a> JsValue<'a> {
/// Normalizes only the current node. Nested alternatives, concatenations,
/// or operations are collapsed.
pub fn normalize_shallow(&mut self) {
pub fn normalize_shallow(&mut self, arena: &'a Bump) {
match self {
JsValue::Alternatives {
total_nodes: _,
......@@ -54,7 +54,9 @@ impl JsValue {
values.len(),
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 {
JsValue::Alternatives {
total_nodes: _,
......@@ -73,32 +75,34 @@ impl JsValue {
if set.len() == 1 {
*self = set.into_iter().next().unwrap().0;
} 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();
}
}
}
JsValue::Concat(_, v) => {
// Remove empty strings
v.retain(|v| v.as_str() != Some(""));
// TODO(kdy1): Remove duplicate
let mut new: Vec<JsValue> = vec![];
for v in take(v) {
let taken = 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(last) = new.last_mut() {
if let Some(last_str) = last.as_str() {
*last = [last_str, str].concat().into();
} else {
new.push(v);
new.push(arena, v);
}
} else {
new.push(v);
new.push(arena, v);
}
} else if let JsValue::Concat(_, v) = v {
new.extend(v);
new.extend(arena, v);
} else {
new.push(v);
new.push(arena, v);
}
}
if new.len() == 1 {
......@@ -109,29 +113,31 @@ impl JsValue {
}
}
JsValue::Add(_, v) => {
let mut added: Vec<JsValue> = Vec::new();
let mut iter = take(v).into_iter();
let taken = take(v);
let mut added: BumpVec<JsValue> = BumpVec::with_capacity_in(arena, taken.len());
let mut iter = taken.into_iter();
while let Some(item) = iter.next() {
if item.is_string() == Some(true) {
let mut concat = match added.len() {
0 => Vec::new(),
1 => vec![added.into_iter().next().unwrap()],
_ => vec![JsValue::Add(
1 + added.iter().map(|v| v.total_nodes()).sum::<u32>(),
added,
)],
let mut concat: BumpVec<JsValue> = match added.len() {
0 => BumpVec::new(),
1 => BumpVec::from_iter_in(arena, [added.into_iter().next().unwrap()]),
_ => BumpVec::from_iter_in(
arena,
[JsValue::Add(
1 + added.iter().map(|v| v.total_nodes()).sum::<u32>(),
added,
)],
),
};
concat.push(item);
for item in iter.by_ref() {
concat.push(item);
}
concat.push(arena, item);
concat.extend(arena, iter);
*self = JsValue::Concat(
1 + concat.iter().map(|v| v.total_nodes()).sum::<u32>(),
concat,
);
return;
} else {
added.push(item);
added.push(arena, item);
}
}
if added.len() == 1 {
......@@ -152,15 +158,16 @@ impl JsValue {
}
}) => {
// 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 inner_op == op {
list.append(inner_list);
list.extend(arena, take(inner_list));
} else {
list.push(v);
list.push(arena, v);
}
} else {
list.push(v);
list.push(arena, v);
}
}
self.update_total_nodes();
......@@ -170,12 +177,12 @@ impl JsValue {
}
/// 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| {
child.normalize();
child.normalize(arena);
true
});
self.normalize_shallow();
self.normalize_shallow(arena);
}
}
......
......@@ -3,8 +3,8 @@ use std::hash::Hash;
use crate::analyzer::{CallList, JsValue, MemberCallList, ModuleValue, ObjectPart};
// Like equality, but with depth limit
impl JsValue {
pub(super) fn all_similar(a: &[JsValue], b: &[JsValue], depth: usize) -> bool {
impl<'a> JsValue<'a> {
pub(super) fn all_similar(a: &[JsValue<'a>], b: &[JsValue<'a>], depth: usize) -> bool {
if a.len() != b.len() {
return false;
}
......@@ -12,12 +12,12 @@ impl JsValue {
}
/// 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.
fn similar(&self, other: &JsValue, depth: usize) -> bool {
fn similar(&self, other: &JsValue<'a>, depth: usize) -> bool {
if depth == 0 {
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() {
return false;
}
......@@ -134,14 +134,18 @@ impl JsValue {
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 {
item.similar_hash(state, depth);
}
}
fn all_parts_similar_hash<H: std::hash::Hasher>(
slice: &[ObjectPart],
slice: &[ObjectPart<'_>],
state: &mut H,
depth: usize,
) {
......@@ -178,12 +182,12 @@ impl JsValue {
| JsValue::Logical(_, _, v) => all_similar_hash(v, state, depth - 1),
JsValue::Not(_, v) => v.similar_hash(state, depth - 1),
JsValue::New(_, call) => {
call.for_each_children(&mut |child: &JsValue| {
call.for_each_children(&mut |child: &JsValue<'_>| {
child.similar_hash(state, depth - 1);
});
}
JsValue::Call(_, call) => {
call.for_each_children(&mut |child: &JsValue| {
call.for_each_children(&mut |child: &JsValue<'_>| {
child.similar_hash(state, depth - 1);
});
}
......@@ -191,7 +195,7 @@ impl JsValue {
all_similar_hash(args, state, depth - 1);
}
JsValue::MemberCall(_, call) => {
call.for_each_children(&mut |child: &JsValue| {
call.for_each_children(&mut |child: &JsValue<'_>| {
child.similar_hash(state, depth - 1);
});
}
......@@ -249,17 +253,17 @@ const SIMILAR_HASH_DEPTH: usize = 2;
/// A wrapper around `JsValue` that implements `PartialEq` and `Hash` by
/// comparing the values with a depth of [SIMILAR_EQ_DEPTH] and hashing values
/// 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 {
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) {
self.0.similar_hash(state, SIMILAR_HASH_DEPTH)
}
......
use crate::analyzer::{JsValue, ObjectPart};
// Visiting
impl JsValue {
impl<'a> JsValue<'a> {
/// Calls a function for each child of the node. Allows mutating the node.
/// Updates the total nodes count after mutation.
pub fn for_each_children_mut(
&mut self,
visitor: &mut impl FnMut(&mut JsValue) -> bool,
visitor: &mut impl FnMut(&mut JsValue<'a>) -> bool,
) -> bool {
match self {
JsValue::Alternatives {
......@@ -158,7 +158,7 @@ impl JsValue {
/// node. Updates the total nodes count after mutation.
pub fn for_each_early_children_mut(
&mut self,
visitor: &mut impl FnMut(&mut JsValue) -> bool,
visitor: &mut impl FnMut(&mut JsValue<'a>) -> bool,
) -> bool {
match self {
JsValue::New(_, call) if !call.args().is_empty() => {
......@@ -199,7 +199,7 @@ impl JsValue {
/// node. Updates the total nodes count after mutation.
pub fn for_each_late_children_mut(
&mut self,
visitor: &mut impl FnMut(&mut JsValue) -> bool,
visitor: &mut impl FnMut(&mut JsValue<'a>) -> bool,
) -> bool {
match self {
JsValue::New(_, call) if !call.args().is_empty() => {
......@@ -250,13 +250,13 @@ impl JsValue {
}
/// 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));
visitor(self);
}
/// 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 {
JsValue::Alternatives {
total_nodes: _,
......
......@@ -6,24 +6,27 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::ecma::ast::Id;
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>(
graph: &VarGraph,
mut val: JsValue,
early_visitor: &B,
visitor: &F,
fun_args_values: &Mutex<FxHashMap<u32, Vec<JsValue>>>,
var_cache: &Mutex<FxHashMap<Id, JsValue>>,
) -> Result<(JsValue, u32)>
pub async fn link<'a, 'l, B, RB, F, RF>(
arena: &'a ThreadLocal<Bump>,
graph: &'l VarGraph<'a>,
mut val: JsValue<'a>,
early_visitor: &'l B,
visitor: &'l F,
fun_args_values: &Mutex<FxHashMap<u32, BumpVec<'a, JsValue<'a>>>>,
var_cache: &Mutex<FxHashMap<Id, JsValue<'a>>>,
) -> Result<(JsValue<'a>, u32)>
where
RB: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
B: 'a + Fn(JsValue) -> RB + Sync,
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync,
RB: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
B: 'l + Fn(JsValue<'a>) -> RB + Sync,
RF: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
F: 'l + Fn(JsValue<'a>) -> RF + Sync,
{
val.normalize();
val.normalize(arena.get_or_default());
let (val, steps) = link_internal_iterative(
arena,
graph,
val,
early_visitor,
......@@ -39,19 +42,19 @@ const LIMIT_NODE_SIZE: u32 = 100;
const LIMIT_IN_PROGRESS_NODES: u32 = 500;
const LIMIT_LINK_STEPS: u32 = 1500;
#[derive(Debug, Clone, PartialEq)]
enum Step {
#[derive(Debug, PartialEq)]
enum Step<'a> {
/// Take all children out of the value (replacing temporarily with unknown) and queue them
/// 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
Leave(JsValue),
Leave(JsValue<'a>),
/// Remove the variable from `cycle_stack` which detects e.g. circular reassignments
LeaveVar(Id),
LeaveLate(JsValue),
LeaveLate(JsValue<'a>),
/// Call the visitor callbacks, and requeue the value for further processing if it changed.
Visit(JsValue),
EarlyVisit(JsValue),
Visit(JsValue<'a>),
EarlyVisit(JsValue<'a>),
/// Remove the call from `fun_args_values`
LeaveCall(u32),
/// Placeholder that is used to momentarily reserve a slot that is only filled after
......@@ -59,7 +62,7 @@ enum Step {
TemporarySlot,
}
impl Display for Step {
impl Display for Step<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Step::Enter(val) => write!(f, "Enter({val})"),
......@@ -75,22 +78,23 @@ impl Display for Step {
}
// 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>(
graph: &'a VarGraph,
val: JsValue,
early_visitor: &'a B,
visitor: &'a F,
fun_args_values: &Mutex<FxHashMap<u32, Vec<JsValue>>>,
var_cache: &Mutex<FxHashMap<Id, JsValue>>,
) -> Result<(JsValue, u32)>
pub(crate) async fn link_internal_iterative<'a, 'l, B, RB, F, RF>(
arena: &'a ThreadLocal<Bump>,
graph: &'l VarGraph<'a>,
val: JsValue<'a>,
early_visitor: &'l B,
visitor: &'l F,
fun_args_values: &Mutex<FxHashMap<u32, BumpVec<'a, JsValue<'a>>>>,
var_cache: &Mutex<FxHashMap<Id, JsValue<'a>>>,
) -> Result<(JsValue<'a>, u32)>
where
RB: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
B: 'a + Fn(JsValue) -> RB + Sync,
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync,
RB: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
B: 'l + Fn(JsValue<'a>) -> RB + Sync,
RF: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
F: 'l + Fn(JsValue<'a>) -> RF + Sync,
{
let mut work_queue_stack: Vec<Step> = Vec::new();
let mut done: Vec<JsValue> = Vec::new();
let mut work_queue_stack: Vec<Step<'a>> = Vec::new();
let mut done: Vec<JsValue<'a>> = Vec::new();
// Tracks the number of nodes in the queue and done combined
let mut total_nodes = 0;
let mut cycle_stack: FxHashSet<Id> = FxHashSet::default();
......@@ -122,12 +126,12 @@ where
.then(|| var_cache.lock());
if let Some(val) = var_cache_lock.as_deref().and_then(|cache| cache.get(&var)) {
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) {
cycle_stack.insert(var.clone());
work_queue_stack.push(Step::LeaveVar(var));
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 {
total_nodes += 1;
done.push(JsValue::unknown(
......@@ -142,7 +146,9 @@ where
Step::LeaveVar(var) => {
cycle_stack.remove(&var);
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
......@@ -152,7 +158,7 @@ where
if let Some(args) = fun_args_values.lock().get(&func_ident) {
if let Some(val) = args.get(index) {
total_nodes += val.total_nodes();
done.push(val.clone());
done.push(val.clone_in(arena.get_or_default()));
} else {
total_nodes += 1;
done.push(JsValue::unknown_empty(
......@@ -176,7 +182,7 @@ where
if matches!(call.callee(), JsValue::Function(..)) =>
{
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!()
};
total_nodes -= 2; // Call + Function
......@@ -187,7 +193,7 @@ where
}
entry.insert(args);
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 {
total_nodes -= return_value.total_nodes();
for arg in args.iter() {
......@@ -196,6 +202,7 @@ where
total_nodes += 1;
done.push(JsValue::unknown(
JsValue::call_from_parts(
arena.get_or_default(),
JsValue::Function(function_nodes, func_ident, return_value),
args,
),
......@@ -222,6 +229,7 @@ where
Step::Enter(mut val) => {
if !val.has_children() {
visit(
arena,
&mut total_nodes,
&mut done,
&mut work_queue_stack,
......@@ -318,7 +326,7 @@ where
done.push(JsValue::unknown_empty(true, rcstr!("node limit reached")));
continue;
}
val.normalize_shallow();
val.normalize_shallow(arena.get_or_default());
val.debug_assert_total_nodes_up_to_date();
......@@ -341,7 +349,7 @@ where
done.push(JsValue::unknown_empty(true, rcstr!("node limit reached")));
continue;
}
val.normalize_shallow();
val.normalize_shallow(arena.get_or_default());
val.debug_assert_total_nodes_up_to_date();
......@@ -352,6 +360,7 @@ where
// - visited value is put into done
Step::Visit(val) => {
visit(
arena,
&mut total_nodes,
&mut done,
&mut work_queue_stack,
......@@ -402,22 +411,23 @@ where
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,
done: &mut Vec<JsValue>,
work_queue_stack: &mut Vec<Step>,
visitor: &'a F,
val: JsValue,
done: &mut Vec<JsValue<'a>>,
work_queue_stack: &mut Vec<Step<'a>>,
visitor: &'l F,
val: JsValue<'a>,
) -> Result<()>
where
RF: 'a + Future<Output = Result<(JsValue, bool)>> + Send,
F: 'a + Fn(JsValue) -> RF + Sync,
RF: 'l + Future<Output = Result<(JsValue<'a>, bool)>> + Send,
F: 'l + Fn(JsValue<'a>) -> RF + Sync,
{
*total_nodes -= val.total_nodes();
let (mut val, visit_modified) = visitor(val).await?;
if visit_modified {
val.normalize_shallow();
val.normalize_shallow(arena.get_or_default());
#[cfg(debug_assertions)]
val.debug_assert_total_nodes_up_to_date();
if val.total_nodes() > LIMIT_NODE_SIZE {
......
......@@ -143,7 +143,7 @@ impl WellKnownObjectKind {
/// A list of well-known functions that have special meaning in the analysis.
#[derive(Debug, Clone, Hash, PartialEq)]
pub enum WellKnownFunctionKind {
pub enum WellKnownFunctionKind<'a> {
ArrayFilter,
ArrayForEach,
ArrayMap,
......@@ -151,7 +151,7 @@ pub enum WellKnownFunctionKind {
PathJoin,
PathDirname,
/// `0` is the current working directory.
PathResolve(Box<JsValue>),
PathResolve(&'a JsValue<'a>),
Import,
Require,
/// `0` is the path to resolve from (relative to the current module).
......@@ -196,7 +196,7 @@ pub enum WellKnownFunctionKind {
ImportMetaGlob,
}
impl WellKnownFunctionKind {
impl WellKnownFunctionKind<'_> {
pub fn as_define_name(&self) -> Option<&[&str]> {
match self {
Self::Import { .. } => Some(&["import"]),
......
......@@ -23,7 +23,7 @@ pub struct RequireContextOptions {
/// Parse the arguments passed to a require.context invocation, validate them
/// 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()) {
// 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)");
......
......@@ -88,7 +88,7 @@ pub struct ImportMetaGlobOptions {
/// - **`as` option** (deprecated in Vite 5 in favor of `query`) is not supported. Use `query:
/// '?raw'` or `query: '?url'` instead.
pub fn parse_import_meta_glob(
args: &[JsValue],
args: &[JsValue<'_>],
handler: &Handler,
span: Span,
diagnostic_id: DiagnosticId,
......
......@@ -29,7 +29,7 @@ pub fn unparen(expr: &Expr) -> &Expr {
}
/// 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 {
JsValue::Constant(v) => Pattern::Constant(match v {
ConstantValue::Str(str) => {
......@@ -198,7 +198,7 @@ pub enum AstPathRange {
/// Converts a module value (ie an import) to a well known object,
/// 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() {
b"node:path" | b"path" => JsValue::WellKnownObject(WellKnownObjectKind::PathModule),
b"node:fs/promises" | b"fs/promises" => {
......@@ -300,12 +300,13 @@ mod tests {
use turbopack_core::resolve::pattern::Pattern;
use crate::{
analyzer::{ConstantString, ConstantValue, JsValue},
analyzer::{BumpVec, ConstantString, ConstantValue, JsValue, ThreadLocal},
utils::js_value_to_pattern,
};
#[test]
fn test_path_normalization_in_pattern() {
let arena = ThreadLocal::new();
assert_eq!(
Pattern::Constant(rcstr!("hello/world")),
js_value_to_pattern(&JsValue::Constant(ConstantValue::Str(
......@@ -317,11 +318,14 @@ mod tests {
Pattern::Constant(rcstr!("hello/world")),
js_value_to_pattern(&JsValue::Concat(
1,
vec![
rcstr!("hello").into(),
rcstr!("\\").into(),
rcstr!("world").into()
]
BumpVec::from_iter_in(
arena.get_or_default(),
[
rcstr!("hello").into(),
rcstr!("\\").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