Skip to content

refactor(config): use LazyLock for static regex initialization#67

Closed
echobt wants to merge 5 commits intodocs/standardize-timeout-documentationfrom
refactor/config-regex-lazy-init
Closed

refactor(config): use LazyLock for static regex initialization#67
echobt wants to merge 5 commits intodocs/standardize-timeout-documentationfrom
refactor/config-regex-lazy-init

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Summary

This PR refactors the regex initialization in config_substitution.rs to use std::sync::LazyLock for static regex patterns instead of creating them within struct instances.

Problem

The previous implementation compiled regex patterns in the ConfigSubstitution::new() constructor using unwrap_or_else with panic:

env_regex: Regex::new(r"...").unwrap_or_else(|e| {
    panic!("Failed to compile env regex: {e}");
}),

While the regex patterns are valid, using panic as error handling is not ideal for the following reasons:

  • Panics are difficult to recover from and provide poor user experience
  • The regex compilation occurred on every struct instantiation, which is wasteful

Solution

Replaced the instance-level regex compilation with static LazyLock constants at module level:

static ENV_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\{env:([^:}]+)(?::([^}]*))?\}")
        .expect("env regex pattern is valid and tested")
});

Benefits

  1. One-time initialization: The regex patterns are compiled once on first access and shared across all instances
  2. Fail-fast behavior: Invalid patterns fail immediately on module load rather than during runtime
  3. Performance: Eliminates redundant regex compilation for multiple ConfigSubstitution instances
  4. Cleaner API: The struct maintains backward compatibility while internally using shared static patterns

Testing

All 23 existing tests pass without modification, confirming backward compatibility.

echobt added a commit that referenced this pull request Feb 4, 2026
…it, and documentation

This PR consolidates the following refactoring changes:
- #27: Centralize timeout constants
- #43: Replace magic numbers with documented named constants
- #67: Use LazyLock for static regex initialization
- #68: Extract subagent timeout values as named constants

Key changes:
- Created centralized timeout module with documented constants
- Replaced scattered magic numbers with well-documented constants
- Improved static initialization using LazyLock for regexes
- Added comprehensive documentation for timeout hierarchy
@echobt
Copy link
Contributor Author

echobt commented Feb 4, 2026

Consolidated into #77 - refactor: consolidated code quality improvements - constants, lazy init, and documentation

@echobt echobt closed this Feb 4, 2026
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