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 _force_correct_text_stream by 17% #45

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 20, 2024

📄 17% (0.17x) speedup for _force_correct_text_stream in src/click/_compat.py

⏱️ Runtime : 463 microseconds 396 microseconds (best of 22 runs)

📝 Explanation and details

Here's the optimized version of your code. I've removed redundant checks and refactored some parts for better performance.

This version of the code minimizes function calls, removes redundant checks, and streamlines the logic without altering the functionality. This should help in improving the overall performance of the program.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 2 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 91.7%
🌀 Generated Regression Tests Details
from __future__ import annotations

import typing as t
from io import BytesIO, StringIO
from typing import IO, Any, BinaryIO, Callable, TextIO, Union

# imports
import pytest  # used for our unit tests
from src.click._compat import _force_correct_text_stream


# Mock utilities
def _is_compat_stream_attr(stream: t.TextIO, attr: str, value: str | None) -> bool:
    return getattr(stream, attr, None) == value

def is_ascii_encoding(encoding: str) -> bool:
    return encoding.lower() == 'ascii'

def get_best_encoding(stream: t.BinaryIO) -> str:
    return 'utf-8'

class _NonClosingTextIOWrapper(StringIO):
    def __init__(self, stream: t.BinaryIO, encoding: str, errors: str, line_buffering: bool, force_readable: bool, force_writable: bool):
        super().__init__(stream.read().decode(encoding, errors))
        self.encoding = encoding
        self.errors = errors
        self.force_readable = force_readable
        self.force_writable = force_writable

# unit tests


























from __future__ import annotations

import io  # used for creating in-memory stream objects
import typing as t
from typing import IO, Any, BinaryIO, Callable, TextIO

# imports
import pytest  # used for our unit tests
from src.click._compat import _force_correct_text_stream

# unit tests

# Mock helper functions and classes
def _is_compat_stream_attr(stream, attr, value):
    return getattr(stream, attr, None) == value

def is_ascii_encoding(encoding):
    return encoding.lower() == "ascii"

def get_best_encoding(stream):
    return "utf-8"

class _NonClosingTextIOWrapper(io.TextIOWrapper):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.force_readable = kwargs.get('force_readable', False)
        self.force_writable = kwargs.get('force_writable', False)

    def readable(self):
        return self.force_readable or super().readable()

    def writable(self):
        return self.force_writable or super().writable()

# Mock is_binary and find_binary functions
def mock_is_binary(stream, default):
    return isinstance(stream, io.BytesIO)

def mock_find_binary(stream):
    return io.BytesIO(b"example binary data")

# Basic Functionality Tests





def test_invalid_encoding():
    text_stream = io.StringIO("example text")
    with pytest.raises(LookupError):
        _force_correct_text_stream(text_stream, "invalid-encoding", "replace", mock_is_binary, mock_find_binary)

📢 Feedback on this optimization? Discord

Here's the optimized version of your code. I've removed redundant checks and refactored some parts for better performance.



This version of the code minimizes function calls, removes redundant checks, and streamlines the logic without altering the functionality. This should help in improving the overall performance of the program.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 20, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r December 20, 2024 09:54
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