Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up method PreparedRequest.prepare_url by 88% #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Dec 22, 2024

📄 88% (0.88x) speedup for PreparedRequest.prepare_url in src/requests/models.py

⏱️ Runtime : 9.88 milliseconds 5.26 milliseconds (best of 38 runs)

📝 Explanation and details

To optimize the given Python program, we can make a few changes to remove redundant imports, streamline logic, and enhance performance.

Key changes made.

These optimizations should help reduce the runtime and memory usage while maintaining the functionality of your program.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 71 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 79.5%
🌀 Generated Regression Tests Details
import pytest  # used for our unit tests
from src.requests._internal_utils import to_native_string, unicode_is_ascii
# function to test
from src.requests.compat import builtin_str, bytes, quote, str, urlunparse
from src.requests.exceptions import InvalidURL, MissingSchema
from src.requests.hooks import default_hooks
from src.requests.models import PreparedRequest
from src.requests.utils import requote_uri
from urllib3.exceptions import LocationParseError
from urllib3.util import parse_url


# unit tests
@pytest.mark.parametrize("url, params, expected", [
    # Basic Valid URL
    ("http://example.com", None, "http://example.com/"),
    ("https://example.com", None, "https://example.com/"),

    # URL with Parameters
    ("http://example.com", {"key": "value"}, "http://example.com/?key=value"),
    ("http://example.com", {"key1": "value1", "key2": "value2"}, "http://example.com/?key1=value1&key2=value2"),
    ("http://example.com", {"key": "value with spaces", "key2": "value/with/slash"}, "http://example.com/?key=value%20with%20spaces&key2=value%2Fwith%2Fslash"),

    # URL with Authentication
    ("http://user:[email protected]", None, "http://user:[email protected]/"),

    # URL with Port
    ("http://example.com:8080", None, "http://example.com:8080/"),

    # URL with Path
    ("http://example.com/path", None, "http://example.com/path"),
    ("http://example.com/path/to/resource", None, "http://example.com/path/to/resource"),

    # URL with Query String
    ("http://example.com?existing=param", {"key": "value"}, "http://example.com/?existing=param&key=value"),

    # URL with Fragment
    ("http://example.com#section", None, "http://example.com/#section"),

    # Non-HTTP Schemes
    ("mailto:[email protected]", None, "mailto:[email protected]"),
    ("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", None, "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="),

    # Invalid URLs
    pytest.param("example.com", None, None, marks=pytest.mark.xfail(raises=MissingSchema)),
    pytest.param("http:///path", None, None, marks=pytest.mark.xfail(raises=InvalidURL)),
    pytest.param("http://exa mple.com", None, None, marks=pytest.mark.xfail(raises=InvalidURL)),

    # Unicode and IDNA Encoding
    ("http://xn--fsq.com", None, "http://xn--fsq.com/"),
    ("http://example.com/ümlaut", None, "http://example.com/%C3%BCmlaut"),

    # Edge Cases
    ("  http://example.com", None, "http://example.com/"),
    ("http://example.com  ", None, "http://example.com/"),
    pytest.param("", None, None, marks=pytest.mark.xfail(raises=InvalidURL)),
    pytest.param("http://", None, None, marks=pytest.mark.xfail(raises=InvalidURL)),

    # Large Scale Test Cases
    ("http://example.com/" + "a" * 2048, None, "http://example.com/" + "a" * 2048),
    ("http://example.com", {f"key{i}": f"value{i}" for i in range(1000)}, "http://example.com/?" + "&".join([f"key{i}=value{i}" for i in range(1000)])),

    # Deterministic Behavior
    ("http://example.com", None, "http://example.com/"),
    ("http://example.com", None, "http://example.com/"),
])
def test_prepare_url(url, params, expected):
    # Create a PreparedRequest instance
    req = PreparedRequest()
    # Call the prepare_url method with the given URL and parameters
    req.prepare_url(url, params)

@pytest.mark.parametrize("url, params, expected", [
    # URLs with Unusual Schemes
    ("ftp://example.com", None, "ftp://example.com/"),
    ("custom-scheme://example.com", None, "custom-scheme://example.com/"),

    # URLs with Unusual Characters in Path
    ("http://example.com/path/with/reserved;characters", None, "http://example.com/path/with/reserved;characters"),
    ("http://example.com/path/with/?query=param", None, "http://example.com/path/with/?query=param"),
    ("http://example.com/path%20with%20spaces", None, "http://example.com/path%20with%20spaces"),
    ("http://example.com/path%2Fwith%2Fslashes", None, "http://example.com/path%2Fwith%2Fslashes"),

    # URLs with Unusual Characters in Query
    ("http://example.com", {"key": "value;with;semicolons"}, "http://example.com/?key=value;with;semicolons"),
    ("http://example.com", {"key": "value/with/slash"}, "http://example.com/?key=value/with/slash"),
    ("http://example.com", {"key": "value%20with%20spaces"}, "http://example.com/?key=value%20with%20spaces"),
    ("http://example.com", {"key": "value%2Fwith%2Fslashes"}, "http://example.com/?key=value%2Fwith%2Fslashes"),

    # URLs with Multiple At Signs in Authentication
    ("http://user@name:pass@[email protected]", None, "http://user%40name:pass@[email protected]/"),
    ("http://user:pass@[email protected]", None, "http://user:pass@[email protected]/"),

    # URLs with IPv6 Addresses
    ("http://[2001:db8::1]", None, "http://[2001:db8::1]/"),
    ("http://[2001:db8::1]:8080", None, "http://[2001:db8::1]:8080/"),

    # URLs with Unusual Ports
    ("http://example.com:0", None, "http://example.com:0/"),
    ("http://example.com:65535", None, "http://example.com:65535/"),

    # URLs with Unusual Fragments
    ("http://example.com#section/with/slash", None, "http://example.com/#section/with/slash"),
    ("http://example.com#section?with=query", None, "http://example.com/#section?with=query"),
    ("http://example.com#section%20with%20spaces", None, "http://example.com/#section%20with%20spaces"),
    ("http://example.com#section%2Fwith%2Fslashes", None, "http://example.com/#section%2Fwith%2Fslashes"),

    # URLs with Empty Components
    ("http://example.com?", None, "http://example.com/?"),
    ("http://example.com#", None, "http://example.com/#"),
    ("http://@example.com", None, "http://@example.com/"),
    ("http://:@example.com", None, "http://:@example.com/"),

    # URLs with Mixed Encodings
    ("http://example.com/path%20with%20spaces?query=param%20with%20spaces", None, "http://example.com/path%20with%20spaces?query=param%20with%20spaces"),
    ("http://example.com/path/with/slash?query=param/with/slash", None, "http://example.com/path/with/slash?query=param/with/slash"),

    # URLs with Long Query Strings
    ("http://example.com", {"key": "a" * 2048}, "http://example.com/?key=" + "a" * 2048),
    ("http://example.com", {"key1": "value1", "key2": "a" * 1024}, "http://example.com/?key1=value1&key2=" + "a" * 1024),
])
def test_prepare_url_edge_cases(url, params, expected):
    # Create a PreparedRequest instance
    req = PreparedRequest()
    # Call the prepare_url method with the given URL and parameters
    req.prepare_url(url, params)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

import pytest  # used for our unit tests
from src.requests._internal_utils import to_native_string, unicode_is_ascii
from src.requests.compat import builtin_str, bytes, quote, str, urlunparse
from src.requests.exceptions import InvalidURL, MissingSchema
from src.requests.hooks import default_hooks
from src.requests.models import PreparedRequest
from src.requests.utils import requote_uri
from urllib3.exceptions import LocationParseError
from urllib3.util import parse_url

# unit tests

# 1. Valid HTTP URLs
def test_simple_http_url():
    req = PreparedRequest()
    req.prepare_url("http://example.com", None)

def test_http_url_with_query():
    req = PreparedRequest()
    req.prepare_url("http://example.com", "param=value")

# 2. Valid HTTPS URLs
def test_simple_https_url():
    req = PreparedRequest()
    req.prepare_url("https://example.com", None)

def test_https_url_with_query():
    req = PreparedRequest()
    req.prepare_url("https://example.com", "param=value")

# 3. URLs with Authentication Information
def test_http_url_with_auth():
    req = PreparedRequest()
    req.prepare_url("http://user:[email protected]", None)

def test_https_url_with_auth():
    req = PreparedRequest()
    req.prepare_url("https://user:[email protected]", None)

# 4. URLs with Ports
def test_http_url_with_port():
    req = PreparedRequest()
    req.prepare_url("http://example.com:8080", None)

def test_https_url_with_port():
    req = PreparedRequest()
    req.prepare_url("https://example.com:8443", None)

# 5. URLs with Unicode Characters
def test_unicode_in_domain():
    req = PreparedRequest()
    req.prepare_url("http://xn--fsq.com", None)

def test_unicode_in_path():
    req = PreparedRequest()
    req.prepare_url("http://example.com/路径", None)

# 6. Non-HTTP Schemes
def test_mailto_scheme():
    req = PreparedRequest()
    req.prepare_url("mailto:[email protected]", None)

def test_data_scheme():
    req = PreparedRequest()
    req.prepare_url("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", None)

# 7. Invalid URLs
def test_missing_scheme():
    req = PreparedRequest()
    with pytest.raises(MissingSchema):
        req.prepare_url("example.com", None)

def test_missing_host():
    req = PreparedRequest()
    with pytest.raises(InvalidURL):
        req.prepare_url("http:///path", None)


def test_leading_whitespace():
    req = PreparedRequest()
    req.prepare_url("  http://example.com", None)

# 9. URLs with Parameters
def test_string_params():
    req = PreparedRequest()
    req.prepare_url("http://example.com", "param=value")

def test_bytes_params():
    req = PreparedRequest()
    req.prepare_url("http://example.com", b'param=value')

def test_dict_params():
    req = PreparedRequest()
    req.prepare_url("http://example.com", {'param': 'value'})

# 10. Large Scale Test Cases
def test_long_url():
    long_path = "a" * 10000
    req = PreparedRequest()
    req.prepare_url(f"http://example.com/{long_path}", None)

def test_many_query_params():
    params = "&".join([f"param{i}=value{i}" for i in range(1000)])
    req = PreparedRequest()
    req.prepare_url("http://example.com", params)

# 11. Edge Cases

def test_url_with_only_scheme():
    req = PreparedRequest()
    with pytest.raises(InvalidURL):
        req.prepare_url("http://", None)

📢 Feedback on this optimization? Discord

To optimize the given Python program, we can make a few changes to remove redundant imports, streamline logic, and enhance performance.

Key changes made.

These optimizations should help reduce the runtime and memory usage while maintaining the functionality of your program.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 22, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r December 22, 2024 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants