Skip to content

Commit

Permalink
perf(turbopack): Box ConstantValue::Regex (#74968)
Browse files Browse the repository at this point in the history
### What?

Box the `Regex` variant of `ConstantValue`

### Why?

Data-oriented Design!

This variant is not created frequently compared to other variants, so we can box it to reduce the size of the type.
  • Loading branch information
kdy1 authored Jan 16, 2025
1 parent 2398bc6 commit 30b4a11
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub enum ConstantValue {
False,
Null,
BigInt(Box<BigInt>),
Regex(Atom, Atom),
Regex(Box<(Atom, Atom)>),
}

impl ConstantValue {
Expand Down Expand Up @@ -252,7 +252,7 @@ impl From<Lit> for ConstantValue {
Lit::Null(_) => ConstantValue::Null,
Lit::Num(v) => ConstantValue::Num(ConstantNumber(v.value)),
Lit::BigInt(v) => ConstantValue::BigInt(v.value),
Lit::Regex(v) => ConstantValue::Regex(v.exp, v.flags),
Lit::Regex(v) => ConstantValue::Regex(Box::new((v.exp, v.flags))),
Lit::JSXText(v) => ConstantValue::Str(ConstantString::Atom(v.value)),
}
}
Expand All @@ -268,7 +268,7 @@ impl Display for ConstantValue {
ConstantValue::Null => write!(f, "null"),
ConstantValue::Num(ConstantNumber(n)) => write!(f, "{n}"),
ConstantValue::BigInt(n) => write!(f, "{n}"),
ConstantValue::Regex(exp, flags) => write!(f, "/{exp}/{flags}"),
ConstantValue::Regex(regex) => write!(f, "/{}/{}", regex.0, regex.1),
}
}
}
Expand Down Expand Up @@ -3732,7 +3732,7 @@ pub fn parse_require_context(args: &[JsValue]) -> Result<RequireContextOptions>
};

let filter = if let Some(filter) = args.get(2) {
if let JsValue::Constant(ConstantValue::Regex(pattern, flags)) = filter {
if let JsValue::Constant(ConstantValue::Regex(box (pattern, flags))) = filter {
regex_from_js(pattern, flags)?
} else {
bail!("require.context(..., ..., filter) requires filter to be a regex");
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-ecmascript/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn js_value_to_pattern(value: &JsValue) -> Pattern {
ConstantValue::Null => "null".into(),
ConstantValue::Num(ConstantNumber(n)) => n.to_string().into(),
ConstantValue::BigInt(n) => n.to_string().into(),
ConstantValue::Regex(exp, flags) => format!("/{exp}/{flags}").into(),
ConstantValue::Regex(box (exp, flags)) => format!("/{exp}/{flags}").into(),
ConstantValue::Undefined => "undefined".into(),
}),
JsValue::Url(v, JsValueUrlKind::Relative) => Pattern::Constant(v.as_str().into()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@
Value(
Constant(
Regex(
"\\.test\\.js$",
"",
(
"\\.test\\.js$",
"",
),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
),
Constant(
Regex(
"\\.test\\.js$",
"",
(
"\\.test\\.js$",
"",
),
),
),
],
Expand Down

0 comments on commit 30b4a11

Please sign in to comment.