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 MockRequest.get_full_url by 6% #26

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

📄 6% (0.06x) speedup for MockRequest.get_full_url in src/requests/cookies.py

⏱️ Runtime : 6.79 milliseconds 6.43 milliseconds (best of 75 runs)

📝 Explanation and details

To optimize the code for performance, I will make the following adjustments.

  1. Remove redundant re-import of the to_native_string function.
  2. Optimize the check and operation within the get_full_url method.

Here's the updated code.

Changes made.

  1. Removed the redundant import of to_native_string from src.requests._internal_utils.
  2. Directly returned the original string in to_native_string if isinstance(string, builtin_str) to avoid unnecessary assignment.
  3. In get_full_url, reduced the number of dictionary lookups, string operations, and repeated operations on parsed to increase efficiency.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1039 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests Details
from unittest.mock import Mock

# imports
import pytest  # used for our unit tests
# function to test
from src.requests._internal_utils import to_native_string
from src.requests.compat import builtin_str, urlparse, urlunparse
from src.requests.cookies import MockRequest

# unit tests

# Basic Functionality
def test_no_host_header():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={}))
    codeflash_output = request.get_full_url()

def test_custom_host_header():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={'Host': 'custom.com'}))
    codeflash_output = request.get_full_url()

# Different URL Schemes
def test_different_url_schemes():
    request = MockRequest(request=Mock(url='ftp://example.com/path', headers={}))
    codeflash_output = request.get_full_url()

# Complex URLs
def test_complex_url():
    request = MockRequest(request=Mock(url='http://example.com/path;params?query=1#fragment', headers={}))
    codeflash_output = request.get_full_url()

def test_complex_url_with_host():
    request = MockRequest(request=Mock(url='http://example.com/path;params?query=1#fragment', headers={'Host': 'custom.com'}))
    codeflash_output = request.get_full_url()

# Non-ASCII Characters in Host Header
def test_non_ascii_host_header():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={'Host': 'cüstom.com'}))
    codeflash_output = request.get_full_url()

# Edge Cases: Empty or Missing URL Components
def test_empty_url_components():
    request = MockRequest(request=Mock(url='http://example.com', headers={}))
    codeflash_output = request.get_full_url()

# Invalid Inputs: Malformed URLs
def test_malformed_urls():
    request = MockRequest(request=Mock(url='http:///example.com/path', headers={}))
    codeflash_output = request.get_full_url()

# Unusual Characters in URL
def test_unusual_characters_in_url():
    request = MockRequest(request=Mock(url='http://example.com/pa^th', headers={}))
    codeflash_output = request.get_full_url()

# Unusual Characters in Host Header
def test_unusual_characters_in_host_header():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={'Host': 'cust^om.com'}))
    codeflash_output = request.get_full_url()

# Internationalized Domain Names (IDN)
def test_internationalized_domain_names():
    request = MockRequest(request=Mock(url='http://xn--fsq.com/path', headers={}))
    codeflash_output = request.get_full_url()

# IPv6 Addresses in URL
def test_ipv6_addresses():
    request = MockRequest(request=Mock(url='http://[2001:db8::1]/path', headers={}))
    codeflash_output = request.get_full_url()

# Port Numbers in Host Header
def test_port_numbers_in_host_header():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={'Host': 'custom.com:8080'}))
    codeflash_output = request.get_full_url()

# URLs with User Information
def test_urls_with_user_info():
    request = MockRequest(request=Mock(url='http://user:[email protected]/path', headers={}))
    codeflash_output = request.get_full_url()

# URLs with Multiple Query Parameters
def test_multiple_query_parameters():
    request = MockRequest(request=Mock(url='http://example.com/path?query1=value1&query2=value2', headers={}))
    codeflash_output = request.get_full_url()

# Empty "Host" Header
def test_empty_host_header():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={'Host': ''}))
    codeflash_output = request.get_full_url()

# Large Scale Performance
def test_large_scale_performance():
    request = MockRequest(request=Mock(url='http://example.com/path', headers={'Host': 'custom.com'}))
    for _ in range(1000):
        codeflash_output = request.get_full_url()
# 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, urlunparse

# imports
import pytest  # used for our unit tests
# function to test
from src.requests._internal_utils import to_native_string
from src.requests.compat import builtin_str, urlparse, urlunparse
from src.requests.cookies import MockRequest

# unit tests

# Mock request class for testing
class Request:
    def __init__(self, url, headers=None):
        self.url = url
        self.headers = headers or {}

# Basic Functionality Tests
def test_no_host_header():
    request = Request("http://example.com")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_with_host_header():
    request = Request("http://example.com", headers={"Host": "example.org"})
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# Different URL Schemes
def test_http_scheme():
    request = Request("http://example.com")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_https_scheme():
    request = Request("https://example.com")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_ftp_scheme():
    request = Request("ftp://example.com")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# URL Components
def test_with_path():
    request = Request("http://example.com/path/to/resource")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_with_query():
    request = Request("http://example.com/path?query=value")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_with_fragment():
    request = Request("http://example.com/path#section")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_with_params():
    request = Request("http://example.com/path;params")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# Edge Cases
def test_empty_url():
    request = Request("")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_malformed_url():
    request = Request("http:///example.com")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_url_with_non_standard_chars():
    request = Request("http://example.com/路径")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_host_with_non_standard_chars():
    request = Request("http://example.com", headers={"Host": "例子.测试"})
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# Encoding Issues
def test_host_with_utf8_chars():
    request = Request("http://example.com", headers={"Host": "exámple.com"})
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()


def test_long_url():
    long_path = "a" * 1000
    request = Request(f"http://example.com/{long_path}")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_large_number_of_headers():
    headers = {f"Header-{i}": f"Value-{i}" for i in range(1000)}
    headers["Host"] = "example.org"
    request = Request("http://example.com", headers=headers)
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# Special Characters in Host Header
def test_host_with_special_chars():
    request = Request("http://example.com", headers={"Host": "example.com:8080"})
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_host_with_user_pass():
    request = Request("http://example.com", headers={"Host": "user:[email protected]"})
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# Different URL Formats
def test_ipv6_url():
    request = Request("http://[::1]")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_ipv4_url():
    request = Request("http://127.0.0.1")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

def test_url_with_port():
    request = Request("http://example.com:8080")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# Mixed Case Scenarios
def test_mixed_case_url_and_host():
    request = Request("http://Example.Com/Path", headers={"Host": "Example.Org"})
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()

# URL with Username and Password
def test_url_with_credentials():
    request = Request("http://user:[email protected]")
    mock_request = MockRequest(request)
    codeflash_output = mock_request.get_full_url()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from src.requests.cookies import MockRequest

📢 Feedback on this optimization? Discord

To optimize the code for performance, I will make the following adjustments.
1. Remove redundant re-import of the `to_native_string` function.
2. Optimize the check and operation within the `get_full_url` method.

Here's the updated code.



### Changes made.
1. Removed the redundant import of `to_native_string` from `src.requests._internal_utils`.
2. Directly returned the original string in `to_native_string` if `isinstance(string, builtin_str)` to avoid unnecessary assignment.
3. In `get_full_url`, reduced the number of dictionary lookups, string operations, and repeated operations on `parsed` to increase efficiency.
@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 19:43
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