⚡️ Speed up function get_local_ip_auto by 45%
#462
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.
📄 45% (0.45x) speedup for
get_local_ip_autoinpython/sglang/srt/utils/common.py⏱️ Runtime :
71.4 microseconds→49.2 microseconds(best of40runs)📝 Explanation and details
The optimized version achieves a 44% speedup through three key micro-optimizations:
1. Environment Variable Access Optimization
os.getenv("SGLANG_HOST_IP", "") or os.getenv("HOST_IP", "")to separateos.environ.get()calls with explicit None checkingos.getenv's default parameter handling and the unnecessary second call when the first succeeds2. Dictionary Access Pattern Improvements
get_local_ip_by_nic(), replacednetifaces.AF_INET in addresseschecks withaddresses.get(netifaces.AF_INET)to eliminate redundant hash table lookups3. Socket Resource Management
withstatements) for socket creation inget_local_ip_by_remote()Impact on Workloads:
Based on the function references,
get_local_ip_auto()is called during initialization of disaggregation engines and connection managers. While not in tight loops, this function is called during critical startup paths where every microsecond matters for distributed ML workloads. The test results show consistent 10-30% improvements across various environment configurations, making this particularly beneficial for containerized deployments where environment variable lookups are frequent.The optimizations are especially effective for cases with environment variables set (which are the most common in production), showing 20-30% improvements in those scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_local_ip_auto-mijpjwzrand push.