From a54f33e0972143dc936626b1bfc5491a0b11391d Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Sun, 9 Feb 2025 14:04:43 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`v?= =?UTF-8?q?alidate=5Fregex`=20by=202,003%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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. --- json2nginx.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/json2nginx.py b/json2nginx.py index 83a4d36..4d015c2 100644 --- a/json2nginx.py +++ b/json2nginx.py @@ -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: