⚡️ Speed up function is_remote_url by 103%
#459
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.
📄 103% (1.03x) speedup for
is_remote_urlinpython/sglang/srt/utils/common.py⏱️ Runtime :
2.87 milliseconds→1.42 milliseconds(best of44runs)📝 Explanation and details
The optimization precompiles the regex pattern as a module-level constant
_REMOTE_URL_PATTERNinstead of recreating it on every function call. This eliminates the expensive regex compilation overhead that was consuming 66.7% of the original function's runtime.Key changes:
r"(.+)://(.*)"tor".+://.*"since capture groups aren't usedre.match()with the precompiled pattern's.match()methodWhy this is faster:
In Python,
re.match()compiles the pattern every time it's called. The line profiler shows this compilation step took 6.59ms out of 9.87ms total runtime (66.7%). By precompiling, we eliminate this per-call overhead, reducing total function time from 9.87ms to 3.38ms - a 102% speedup.Impact on workloads:
The function references show
is_remote_url()is called during model loading and server argument handling - critical initialization paths where this optimization provides meaningful speedup. The annotated tests demonstrate consistent 70-300% performance improvements across all URL types, with the largest gains on complex URLs and batch processing scenarios.Test case performance:
This optimization is particularly valuable for applications that validate many URLs during startup or configuration parsing.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_remote_url-mijn71vland push.