Skip to content

feat(turbopack): turbo-esregex support captures method #81119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

fireairforce
Copy link
Contributor

EsRegex support captures method, in utoo situation, we need Esregexp to support captures the match string from user's regex config item.

@ijjk ijjk added the Turbopack Related to Turbopack with Next.js. label Jul 1, 2025
@ijjk
Copy link
Member

ijjk commented Jul 1, 2025

Allow CI Workflow Run

  • approve CI run for commit: a0bd7c0

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

2 similar comments
@ijjk
Copy link
Member

ijjk commented Jul 1, 2025

Allow CI Workflow Run

  • approve CI run for commit: a0bd7c0

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@ijjk
Copy link
Member

ijjk commented Jul 1, 2025

Allow CI Workflow Run

  • approve CI run for commit: a0bd7c0

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Copy link

codspeed-hq bot commented Jul 1, 2025

CodSpeed Performance Report

Merging #81119 will not alter performance

Comparing fireairforce:turbo-regexp-support-match (a0bd7c0) with canary (ce55374)

Summary

✅ 3 untouched benchmarks
⁉️ 9 dropped benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
⁉️ build[date-fns-all] 2.6 s N/A N/A
⁉️ build[date-fns-single] 1.4 s N/A N/A
⁉️ build[framer-motion-all] 3.5 s N/A N/A
⁉️ build[framer-motion-single] 2.3 s N/A N/A
⁉️ build[joy] 2.3 s N/A N/A
⁉️ build[lucide-react-all] 11.4 s N/A N/A
⁉️ build[lucide-react-single] 922.7 ms N/A N/A
⁉️ build[mui] 3.4 s N/A N/A
⁉️ build[shiki] 6.3 s N/A N/A
@mischnic mischnic requested a review from lukesandberg July 1, 2025 13:05
match &self.delegate {
EsRegexImpl::Regex(r) => r.captures(haystack).map(|caps| {
caps.iter()
.map(|m| m.map(|m| m.as_str().to_string()).unwrap_or_default())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can avoid the .to_string() conversion and make the return type of this function Option<Vec<&'h str>> (where 'h is the lifetime of haystack). It seems like both regex and regress support such an API. The captures are just slices of the haystack.

EsRegexImpl::Regex(r) => r.captures(haystack).map(|caps| {
caps.iter()
.map(|m| m.map(|m| m.as_str().to_string()).unwrap_or_default())
.collect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Instead of collect()ing here, it might be neat to try to make the return type an impl Iterator:

pub fn captures<'h>(&self, haystack: &'h str) -> Option<impl Iterator<&'h str>>

The iterator types for regex and regress are different, but we often use the either crate to work around this:

Comment on lines +142 to +145
let group_result =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
m.group(group_index)
}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We absolutely cannot do this:

  • Panic handling in Rust is very slow, and should only be used for codepaths you expect to never be hit. This is especially true for library code (which this is).
  • Panics (even if caught) trigger the global panic hooks, which we use in Next.js for things like cache invalidation and anonymized telemetry.

It looks like regress::Match has a groups function that returns an iterator: https://docs.rs/regress/latest/regress/struct.Match.html#method.groups

Could you use that?

Copy link
Member

@bgw bgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see inline comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Turbopack Related to Turbopack with Next.js.
3 participants