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 unquote_unreserved by 6% #19

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

⏱️ Runtime : 5.65 milliseconds 5.34 milliseconds (best of 34 runs)

📝 Explanation and details

To optimize this program, we can make minor adjustments to reduce unnecessary operations and optimize the flow.

Changes made.

  1. Early Exit for Common Case: Adding an early return if there are no percent-escape sequences present.
  2. String Construction Optimization: Instead of modifying and re-joining the parts list in-place, I've created a new list unquoted_parts for the final result. This reduces the overhead of modifying the original list.
  3. Slice Moving in the loop: Moved the slice operation inside conditional cases to avoid slicing parts multiple times.

These changes minimize the number of operations, which can be beneficial for large or complex inputs.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 57 Passed
🌀 Generated Regression Tests 46 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
# function to test
from src.requests.exceptions import InvalidURL
from src.requests.utils import unquote_unreserved

UNRESERVED_SET = frozenset(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789-._~"
)
from src.requests.utils import unquote_unreserved

# unit tests

def test_basic_functionality():
    # Simple unreserved characters
    codeflash_output = unquote_unreserved("Hello%20World")
    codeflash_output = unquote_unreserved("abc%20def")
    # Mixed reserved and unreserved characters
    codeflash_output = unquote_unreserved("abc%20def%7Eghi")
    codeflash_output = unquote_unreserved("foo%2Fbar%3Fbaz")

def test_edge_cases():
    # Empty string
    codeflash_output = unquote_unreserved("")
    # No percent-escape sequences
    codeflash_output = unquote_unreserved("abcdef")
    # Single percent character
    codeflash_output = unquote_unreserved("%")


def test_valid_percent_escape_sequences_for_unreserved_characters():
    # Lowercase hexadecimal
    codeflash_output = unquote_unreserved("abc%7e")
    codeflash_output = unquote_unreserved("foo%2dbar")
    # Uppercase hexadecimal
    codeflash_output = unquote_unreserved("abc%7E")
    codeflash_output = unquote_unreserved("foo%2Dbar")

def test_valid_percent_escape_sequences_for_reserved_characters():
    # Reserved characters
    codeflash_output = unquote_unreserved("abc%2Fdef")
    codeflash_output = unquote_unreserved("foo%3Fbar")

def test_mixed_valid_and_invalid_percent_escape_sequences():
    # Mixed sequences
    with pytest.raises(InvalidURL):
        unquote_unreserved("abc%7E%2G")
    with pytest.raises(InvalidURL):
        unquote_unreserved("foo%2Dbar%ZZ")

def test_large_scale_test_cases():
    # Long URI with multiple percent-escape sequences
    codeflash_output = unquote_unreserved("a%62c%64e" * 1000)
    codeflash_output = unquote_unreserved("%7E%7E%7E" * 1000)
    # Long URI with mixed characters
    codeflash_output = unquote_unreserved("a%62c%20d%2Fe%3F" * 1000)

def test_non_ascii_characters():
    # Percent-escape sequences representing non-ASCII characters
    codeflash_output = unquote_unreserved("abc%C3%A9")
    codeflash_output = unquote_unreserved("foo%E2%9C%93")

def test_special_cases():
    # Percent-escape sequences at the start and end
    codeflash_output = unquote_unreserved("%7Eabc")
    codeflash_output = unquote_unreserved("abc%7E")
    # Consecutive percent-escape sequences
    codeflash_output = unquote_unreserved("abc%7E%7E%7E")
    codeflash_output = unquote_unreserved("foo%2D%2D%2D")
# 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
# function to test
from src.requests.exceptions import InvalidURL
from src.requests.utils import unquote_unreserved

UNRESERVED_SET = frozenset(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789-._~"
)
from src.requests.utils import unquote_unreserved

# unit tests

def test_basic_functionality():
    # Simple Unreserved Characters
    codeflash_output = unquote_unreserved("hello%20world")
    codeflash_output = unquote_unreserved("foo%2Dbar")
    codeflash_output = unquote_unreserved("baz%2Equx")

def test_edge_cases():
    # Empty String
    codeflash_output = unquote_unreserved("")
    # Single Percent Sign
    codeflash_output = unquote_unreserved("%")

def test_valid_percent_encoding():
    # Valid Hexadecimal Percent-Encoding
    codeflash_output = unquote_unreserved("hello%20world%21")
    codeflash_output = unquote_unreserved("foo%2Dbar%7E")
    codeflash_output = unquote_unreserved("baz%2Equx%5F")

def test_invalid_percent_encoding():
    # Incomplete Percent-Encoding
    codeflash_output = unquote_unreserved("hello%")
    codeflash_output = unquote_unreserved("foo%2")
    # Non-Hexadecimal Characters
    with pytest.raises(InvalidURL):
        unquote_unreserved("hello%2Gworld")
    with pytest.raises(InvalidURL):
        unquote_unreserved("foo%ZZbar")

def test_mixed_content():
    # Combination of Unreserved and Reserved Characters
    codeflash_output = unquote_unreserved("hello%20world%21foo%2Dbar%7Ebaz%2E")
    codeflash_output = unquote_unreserved("test%25string%3Fwith%26reserved%2Bchars")

def test_non_ascii_characters():
    # Percent-Encoding Non-ASCII Characters
    codeflash_output = unquote_unreserved("hello%C3%A9world")
    codeflash_output = unquote_unreserved("foo%C3%B1bar")

def test_large_scale():
    # Long URIs
    codeflash_output = unquote_unreserved("a" * 1000 + "%20" * 1000)
    codeflash_output = unquote_unreserved("%41" * 1000)

def test_special_characters():
    # Special Characters in URI
    codeflash_output = unquote_unreserved("hello%2Dworld%2Efoo%2Dbar")
    codeflash_output = unquote_unreserved("test%2Fstring%3Fwith%3Dspecial%26chars")

def test_boundary_values():
    # Boundary Values for Hexadecimal
    codeflash_output = unquote_unreserved("foo%00bar")
    codeflash_output = unquote_unreserved("baz%FFqux")
# 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

To optimize this program, we can make minor adjustments to reduce unnecessary operations and optimize the flow.



### Changes made.
1. **Early Exit for Common Case**: Adding an early return if there are no percent-escape sequences present.
2. **String Construction Optimization**: Instead of modifying and re-joining the parts list in-place, I've created a new list `unquoted_parts` for the final result. This reduces the overhead of modifying the original list.
3. **Slice Moving in the loop**: Moved the slice operation inside conditional cases to avoid slicing parts multiple times.

These changes minimize the number of operations, which can be beneficial for large or complex inputs.
@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:11
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