mirror of
https://github.com/fabriziosalmi/patterns.git
synced 2025-12-17 17:55:48 +00:00
⚡️ Speed up function validate_regex by 2,003%
### Explanation. 1. **Caching with lru_cache**. - By using `functools.lru_cache`, the function `validate_regex` now caches the results of previous calls. If the same pattern is validated multiple times, the cached result is returned immediately, significantly improving the performance for repeated patterns. This change optimizes the runtime without altering the function's behavior.
This commit is contained in:
parent
1a4a2d4e42
commit
a54f33e097
@ -4,6 +4,7 @@ import re
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
from functools import lru_cache
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
@ -36,6 +37,7 @@ def load_owasp_rules(file_path):
|
||||
raise
|
||||
|
||||
|
||||
@lru_cache(maxsize=None)
|
||||
def validate_regex(pattern):
|
||||
"""Validate if a pattern is a valid regex."""
|
||||
try:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user