mirror of
https://github.com/fabriziosalmi/patterns.git
synced 2026-04-19 21:38:18 -04:00
Here is an optimized version of the provided Python program. The optimizations focus on improving the I/O operations, avoiding unnecessary checks, and caching the regex pattern validation. ### Explanation of Optimizations. 1. **Caching with `@lru_cache`**: The `validate_regex` function is wrapped with `@lru_cache` to cache the results of previously validated regex patterns. This prevents repeated compilation of the same regex patterns. 2. **Reading the JSON file**: I/O operations were optimized by using the `with` statement to handle file reading and writing. 3. **Avoiding repeated checks**: The unsupported patterns are checked just once per pattern, eliminating redundant operations. 4. **Batch writing**: All rules are collected in a list and written to the output file in a single operation, reducing the overhead of multiple write operations.
6.4 KiB
6.4 KiB