Skip to content

chore: lazy-compile validation regexes via std::sync::LazyLock - #830

Merged
manan19 merged 1 commit into
mainfrom
chore/lazy-regex-validations
Apr 27, 2026
Merged

chore: lazy-compile validation regexes via std::sync::LazyLock#830
manan19 merged 1 commit into
mainfrom
chore/lazy-regex-validations

Conversation

@manan19

@manan19 manan19 commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Summary

The 4 constant-source regexes in core/validations/message.rs (FNAME_REGEX, TWITTER_USERNAME_REGEX, GITHUB_USERNAME_REGEX, geo) were being recompiled with fancy_regex::Regex::new(...) on every call to validate_fname, validate_ens_name, validate_base_name, validate_twitter_username, validate_github_username, and validate_user_location. Each is hot-path validation code.

Moves them to module-level static LazyLock<Regex> so each compiles exactly once. The CAIP-19 regex is dynamically constructed from a local variable (depends on namespace input), so it's left as-is.

Aligns with the intent of #576; uses stable std::sync::LazyLock (stabilized in Rust 1.80) instead of lazy_static! or once_cell. Split out from #796 per review feedback.

Test plan

  • All 78 existing validation tests pass

🤖 Generated with Claude Code

The 4 constant-source regexes in core/validations/message.rs (FNAME,
TWITTER_USERNAME, GITHUB_USERNAME, geo) were being recompiled with
fancy_regex::Regex::new(...) on every call to validate_fname,
validate_ens_name, validate_base_name, validate_twitter_username,
validate_github_username, and validate_user_location.

Move them to module-level static LazyLock<Regex> so each compiles
exactly once. The CAIP-19 regex is dynamically constructed from a
local variable and is left as-is.

Aligns with the intent of #576; uses stable std::sync::LazyLock
(stabilized in Rust 1.80) instead of lazy_static!. Split out from #796
per review feedback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 24, 2026 23:33
@vercel

vercel Bot commented Apr 24, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
snapchain-docs Ready Ready Preview, Comment Apr 24, 2026 11:33pm

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves performance in hot-path message validation by compiling several constant regex patterns once at module initialization time (lazy, on first use) instead of recompiling them on every validation call.

Changes:

  • Introduces module-level static LazyLock<Regex> instances for fname, Twitter username, GitHub username, and geo location regexes.
  • Updates the corresponding validation functions to reuse the cached compiled regexes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • src/core/validations/message.rs (40.0%): Missing lines 30,32,34,397,415,543

Summary

  • Total: 10 lines
  • Missing: 6 lines
  • Coverage: 40%

src/core/validations/message.rs

  26 const GITHUB_USERNAME_REGEX: &str = "^[a-zA-Z\\d](?:[a-zA-Z\\d]|-(?!-)){0,38}$";
  27 
  28 static FNAME_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(FNAME_REGEX).unwrap());
  29 static TWITTER_USERNAME_RE: LazyLock<Regex> =
! 30     LazyLock::new(|| Regex::new(TWITTER_USERNAME_REGEX).unwrap());
  31 static GITHUB_USERNAME_RE: LazyLock<Regex> =
! 32     LazyLock::new(|| Regex::new(GITHUB_USERNAME_REGEX).unwrap());
  33 static GEO_RE: LazyLock<Regex> =
! 34     LazyLock::new(|| Regex::new(r"^geo:(-?\d{1,2}\.\d{2}),(-?\d{1,3}\.\d{2})$").unwrap());
  35 
  36 /** Number of seconds (10 minutes) that is appropriate for clock skew */
  37 const ALLOWED_CLOCK_SKEW_SECONDS: u64 = 10 * 60;

  393     if input.len() > 15 {
  394         return Err(ValidationError::UsernameExceedsLength(input.clone(), 15));
  395     }
  396 
! 397     if !TWITTER_USERNAME_RE
  398         .is_match(&input)
  399         .map_err(|_| ValidationError::InvalidData)?
  400     {
  401         return Err(ValidationError::UsernameDoesntMatch(

  411     if input.len() > 38 {
  412         return Err(ValidationError::UsernameExceedsLength(input.clone(), 38));
  413     }
  414 
! 415     if !GITHUB_USERNAME_RE
  416         .is_match(&input)
  417         .map_err(|_| ValidationError::InvalidData)?
  418     {
  419         return Err(ValidationError::UsernameDoesntMatch(

  539     if location.is_empty() {
  540         return Ok(());
  541     }
  542 
! 543     let captures = GEO_RE
  544         .captures(location)
  545         .map_err(|_| ValidationError::InvalidLocationString)?;
  546 
  547     if captures.is_none() {

@manan19
manan19 requested a review from topocount April 25, 2026 00:18
@manan19
manan19 merged commit e50132d into main Apr 27, 2026
18 checks passed
@manan19
manan19 deleted the chore/lazy-regex-validations branch April 27, 2026 18:15
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.

3 participants