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 get_encoding_from_headers by 19% #18

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 get_encoding_from_headers in src/requests/utils.py

⏱️ Runtime : 956 microseconds 803 microseconds (best of 27 runs)

📝 Explanation and details

Here's the optimized version of the original program.

Changes made for optimization.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 58 Passed
🌀 Generated Regression Tests 65 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 get_encoding_from_headers


# unit tests
def test_valid_content_type_with_charset():
    # Basic functionality tests
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=UTF-8'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=utf-8'})

def test_valid_content_type_without_charset():
    # Default encodings for known content types
    codeflash_output = get_encoding_from_headers({'content-type': 'text/plain'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json'})

def test_no_content_type_header():
    # No content-type header provided
    codeflash_output = get_encoding_from_headers({})

def test_empty_content_type_header():
    # Empty content-type header
    codeflash_output = get_encoding_from_headers({'content-type': ''})

def test_content_type_with_only_charset_parameter():
    # Content-type with only charset parameter
    codeflash_output = get_encoding_from_headers({'content-type': '; charset=UTF-8'})

def test_content_type_with_extra_whitespace():
    # Content-type with extra whitespace
    codeflash_output = get_encoding_from_headers({'content-type': ' text/html ; charset = UTF-8 '})

def test_content_type_with_quoted_charset_parameter():
    # Content-type with quoted charset parameter
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset="UTF-8"'})
    codeflash_output = get_encoding_from_headers({'content-type': "text/html; charset='UTF-8'"})

def test_mixed_case_content_type_and_charset():
    # Content-type and charset with mixed case
    codeflash_output = get_encoding_from_headers({'content-type': 'Text/Html; Charset=UTF-8'})


def test_content_type_with_multiple_parameters():
    # Content-type with multiple parameters
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=UTF-8; version=1.0'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=utf-8; version=2.0'})

def test_content_type_with_non_standard_parameter():
    # Content-type with non-standard parameter
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; foo=bar; charset=UTF-8'})

def test_large_headers_dictionary():
    # Large headers dictionary
    headers = {f'header{i}': f'value{i}' for i in range(1000)}
    headers['content-type'] = 'text/html; charset=UTF-8'
    codeflash_output = get_encoding_from_headers(headers)

def test_large_content_type_header():
    # Large content-type header
    large_header = 'text/html' + '; param=value' * 1000 + '; charset=UTF-8'
    codeflash_output = get_encoding_from_headers({'content-type': large_header})

def test_deterministic_behavior():
    # Deterministic behavior
    headers = {'content-type': 'text/html; charset=UTF-8'}
    codeflash_output = get_encoding_from_headers(headers)
    codeflash_output = get_encoding_from_headers(headers)

def test_multiple_charset_parameters():
    # Content-type with multiple charset parameters
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=UTF-8; charset=ISO-8859-1'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=ISO-8859-1; charset=UTF-8'})

def test_invalid_charset_value():
    # Content-type with invalid charset value
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=INVALID-CHARSET'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=INVALID-CHARSET'})

def test_charset_parameter_no_value():
    # Content-type with charset parameter but no value
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset='})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset='})

def test_special_characters_in_charset():
    # Content-type with special characters in charset
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=UTF@8'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=UTF@8'})

def test_non_ascii_characters_in_charset():
    # Content-type with non-ASCII characters in charset
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=UTF-😊'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=UTF-😊'})

def test_charset_parameter_different_position():
    # Content-type with charset parameter in different position
    codeflash_output = get_encoding_from_headers({'content-type': 'charset=UTF-8; text/html'})
    codeflash_output = get_encoding_from_headers({'content-type': 'version=2.0; charset=utf-8; application/json'})

def test_mixed_delimiters():
    # Content-type with mixed delimiters
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=UTF-8, version=1.0'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=utf-8, version=2.0'})

def test_nested_parameters():
    # Content-type with nested parameters
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset="UTF-8; version=1.0"'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset="utf-8; version=2.0"'})

def test_parameters_no_content_type():
    # Content-type with parameters but no content-type
    codeflash_output = get_encoding_from_headers({'content-type': '; charset=UTF-8; version=1.0'})

def test_semicolon_only():
    # Content-type with semicolon only
    codeflash_output = get_encoding_from_headers({'content-type': ';'})

def test_multiple_semicolons():
    # Content-type with multiple semicolons
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html;;;; charset=UTF-8'})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json;;;; charset=utf-8'})

def test_empty_parameter_values():
    # Content-type with empty parameter values
    codeflash_output = get_encoding_from_headers({'content-type': 'text/html; charset=; version='})
    codeflash_output = get_encoding_from_headers({'content-type': 'application/json; charset=; version='})
# 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 get_encoding_from_headers

# unit tests

def test_valid_content_type_with_charset():
    # Basic functionality: Valid Content-Type with charset
    headers = {"content-type": "text/html; charset=UTF-8"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "application/json; charset=utf-8"}
    codeflash_output = get_encoding_from_headers(headers)

def test_valid_content_type_without_charset():
    # Basic functionality: Valid Content-Type without charset
    headers = {"content-type": "text/html"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "application/json"}
    codeflash_output = get_encoding_from_headers(headers)

def test_no_content_type_header():
    # Missing or empty headers: No Content-Type header
    headers = {}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"some-other-header": "value"}
    codeflash_output = get_encoding_from_headers(headers)

def test_empty_content_type_header():
    # Missing or empty headers: Empty Content-Type header
    headers = {"content-type": ""}
    codeflash_output = get_encoding_from_headers(headers)

def test_malformed_content_type_values():
    # Malformed headers: Malformed Content-Type values
    headers = {"content-type": "text/html; charset="}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "text/html; charset=UTF-8; charset=UTF-16"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "text/html; charset=UTF-8; someparam"}
    codeflash_output = get_encoding_from_headers(headers)

def test_case_insensitivity_of_charset_parameter():
    # Case sensitivity: Case insensitivity of charset parameter
    headers = {"content-type": "text/html; CHARSET=UTF-8"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "application/json; Charset=utf-8"}
    codeflash_output = get_encoding_from_headers(headers)

def test_whitespace_around_parameters():
    # Whitespace handling: Whitespace around parameters
    headers = {"content-type": "text/html ; charset = UTF-8 "}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": " application/json ; charset = utf-8 "}
    codeflash_output = get_encoding_from_headers(headers)

def test_multiple_parameters():
    # Unusual but valid headers: Multiple parameters
    headers = {"content-type": "text/html; charset=UTF-8; boundary=something"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "application/json; charset=utf-8; version=1.0"}
    codeflash_output = get_encoding_from_headers(headers)

def test_charset_in_quotes():
    # Edge cases: Charset in quotes
    headers = {"content-type": "text/html; charset='UTF-8'"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "application/json; charset=\"utf-8\""}
    codeflash_output = get_encoding_from_headers(headers)

def test_charset_with_special_characters():
    # Edge cases: Charset with special characters
    headers = {"content-type": "text/html; charset=UTF_8"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "application/json; charset=utf@8"}
    codeflash_output = get_encoding_from_headers(headers)

def test_large_number_of_headers():
    # Large scale test cases: Large number of headers
    headers = {"content-type": "application/json"}
    for i in range(1, 1001):
        headers[f"header{i}"] = f"value{i}"
    codeflash_output = get_encoding_from_headers(headers)

def test_non_standard_content_types():
    # Non-standard content types: Non-standard but common content types
    headers = {"content-type": "application/xml; charset=UTF-8"}
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "image/png"}
    codeflash_output = get_encoding_from_headers(headers)

def test_invalid_header_format():
    # Invalid headers: Invalid header format
    headers = {"content-type": "text/html charset=UTF-8"}  # missing semicolon
    codeflash_output = get_encoding_from_headers(headers)

    headers = {"content-type": "charset=UTF-8; text/html"}  # incorrect order
    codeflash_output = get_encoding_from_headers(headers)
# 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 optimized version of the original program.

Changes made for optimization.
@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 16:01
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