️ Speed up function validate_regex by 9,172%

Changes made for optimization.
1. Added `functools.lru_cache` decorator to cache results of `validate_regex` function calls. This ensures that repeated validations of the same pattern are resolved quickly and avoid redundant regex compilations.
This commit is contained in:
codeflash-ai[bot] 2025-02-09 12:07:05 +00:00 committed by GitHub
parent 1a98411808
commit a3104b23c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import re
import logging import logging
from pathlib import Path from pathlib import Path
from typing import List, Dict, Optional from typing import List, Dict, Optional
from functools import lru_cache
# Configure logging # Configure logging
logging.basicConfig( logging.basicConfig(
@ -46,6 +47,7 @@ def load_owasp_rules(file_path: Path) -> List[Dict]:
logging.error(f"[!] Error loading OWASP rules: {e}") logging.error(f"[!] Error loading OWASP rules: {e}")
raise raise
@lru_cache(maxsize=None)
def validate_regex(pattern: str) -> bool: def validate_regex(pattern: str) -> bool:
""" """
Validate regex pattern for HAProxy. Validate regex pattern for HAProxy.