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 function _parse_content_type_header by 19% #17

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

📄 19% (0.19x) speedup for _parse_content_type_header in src/requests/utils.py

⏱️ Runtime : 2.31 milliseconds 1.94 millisecond (best of 24 runs)

📝 Explanation and details

Here's the rewritten program optimized for faster runtime.

Changes made.

  1. Removed the extra assignment to params variable and stripped tokens in the loop directly.
  2. Used split("=", 1) to get key and value directly, improving efficiency and readability.
  3. Simplified key assignment by combining the strip and lower operations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 64 Passed
🌀 Generated Regression Tests 63 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests Details
- test_utils.py
🌀 Generated Regression Tests Details
import pytest  # used for our unit tests
from src.requests.utils import _parse_content_type_header

# unit tests

def test_simple_content_type_no_params():
    codeflash_output = _parse_content_type_header("application/json")
    codeflash_output = _parse_content_type_header("text/html")

def test_content_type_with_single_param():
    codeflash_output = _parse_content_type_header("application/json; charset=utf-8")
    codeflash_output = _parse_content_type_header("text/html; charset=ISO-8859-1")

def test_content_type_with_multiple_params():
    codeflash_output = _parse_content_type_header("application/json; charset=utf-8; version=1.0")
    codeflash_output = _parse_content_type_header("text/html; charset=ISO-8859-1; format=flowed")

def test_empty_header():
    codeflash_output = _parse_content_type_header("")

def test_whitespace_only():
    codeflash_output = _parse_content_type_header("   ")

def test_leading_trailing_semicolons():
    codeflash_output = _parse_content_type_header(";application/json")
    codeflash_output = _parse_content_type_header("application/json;")

def test_single_param_without_value():
    codeflash_output = _parse_content_type_header("application/json; charset")
    codeflash_output = _parse_content_type_header("text/html; charset")

def test_multiple_params_without_values():
    codeflash_output = _parse_content_type_header("application/json; charset; version")
    codeflash_output = _parse_content_type_header("text/html; charset; format")

def test_mixed_case_and_whitespace():
    codeflash_output = _parse_content_type_header("Application/Json; Charset=UTF-8")
    codeflash_output = _parse_content_type_header("application/json;  charset=utf-8  ")
    codeflash_output = _parse_content_type_header("text/html;   charset=ISO-8859-1;  format=flowed  ")

def test_parameters_with_quoted_values():
    codeflash_output = _parse_content_type_header('application/json; charset="utf-8"')
    codeflash_output = _parse_content_type_header("text/html; charset='ISO-8859-1'")

def test_parameters_with_special_characters():
    codeflash_output = _parse_content_type_header("application/json; charset=utf-8; version=1.0-beta")
    codeflash_output = _parse_content_type_header('text/html; charset=ISO-8859-1; format=flowed; boundary="abc123"')

def test_large_number_of_parameters():
    header = "application/json; " + "; ".join(f"param{i}=value{i}" for i in range(1, 1001))
    expected_params = {f"param{i}": f"value{i}" for i in range(1, 1001)}
    codeflash_output = _parse_content_type_header(header)

def test_long_content_type_and_parameter_values():
    long_content_type = "application/" + "verylongcontenttype" * 10
    long_value = "verylongvalue" * 10
    header = f"{long_content_type}; param={long_value}"
    codeflash_output = _parse_content_type_header(header)

def test_malformed_headers():
    codeflash_output = _parse_content_type_header("application/json; charset=utf-8;=value")
    codeflash_output = _parse_content_type_header("text/html; charset=ISO-8859-1; key=")

def test_headers_with_multiple_equals_signs():
    codeflash_output = _parse_content_type_header("application/json; param=key=value")
    codeflash_output = _parse_content_type_header("text/html; charset=ISO-8859-1; key=value=extra")

def test_consistent_parsing():
    header = "application/json; charset=utf-8"
    result = ("application/json", {"charset": "utf-8"})
    codeflash_output = _parse_content_type_header(header)
    codeflash_output = _parse_content_type_header(header)  # repeated call to ensure consistency
# 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.utils import _parse_content_type_header

# unit tests

def test_basic_content_type_no_params():
    codeflash_output = _parse_content_type_header("text/plain")
    codeflash_output = _parse_content_type_header("application/json")

def test_content_type_single_param():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8")
    codeflash_output = _parse_content_type_header("application/xml; version=1.0")

def test_content_type_multiple_params():
    codeflash_output = _parse_content_type_header("multipart/form-data; boundary=something; charset=UTF-8")
    codeflash_output = _parse_content_type_header("application/json; charset=UTF-8; version=1.1")

def test_params_with_quoted_values():
    codeflash_output = _parse_content_type_header('text/html; charset="UTF-8"')
    codeflash_output = _parse_content_type_header("application/xml; version='1.0'")

def test_params_with_extra_whitespace():
    codeflash_output = _parse_content_type_header("text/html ; charset = UTF-8 ")
    codeflash_output = _parse_content_type_header("application/json;  charset  =  UTF-8 ;  version =  1.1 ")

def test_params_with_no_value():
    codeflash_output = _parse_content_type_header("text/html; compress")
    codeflash_output = _parse_content_type_header("application/json; secure")

def test_case_insensitivity_in_params():
    codeflash_output = _parse_content_type_header("text/html; CHARSET=UTF-8")
    codeflash_output = _parse_content_type_header("application/json; Charset=UTF-8")

def test_empty_header_string():
    codeflash_output = _parse_content_type_header("")

def test_header_string_with_only_whitespace():
    codeflash_output = _parse_content_type_header("   ")

def test_header_with_semicolons_but_no_params():
    codeflash_output = _parse_content_type_header("text/html;")
    codeflash_output = _parse_content_type_header("application/json;;")

def test_header_with_malformed_params():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8; version")
    codeflash_output = _parse_content_type_header("application/json; charset=UTF-8; version=")

def test_header_with_special_characters_in_params():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8; version=1.0-beta")
    codeflash_output = _parse_content_type_header("application/json; charset=UTF-8; token=abc123!@#")

def test_header_with_duplicate_params():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8; charset=ISO-8859-1")
    codeflash_output = _parse_content_type_header("application/json; version=1.0; version=2.0")

def test_large_number_of_params():
    header = "text/html;" + "; ".join(f"param{i}=value{i}" for i in range(1, 101))
    expected_params = {f"param{i}": f"value{i}" for i in range(1, 101)}
    codeflash_output = _parse_content_type_header(header)

    header = "application/json;" + "; ".join(f"key{i}=val{i}" for i in range(1, 51))
    expected_params = {f"key{i}": f"val{i}" for i in range(1, 51)}
    codeflash_output = _parse_content_type_header(header)

def test_header_with_non_ascii_characters():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8; title=こんにちは")
    codeflash_output = _parse_content_type_header("application/json; charset=UTF-8; description=测试")

def test_header_with_params_containing_equals_signs():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8; data=key=value")
    codeflash_output = _parse_content_type_header("application/json; charset=UTF-8; config=setting1=value1,setting2=value2")

def test_header_with_params_containing_semicolons():
    codeflash_output = _parse_content_type_header("text/html; charset=UTF-8; description=some;text;here")
    codeflash_output = _parse_content_type_header("application/json; charset=UTF-8; info=part1;part2")

def test_large_scale_performance():
    header = "text/html;" + "; ".join(f"param{i}=value{i}" for i in range(1, 1001))
    expected_params = {f"param{i}": f"value{i}" for i in range(1, 1001)}
    codeflash_output = _parse_content_type_header(header)

    header = "application/json;" + "; ".join(f"key{i}=val{i}" for i in range(1, 1001))
    expected_params = {f"key{i}": f"val{i}" for i in range(1, 1001)}
    codeflash_output = _parse_content_type_header(header)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

📢 Feedback on this optimization? Discord

Here's the rewritten program optimized for faster runtime.



Changes made.
1. Removed the extra assignment to `params` variable and stripped tokens in the loop directly.
2. Used `split("=", 1)` to get `key` and `value` directly, improving efficiency and readability.
3. Simplified `key` assignment by combining the strip and lower operations.
@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 15: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