perf(regular_expression): avoid rescanning unique capture names (#28450)

Check the capture-name set before scanning prior groups for
duplicate-name conflicts, avoiding repeated scans for unique names.

Validated with scoped Rust checks, full conformance, parser bindings,
the linter suite.

AI-assisted (Codex).
This commit is contained in:
Boshen authored and GitHub committed 2026-10-30 11:37:55 +08:00
1 parent cb64638ed4
commit 550e98a43b
1 file changed
+13 -12
@@ -158,16 +158,19 @@ fn parse_capturing_groups<'a>(
};
let alternative_path = tracker.get_alternative_path();
// Check for duplicates with existing groups
for existing in &named_groups {
if existing.name == group_name {
// Check if they can participate together
if AlternativeTracker::can_participate(
&existing.alternative_path,
&alternative_path,
) {
// True duplicate - return error
return Err(vec![existing.span, (span_start, span_end)]);
// A newly seen name cannot be a duplicate. Avoid scanning all prior
// groups, which would be quadratic for many distinct names.
if !group_names.insert(group_name) {
for existing in &named_groups {
if existing.name == group_name {
// Check if they can participate together
if AlternativeTracker::can_participate(
&existing.alternative_path,
&alternative_path,
) {
// True duplicate - return error
return Err(vec![existing.span, (span_start, span_end)]);
}
}
}
}
@@ -177,8 +180,6 @@ fn parse_capturing_groups<'a>(
span: (span_start, span_end),
alternative_path,
});
group_names.insert(group_name);
continue;
}
}