7 Commits

Author SHA1 Message Date
fab
676c53d383
Update json2nginx.py 2025-02-28 11:19:32 +01:00
codeflash-ai[bot]
b08285fd76
️ Speed up function sanitize_pattern by 631%
Here's an optimized version of the given Python program for better runtime efficiency.
2025-02-09 14:27:36 +00:00
fab
870da8e1c9
Merge pull request #11 from fabriziosalmi/codeflash/optimize-validate_regex-m6xp4bpf
️ Speed up function `validate_regex` by 2,003%
2025-02-09 15:14:42 +01:00
codeflash-ai[bot]
17faa1f1b9
️ Speed up function sanitize_pattern by 11,547%
I've analyzed the script provided and I'll make some optimizations to improve its runtime performance while ensuring the functionality remains the same. Let's break it down step-by-step.

### Improvements.
1. **Avoid Redundant Checks:** Optimize by eliminating unnecessary repetitive checks.
2. **Combining String Operations:** Combine string operations to minimize calls.
3. **Caching Compiled Patterns:** If re.escape or re.compile are used multiple times for the same pattern, cache the results to avoid recomputing them.

Here’s the optimized version of the script.



### Summary of changes.
1. **LRU Caching**.
   - Used `functools.lru_cache` to cache results of `_compile_pattern` and `_sanitize_pattern` for improved performance on repetitive calls.
2. **Removed Redundant Condition**.
   - Moved repeated checks and operations within a single `if` block to simplify the flow and eliminate unnecessary calls.
3. **Centralized Pattern Validation**.
   - Centralized the regex validation and escaping in `_sanitize_pattern` function to minimize redundancy.

These changes should optimize your program's performance by reducing redundant computations and leveraging caching mechanisms. The functionality remains unchanged and will return the same values as before.
2025-02-09 14:06:18 +00:00
codeflash-ai[bot]
a54f33e097
️ 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.
2025-02-09 14:04:43 +00:00
fabriziosalmi
f1bae07d6c feat: Generate Nginx WAF config with separate map and rule files
This commit modifies the script to output two files:
- waf_maps.conf (for http block)
- waf_rules.conf (for server block)
to avoid conflicts and provide more flexibility.

This update should fix the bugged nginx rules integration on existing setups: https://github.com/fabriziosalmi/patterns/issues/8
2025-01-28 22:41:54 +01:00
fabriziosalmi
1da19ed802 nginx generation improved 2025-01-16 13:49:54 +01:00