⚡️ Speed up function format_tcp_address by 110%
#457
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.
📄 110% (1.10x) speedup for
format_tcp_addressinpython/sglang/srt/utils/common.py⏱️ Runtime :
21.2 milliseconds→10.1 milliseconds(best of114runs)📝 Explanation and details
The optimization introduces caching of IPv6 address validation using
@lru_cache(maxsize=4096)on a new helper function_cached_is_valid_ipv6_address. This delivers a 110% speedup by eliminating redundant IPv6 validation operations.Key optimization:
is_valid_ipv6_address()with a cached version that stores validation results for up to 4,096 unique IP addressesipaddress.IPv6Address()validation (same logic as the original dependency)Why this is faster:
The line profiler shows the IPv6 validation (
is_valid_ipv6_address) was the dominant bottleneck, consuming 97.4% of execution time inmaybe_wrap_ipv6_address. IPv6 address parsing involves complex string validation and formatting checks. With caching, subsequent calls to validate the same address become simple dictionary lookups instead of full parsing operations.Performance impact by workload:
Hot path impact:
Based on function references,
format_tcp_addressis called in network connection establishment (_bind_server_socket,_connect_to_bootstrap_server) and data transmission paths (send_aux_data_to_endpoint). These are critical performance paths where the same IP addresses are frequently reused across connections, making caching highly beneficial.The optimization is particularly effective for distributed systems scenarios where a limited set of node IP addresses are repeatedly validated during cluster communication.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-format_tcp_address-mijlp2sgand push.