⚡️ Speed up function _colorized_url by 11%
#607
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.
📄 11% (0.11x) speedup for
_colorized_urlinmarimo/_server/print.py⏱️ Runtime :
2.02 milliseconds→1.82 milliseconds(best of56runs)📝 Explanation and details
The optimized code achieves an 11% speedup through several key performance improvements:
1. Eliminated ANSI escape code string concatenation in
bold()andmuted():"\033[1m" + text + "\033[0m"on every call_BOLD_PREFIX,_MUTED_PREFIX,_RESET) and uses f-strings, reducing memory allocations and string operations2. Moved
urllib.parse.urlparseimport to module scope:urlparseinside_colorized_url()function on every call (238 calls shown in profiler)3. Reduced attribute access overhead:
url.portto a local variable once instead of accessing it multiple timesresultvsurl_string)Impact on workloads:
The test results show consistent 4-15% improvements across all URL types, with particularly strong gains for:
Since
_colorized_url()appears to be used for server output formatting (likely in hot paths for web server responses), these micro-optimizations compound significantly when processing many URLs, making the 11% overall speedup valuable for server performance.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
import sys
function to test
from urllib.parse import urlparse
imports
import pytest
from marimo._server.print import _colorized_url
------------------- UNIT TESTS -------------------
Helper functions for expected output
def expected_bold(text):
return "\033[1m" + text + "\033[0m"
def expected_muted(text):
return "\033[37;2m" + text + "\033[0m"
1. Basic Test Cases
def test_basic_https_url_with_path():
# HTTPS URL with a path
url = "https://example.com/foo/bar"
expected = expected_bold("https://example.com/foo/bar")
codeflash_output = _colorized_url(url) # 14.3μs -> 13.3μs (6.98% faster)
def test_url_with_path_and_query_and_port():
# URL with path, port, and query
url = "https://example.com:8443/path/to/resource?x=1&y=2"
expected = expected_bold("https://example.com:8443/path/to/resource" + expected_muted("?x=1&y=2"))
codeflash_output = _colorized_url(url) # 16.2μs -> 14.8μs (9.66% faster)
2. Edge Test Cases
def test_url_with_trailing_slash():
# URL with trailing slash
url = "http://example.com/"
expected = expected_bold("http://example.com/")
codeflash_output = _colorized_url(url) # 12.5μs -> 11.8μs (5.98% faster)
def test_url_with_empty_path():
# URL with empty path (no trailing slash)
url = "http://example.com"
expected = expected_bold("http://example.com")
codeflash_output = _colorized_url(url) # 9.09μs -> 8.23μs (10.5% faster)
def test_url_with_path_only():
# Path only (no scheme/hostname)
url = "/foo/bar"
# urlparse: scheme='', hostname=None, path='/foo/bar'
expected = expected_bold("://None/foo/bar")
codeflash_output = _colorized_url(url) # 10.1μs -> 9.36μs (7.50% faster)
def test_url_with_query_only():
# Query only (no scheme/hostname/path)
url = "?foo=bar"
# urlparse: scheme='', hostname=None, path='', query='foo=bar'
expected = expected_bold("://None" + expected_muted("?foo=bar"))
codeflash_output = _colorized_url(url) # 10.8μs -> 9.78μs (9.94% faster)
def test_url_with_path_and_fragment_and_query():
# Path, query, and fragment
url = "https://example.com/foo/bar?x=1#frag"
expected = expected_bold("https://example.com/foo/bar" + expected_muted("?x=1"))
codeflash_output = _colorized_url(url) # 14.7μs -> 13.9μs (5.84% faster)
def test_url_with_uppercase_scheme_and_hostname():
# Uppercase scheme and hostname should be preserved
url = "HTTP://EXAMPLE.COM/foo"
expected = expected_bold("HTTP://EXAMPLE.COM/foo")
codeflash_output = _colorized_url(url) # 13.3μs -> 12.4μs (7.12% faster)
def test_url_with_port_0():
# Port 0 is valid, but urlparse treats it as 0 (should be included)
url = "http://example.com:0/foo"
expected = expected_bold("http://example.com:0/foo")
codeflash_output = _colorized_url(url) # 14.0μs -> 13.2μs (6.06% faster)
def test_url_with_port_65535():
# Max valid port
url = "http://example.com:65535/foo"
expected = expected_bold("http://example.com:65535/foo")
codeflash_output = _colorized_url(url) # 14.9μs -> 13.2μs (13.3% faster)
def test_large_number_of_query_params():
# Test with 500 query parameters
query = "&".join([f"x{i}={i}" for i in range(500)])
url = f"http://example.com/foo?{query}"
expected = expected_bold("http://example.com/foo" + expected_muted(f"?{query}"))
codeflash_output = _colorized_url(url) # 19.5μs -> 18.4μs (5.78% faster)
def test_long_path():
# Test with a long path (500 segments)
path = "/" + "/".join([f"segment{i}" for i in range(500)])
url = f"http://example.com{path}"
expected = expected_bold(f"http://example.com{path}")
codeflash_output = _colorized_url(url) # 20.8μs -> 19.3μs (7.86% faster)
def test_large_scale_ipv6():
# Test 100 URLs with unique IPv6 addresses and ports
for i in range(100):
url = f"http://[2001:db8::{i}]:{10000+i}/foo{i}?a={i}"
expected = expected_bold(f"http://2001:db8::{i}:{10000+i}/foo{i}" + expected_muted(f"?a={i}"))
codeflash_output = _colorized_url(url) # 1.01ms -> 899μs (12.7% faster)
4. Determinism Test
def test_determinism():
# The output should be the same for repeated calls
url = "https://example.com/foo?bar=baz"
codeflash_output = _colorized_url(url); out1 = codeflash_output # 13.4μs -> 12.9μs (3.77% faster)
codeflash_output = _colorized_url(url); out2 = codeflash_output # 4.90μs -> 4.26μs (15.0% faster)
5. Miscellaneous/Unusual Inputs
def test_url_with_spaces_in_path():
# Spaces in path should be preserved as-is
url = "http://example.com/foo bar"
expected = expected_bold("http://example.com/foo bar")
codeflash_output = _colorized_url(url) # 12.3μs -> 11.7μs (4.95% faster)
def test_url_with_encoded_characters():
# Encoded characters in path
url = "http://example.com/foo%20bar"
expected = expected_bold("http://example.com/foo%20bar")
codeflash_output = _colorized_url(url) # 12.0μs -> 11.4μs (4.65% faster)
def test_url_with_semicolon_in_query():
# Semicolon as query separator (should be preserved)
url = "http://example.com/foo?x=1;y=2"
expected = expected_bold("http://example.com/foo" + expected_muted("?x=1;y=2"))
codeflash_output = _colorized_url(url) # 13.2μs -> 12.6μs (5.20% faster)
def test_url_with_empty_query():
# URL with question mark but empty query
url = "http://example.com/foo?"
expected = expected_bold("http://example.com/foo")
codeflash_output = _colorized_url(url) # 13.4μs -> 12.3μs (8.95% faster)
def test_url_with_non_ascii_path():
# Non-ASCII characters in path
url = "http://example.com/路径"
expected = expected_bold("http://example.com/路径")
codeflash_output = _colorized_url(url) # 14.5μs -> 13.6μs (6.72% faster)
def test_url_with_non_ascii_query():
# Non-ASCII characters in query
url = "http://example.com/foo?ключ=значение"
expected = expected_bold("http://example.com/foo" + expected_muted("?ключ=значение"))
codeflash_output = _colorized_url(url) # 14.8μs -> 14.0μs (5.71% faster)
def test_url_with_dot_segments():
# Path with dot-segments
url = "http://example.com/foo/./bar/../baz"
expected = expected_bold("http://example.com/foo/./bar/../baz")
codeflash_output = _colorized_url(url) # 12.5μs -> 12.0μs (4.21% faster)
def test_url_with_percent_in_hostname():
# Percent-encoded hostname (rare, but test)
url = "http://xn--exmple-cua.com/foo"
expected = expected_bold("http://xn--exmple-cua.com/foo")
codeflash_output = _colorized_url(url) # 13.2μs -> 12.1μs (8.74% faster)
codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from urllib.parse import urlparse
imports
import pytest
from marimo._server.print import _colorized_url
unit tests
--- Basic Test Cases ---
#------------------------------------------------
from marimo._server.print import _colorized_url
def test__colorized_url():
_colorized_url('?;')
def test__colorized_url_2():
_colorized_url('')
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_bps3n5s8/tmp083y96nz/test_concolic_coverage.py::test__colorized_urlcodeflash_concolic_bps3n5s8/tmp083y96nz/test_concolic_coverage.py::test__colorized_url_2To edit these changes
git checkout codeflash/optimize-_colorized_url-mhvffehcand push.