Skip to content

⚡ Optimize RegexRule compilation#102

Open
Tuntii wants to merge 1 commit intomainfrom
perf/optimize-regex-rule-compilation-3847355307570598131
Open

⚡ Optimize RegexRule compilation#102
Tuntii wants to merge 1 commit intomainfrom
perf/optimize-regex-rule-compilation-3847355307570598131

Conversation

@Tuntii
Copy link
Owner

@Tuntii Tuntii commented Feb 6, 2026

Updated RegexRule to cache the result of regex compilation (including errors) using OnceLock<Result<Regex, String>>. Previously, it only cached the success case and re-compiled the regex on every validation call to check for errors.

The redundant regex compilation caused significant overhead. The optimization eliminates the expensive Regex::new parsing and compilation step from the hot path of validation.

Benchmark showed a massive performance improvement (from ~1.58ms to ~2.45µs per iteration).


PR created automatically by Jules for task 3847355307570598131 started by @Tuntii

Updated `RegexRule` to cache the result of regex compilation (including errors) using `OnceLock<Result<Regex, String>>`. This eliminates redundant `Regex::new` calls on every validation.

Benchmark results:
- Baseline: ~1.58ms per iteration
- Optimized: ~2.45µs per iteration
- Speedup: ~645x

Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 6, 2026 03:44
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the RegexRule validation by caching both successful and failed regex compilation results. Previously, the code cached only successful compilations (with a fallback pattern) and re-compiled the regex on every validation call to check for errors, causing significant overhead.

Changes:

  • Modified RegexRule.compiled field type from OnceLock<Regex> to OnceLock<Result<Regex, String>> to cache both success and error cases
  • Refactored get_regex() method to properly handle and cache compilation errors without redundant regex compilation

Comment on lines 311 to 321
fn get_regex(&self) -> Result<&Regex, RuleError> {
self.compiled.get_or_init(|| {
Regex::new(&self.pattern).unwrap_or_else(|_| Regex::new("^$").unwrap())
let result = self.compiled.get_or_init(|| {
Regex::new(&self.pattern)
.map_err(|_| format!("Invalid regex pattern: {}", self.pattern))
});

// Verify the pattern is valid
if Regex::new(&self.pattern).is_err() {
return Err(RuleError::new(
"regex",
format!("Invalid regex pattern: {}", self.pattern),
));
match result {
Ok(regex) => Ok(regex),
Err(msg) => Err(RuleError::new("regex", msg.clone())),
}

Ok(self.compiled.get().unwrap())
}
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The error handling path for invalid regex patterns is not covered by tests. Consider adding a test case that validates the behavior when an invalid regex pattern is provided, such as a pattern with unbalanced brackets. This would ensure that the new error caching logic works correctly and that the error message is properly formatted and returned.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant