⚡️ Speed up function _is_valid_proxy_url by 68%
#147
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 68% (0.68x) speedup for
_is_valid_proxy_urlinskyvern/webeye/browser_factory.py⏱️ Runtime :
6.98 milliseconds→4.16 milliseconds(best of195runs)📝 Explanation and details
The optimization achieves a 67% speedup through two key changes:
Regex Compilation Hoisting: Moving
PROXY_PATTERNcompilation outside the function eliminates repeated compilation overhead. The original code recompiled the regex on every call (11% of runtime), while the optimized version compiles it once at import time.Early Rejection with Regex First: The optimized version checks the regex pattern before calling
urlparse(). Sinceurlparse()consumed 82.3% of the original runtime, this reordering provides massive gains for invalid URLs. The regex can quickly reject malformed URLs without the expensive parsing step.Performance Impact by Test Case:
urlparse()but benefit from eliminated regex recompilationReal-World Impact: Based on the function reference,
_is_valid_proxy_urlis called insetup_proxy()to validate proxy URLs from configuration. When proxy pools contain invalid entries (common in real deployments), this optimization will significantly reduce startup time and configuration validation overhead. The function processes each proxy in a list, so the performance gains compound with larger proxy pools or frequent proxy validation.The optimization maintains identical behavior while dramatically improving performance for the common case of rejecting invalid proxy URLs.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_is_valid_proxy_url-mjai3ldaand push.