⚡️ Speed up function parse_connector_type by 77%
#460
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.
📄 77% (0.77x) speedup for
parse_connector_typeinpython/sglang/srt/utils/common.py⏱️ Runtime :
2.97 milliseconds→1.68 milliseconds(best of169runs)📝 Explanation and details
The optimization replaces inline regex compilation with a pre-compiled regex pattern, achieving a 76% speedup by eliminating repetitive pattern compilation overhead.
Key optimization: The regex pattern
r"(.+)://(.*)"is compiled once at module load time as_connector_patternand reused across all function calls, instead of recompiling it on every invocation.Why this works: Python's
re.compile()creates an optimized pattern object that can be reused efficiently. The original code was recompiling the same pattern on every call (74.2% of total time spent inre.match()), which involves parsing the regex syntax and building internal state machines. The optimized version eliminates this overhead entirely.Performance impact by workload:
create_remote_connector()for URL parsing and_handle_model_specific_adjustments()for model path validation. These are likely called during server startup and model loading phases.The optimization is particularly effective for this use case because URL parsing typically involves many calls with the same simple pattern, making regex compilation the primary bottleneck.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-parse_connector_type-mijnd2faand push.