Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 13, 2025

📄 17% (0.17x) speedup for encode_to_base64 in gradio/external_utils.py

⏱️ Runtime : 109 microseconds 92.7 microseconds (best of 97 runs)

📝 Explanation and details

The optimization achieves a 17% speedup by reordering the execution logic to avoid expensive operations when they're not needed.

Key optimization: Fast-path for JSON responses
The original code always performed base64.b64encode(r.content).decode("utf-8") first, regardless of the response type. The optimized version checks content-type header upfront and handles application/json responses directly without any base64 encoding, since JSON responses already contain the pre-encoded blob.

Specific changes:

  • Moved content-type check earlier: Determines response type before expensive operations
  • Eliminated wasteful base64 encoding for JSON: For application/json responses (which appear frequently in the test results), the function now extracts the blob directly from the JSON without encoding r.content
  • Streamlined control flow: Removed the intermediate new_base64 variable and redundant logic branches

Performance impact by test type:

  • JSON responses show dramatic improvements: 63-99% faster for JSON test cases, as they completely skip base64 encoding
  • Non-JSON responses show modest slowdowns: 1-6% slower due to the additional upfront header check, but this is minimal compared to JSON gains
  • Large content benefits: Better performance on large JSON blobs (24-36% faster) due to avoiding unnecessary encoding

Hot path relevance:
Based on the function references, encode_to_base64 is called from custom_post_binary in the Hugging Face model integration pipeline. Given that many HF inference endpoints return JSON responses with pre-encoded blobs, this optimization will significantly benefit common ML model inference workloads where the function processes JSON responses in a loop or batch processing scenario.

The 17% overall speedup represents the weighted average across different response types, with JSON responses (likely common in HF API usage) seeing the most substantial gains.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 53 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 93.3%
🌀 Generated Regression Tests and Runtime
import base64
from types import SimpleNamespace

# function to test
# (copied from prompt)
import httpx
# imports
import pytest  # used for our unit tests
from gradio.external_utils import encode_to_base64

# --- UNIT TESTS ---

# Helper: create a mock httpx.Response object
class MockResponse:
    def __init__(self, content=b"", headers=None, json_data=None):
        self.content = content
        self._headers = headers or {}
        self._json_data = json_data
        self._json_called = False

    @property
    def headers(self):
        return self._headers

    def json(self):
        self._json_called = True
        if callable(self._json_data):
            return self._json_data()
        return self._json_data

# -------------------------
# 1. BASIC TEST CASES
# -------------------------

def test_basic_png_bytes():
    # Basic: PNG image bytes, content-type header set
    png_bytes = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR"
    headers = {"content-type": "image/png"}
    r = MockResponse(content=png_bytes, headers=headers)
    codeflash_output = encode_to_base64(r); result = codeflash_output # 3.00μs -> 3.22μs (6.81% slower)
    expected_b64 = base64.b64encode(png_bytes).decode("utf-8")

def test_basic_jpeg_bytes():
    # Basic: JPEG image bytes, content-type header set
    jpeg_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF"
    headers = {"content-type": "image/jpeg"}
    r = MockResponse(content=jpeg_bytes, headers=headers)
    codeflash_output = encode_to_base64(r); result = codeflash_output # 2.74μs -> 2.90μs (5.56% slower)
    expected_b64 = base64.b64encode(jpeg_bytes).decode("utf-8")

def test_basic_text_bytes():
    # Basic: plain text content, content-type header set
    text = b"Hello, world!"
    headers = {"content-type": "text/plain"}
    r = MockResponse(content=text, headers=headers)
    codeflash_output = encode_to_base64(r); result = codeflash_output # 2.76μs -> 2.85μs (2.99% slower)
    expected_b64 = base64.b64encode(text).decode("utf-8")

def test_already_base64_with_prefix():
    # Basic: content is already a base64 string with data prefix in it
    # Should be returned as is, not re-encoded
    fake_b64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA"
    # Place the prefix inside the base64 string so the check passes
    # The function encodes r.content, so we need to make r.content's b64 encoding include ";base64,"
    # We'll fudge this by making the base64 of r.content include ";base64,"
    # So, base64_repr = base64.b64encode(r.content).decode("utf-8")
    # Let's reverse engineer: base64.b64encode(x).decode("utf-8") == "foo;base64,bar"
    # So we need to set r.content = b"foo;base64,bar"
    content = b"foo;base64,bar"
    headers = {"content-type": "image/png"}
    r = MockResponse(content=content, headers=headers)
    # base64_repr will be 'Zm9vO2Jhc2U2NCxiYXI=' which does not contain ';base64,'
    # So fudge: set content so that base64.b64encode(content) includes ';base64,'
    # Let's use base64.b64decode("c29tZTs7YmFzZTY0LHRleHQ=") == b"some;;base64,text"
    # But that's complex. Instead, let's patch the function for this test.
    # Or, since the function checks for ';base64,' in the base64_repr, let's monkeypatch base64.b64encode for this test.
    # But per instructions, don't mock or stub. So, we can skip this, or test the logic via the json path (see below).
    # Instead, test the JSON path where the blob already includes a prefix.
    blob = "data:image/png;base64,Zm9vYmFy"
    headers = {"content-type": "application/json"}
    json_data = [{"content-type": "image/png", "blob": blob}]
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    codeflash_output = encode_to_base64(r); result = codeflash_output # 3.42μs -> 2.09μs (63.3% faster)

# -------------------------
# 2. EDGE TEST CASES
# -------------------------

def test_no_content_type_header():
    # Edge: No content-type header at all
    content = b"test"
    r = MockResponse(content=content, headers={})
    # content_type will be None, so the output will be 'data:None;base64,<b64>'
    expected_b64 = base64.b64encode(content).decode("utf-8")
    codeflash_output = encode_to_base64(r) # 2.03μs -> 2.13μs (4.73% slower)

def test_content_type_json_missing_keys():
    # Edge: content-type is application/json but missing content-type key in JSON
    headers = {"content-type": "application/json"}
    json_data = [{}]  # missing "content-type" and "blob"
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    with pytest.raises(ValueError) as excinfo:
        encode_to_base64(r) # 4.31μs -> 2.82μs (52.7% faster)

def test_content_type_json_blob_missing():
    # Edge: content-type is application/json but missing blob key in JSON
    headers = {"content-type": "application/json"}
    json_data = [{"content-type": "image/png"}]  # missing "blob"
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    with pytest.raises(ValueError) as excinfo:
        encode_to_base64(r) # 4.43μs -> 2.88μs (54.0% faster)

def test_content_type_json_content_type_missing():
    # Edge: content-type is application/json but missing content-type key in JSON
    headers = {"content-type": "application/json"}
    json_data = [{"blob": "abc"}]  # missing "content-type"
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    with pytest.raises(ValueError) as excinfo:
        encode_to_base64(r) # 4.21μs -> 2.66μs (58.1% faster)

def test_content_type_case_insensitive():
    # Edge: content-type header is mixed case
    content = b"abc"
    headers = {"Content-Type": "text/plain"}
    # The function uses .get("content-type") so this should return None
    # and thus output should be data:None;base64,<b64>
    expected_b64 = base64.b64encode(content).decode("utf-8")
    r = MockResponse(content=content, headers=headers)
    codeflash_output = encode_to_base64(r) # 2.06μs -> 2.13μs (3.38% slower)

def test_empty_content():
    # Edge: Empty content
    content = b""
    headers = {"content-type": "application/octet-stream"}
    r = MockResponse(content=content, headers=headers)
    expected_b64 = ""
    codeflash_output = encode_to_base64(r) # 2.63μs -> 2.68μs (1.64% slower)

def test_content_type_none():
    # Edge: content-type is explicitly None
    content = b"xyz"
    headers = {"content-type": None}
    r = MockResponse(content=content, headers=headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    codeflash_output = encode_to_base64(r) # 2.08μs -> 2.15μs (3.30% slower)


def test_json_returns_empty_list():
    # Edge: JSON returns empty list
    headers = {"content-type": "application/json"}
    json_data = []
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    with pytest.raises(IndexError):
        encode_to_base64(r) # 4.32μs -> 2.36μs (83.4% faster)

def test_content_type_unusual():
    # Edge: Unusual content-type
    content = b"foo"
    headers = {"content-type": "application/x-custom"}
    r = MockResponse(content=content, headers=headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    codeflash_output = encode_to_base64(r) # 1.90μs -> 1.90μs (0.316% slower)

def test_content_type_with_parameters():
    # Edge: Content-type header with parameters (e.g., charset)
    content = b"bar"
    headers = {"content-type": "text/html; charset=utf-8"}
    r = MockResponse(content=content, headers=headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    codeflash_output = encode_to_base64(r) # 1.83μs -> 1.90μs (3.68% slower)

# -------------------------
# 3. LARGE SCALE TEST CASES
# -------------------------

def test_large_binary_content():
    # Large: binary content of 1000 bytes
    content = bytes(range(256)) * 3 + b"\x00" * 232  # 256*3+232 = 1000
    headers = {"content-type": "application/octet-stream"}
    r = MockResponse(content=content, headers=headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    codeflash_output = encode_to_base64(r) # 4.22μs -> 4.19μs (0.884% faster)

def test_large_text_content():
    # Large: text content of 1000 characters
    text = "a" * 1000
    content = text.encode("utf-8")
    headers = {"content-type": "text/plain"}
    r = MockResponse(content=content, headers=headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    codeflash_output = encode_to_base64(r) # 3.58μs -> 3.68μs (2.80% slower)

def test_large_json_blob():
    # Large: JSON with a large base64 blob string
    blob = base64.b64encode(b"x" * 1000).decode("utf-8")
    headers = {"content-type": "application/json"}
    json_data = [{"content-type": "application/octet-stream", "blob": blob}]
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    codeflash_output = encode_to_base64(r); result = codeflash_output # 2.86μs -> 2.30μs (24.2% faster)

def test_many_different_content_types():
    # Large: test with many different content-types and random data
    for i in range(10):
        content = bytes([i] * 100)
        content_type = f"application/x-type-{i}"
        headers = {"content-type": content_type}
        r = MockResponse(content=content, headers=headers)
        expected_b64 = base64.b64encode(content).decode("utf-8")
        codeflash_output = encode_to_base64(r) # 8.85μs -> 8.99μs (1.52% slower)

def test_large_json_list():
    # Large: JSON returns a large list, only first element is used
    blob = base64.b64encode(b"abc").decode("utf-8")
    headers = {"content-type": "application/json"}
    json_data = [{"content-type": "text/plain", "blob": blob}] + [{} for _ in range(999)]
    r = MockResponse(content=b"", headers=headers, json_data=json_data)
    codeflash_output = encode_to_base64(r); result = codeflash_output # 2.71μs -> 2.00μs (35.9% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import base64
from typing import Any

# imports
import pytest  # used for our unit tests
from gradio.external_utils import encode_to_base64


# For mocking httpx.Response
class DummyResponse:
    """
    A minimal mock of httpx.Response for testing encode_to_base64.
    Allows setting .content, .headers, and .json().
    """
    def __init__(self, content: bytes, headers: dict[str, str] = None, json_data: Any = None):
        self.content = content
        self.headers = headers or {}
        self._json_data = json_data

    def json(self):
        if self._json_data is not None:
            return self._json_data
        raise ValueError("No JSON data set for DummyResponse")
from gradio.external_utils import encode_to_base64

# unit tests

# ---------------- BASIC TEST CASES ----------------

def test_basic_png_image():
    # Test encoding a simple PNG image content
    content = b"\x89PNG\r\n\x1a\n"  # PNG file header
    headers = {"content-type": "image/png"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:image/png;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.68μs -> 1.77μs (5.26% slower)

def test_basic_jpeg_image():
    # Test encoding a simple JPEG image content
    content = b"\xff\xd8\xff\xe0"  # JPEG file header
    headers = {"content-type": "image/jpeg"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:image/jpeg;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.64μs -> 1.72μs (4.83% slower)

def test_basic_text_plain():
    # Test encoding plain text content
    text = "Hello, world!"
    content = text.encode("utf-8")
    headers = {"content-type": "text/plain"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:text/plain;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.73μs -> 1.68μs (3.28% faster)

def test_basic_binary_data():
    # Test encoding generic binary content
    content = b"\x00\x01\x02\x03\x04"
    headers = {"content-type": "application/octet-stream"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:application/octet-stream;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.74μs -> 1.71μs (1.70% faster)

# ---------------- EDGE TEST CASES ----------------

def test_empty_content():
    # Test with empty content
    content = b""
    headers = {"content-type": "application/octet-stream"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:application/octet-stream;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.57μs -> 1.55μs (1.62% faster)

def test_missing_content_type_header():
    # Test when 'content-type' header is missing
    content = b"abc"
    resp = DummyResponse(content, headers={})
    # content_type will be None
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:None;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.81μs -> 1.83μs (1.58% slower)

def test_content_type_application_json_with_blob():
    # Test when content-type is 'application/json' and blob is present
    blob_b64 = "Zm9vYmFy"  # base64 for 'foobar'
    json_data = [{
        "content-type": "image/png",
        "blob": blob_b64
    }]
    resp = DummyResponse(b"", headers={"content-type": "application/json"}, json_data=json_data)
    expected = f"data:image/png;base64,{blob_b64}"
    codeflash_output = encode_to_base64(resp) # 3.20μs -> 1.60μs (99.6% faster)

def test_content_type_application_json_missing_blob():
    # Test when content-type is 'application/json' but 'blob' key is missing
    json_data = [{
        "content-type": "image/png"
        # "blob" key missing
    }]
    resp = DummyResponse(b"", headers={"content-type": "application/json"}, json_data=json_data)
    with pytest.raises(ValueError):
        encode_to_base64(resp) # 4.12μs -> 2.56μs (60.6% faster)

def test_content_type_application_json_missing_content_type():
    # Test when content-type is 'application/json' but 'content-type' key is missing in JSON
    json_data = [{
        "blob": "Zm9vYmFy"
        # "content-type" key missing
    }]
    resp = DummyResponse(b"", headers={"content-type": "application/json"}, json_data=json_data)
    with pytest.raises(ValueError):
        encode_to_base64(resp) # 3.98μs -> 2.41μs (65.1% faster)

def test_base64_already_contains_data_prefix():
    # Test when base64_repr already includes data prefix
    # This should be returned as is
    fake_b64 = "data:image/png;base64,iVBORw0KGgo="
    # When content is the decoded base64, the function will re-encode it, but if
    # ";base64," is in the result, it returns that string.
    # So, we force the content to be b"data:image/png;base64,iVBORw0KGgo="
    # But base64.b64encode will not contain ';base64,' unless the content itself does.
    # Instead, we simulate the scenario by patching the function to test this directly.
    # But since we cannot patch, let's test with content that encodes to string containing ';base64,'
    # Find a string that encodes to a base64 string containing ";base64,"
    # For simplicity, we can directly test the branch:
    content = b";base64,"
    headers = {"content-type": "image/png"}
    resp = DummyResponse(content, headers)
    # base64.b64encode(b";base64,") == 'O2Jhc2U2NCw='
    # That doesn't contain ';base64,'
    # Instead, we can simulate the scenario by using content that encodes to a string containing ';base64,'
    # But that's unlikely for normal content. Let's test with the actual string.
    # Instead, let's test the function with a content that is already a base64 string with the prefix.
    # We'll simulate that by overriding the function for this test.
    # But since we can't patch, we skip this edge case as it's not reachable for normal content.

def test_non_utf8_bytes():
    # Test with bytes that are not valid UTF-8 (should still encode fine)
    content = b"\xff\xfe\xfd\xfc"
    headers = {"content-type": "application/octet-stream"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:application/octet-stream;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 1.81μs -> 1.75μs (3.37% faster)


def test_json_response_empty_list():
    # Test when .json() returns an empty list
    json_data = []
    resp = DummyResponse(b"", headers={"content-type": "application/json"}, json_data=json_data)
    # Should raise IndexError because r.json()[0] will fail
    with pytest.raises(IndexError):
        encode_to_base64(resp) # 3.84μs -> 2.04μs (87.9% faster)

# ---------------- LARGE SCALE TEST CASES ----------------

def test_large_binary_content():
    # Test with large binary content (999 bytes)
    content = b"A" * 999
    headers = {"content-type": "application/octet-stream"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:application/octet-stream;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 3.78μs -> 3.60μs (4.97% faster)

def test_large_text_content():
    # Test with large text content (999 chars)
    text = "hello" * 199  # 995 chars
    text += "abcd"  # 999 chars
    content = text.encode("utf-8")
    headers = {"content-type": "text/plain"}
    resp = DummyResponse(content, headers)
    expected_b64 = base64.b64encode(content).decode("utf-8")
    expected = f"data:text/plain;base64,{expected_b64}"
    codeflash_output = encode_to_base64(resp) # 3.79μs -> 3.78μs (0.212% faster)

def test_large_json_blob():
    # Test with large base64 blob in JSON
    large_blob = base64.b64encode(b"X" * 999).decode("utf-8")
    json_data = [{
        "content-type": "image/png",
        "blob": large_blob
    }]
    resp = DummyResponse(b"", headers={"content-type": "application/json"}, json_data=json_data)
    expected = f"data:image/png;base64,{large_blob}"
    codeflash_output = encode_to_base64(resp) # 2.46μs -> 1.84μs (33.8% faster)

def test_many_different_content_types():
    # Test with many different content types
    for content_type in [
        "image/png", "image/jpeg", "image/gif", "application/pdf", "text/html",
        "application/zip", "audio/mpeg", "video/mp4", "application/xml", "application/json"
    ]:
        content = b"testdata"
        headers = {"content-type": content_type}
        resp = DummyResponse(content, headers)
        if content_type == "application/json":
            # For application/json, must provide blob and content-type keys in JSON
            json_data = [{
                "content-type": "image/png",
                "blob": base64.b64encode(content).decode("utf-8")
            }]
            resp = DummyResponse(b"", headers=headers, json_data=json_data)
            expected = f"data:image/png;base64,{base64.b64encode(content).decode('utf-8')}"
        else:
            expected = f"data:{content_type};base64,{base64.b64encode(content).decode('utf-8')}"
        codeflash_output = encode_to_base64(resp) # 7.57μs -> 7.07μs (7.13% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-encode_to_base64-mhws3q0o and push.

Codeflash Static Badge

The optimization achieves a 17% speedup by **reordering the execution logic** to avoid expensive operations when they're not needed.

**Key optimization: Fast-path for JSON responses**
The original code always performed `base64.b64encode(r.content).decode("utf-8")` first, regardless of the response type. The optimized version checks `content-type` header upfront and handles `application/json` responses directly without any base64 encoding, since JSON responses already contain the pre-encoded blob.

**Specific changes:**
- **Moved content-type check earlier**: Determines response type before expensive operations
- **Eliminated wasteful base64 encoding for JSON**: For `application/json` responses (which appear frequently in the test results), the function now extracts the blob directly from the JSON without encoding `r.content`
- **Streamlined control flow**: Removed the intermediate `new_base64` variable and redundant logic branches

**Performance impact by test type:**
- **JSON responses show dramatic improvements**: 63-99% faster for JSON test cases, as they completely skip base64 encoding
- **Non-JSON responses show modest slowdowns**: 1-6% slower due to the additional upfront header check, but this is minimal compared to JSON gains
- **Large content benefits**: Better performance on large JSON blobs (24-36% faster) due to avoiding unnecessary encoding

**Hot path relevance:**
Based on the function references, `encode_to_base64` is called from `custom_post_binary` in the Hugging Face model integration pipeline. Given that many HF inference endpoints return JSON responses with pre-encoded blobs, this optimization will significantly benefit common ML model inference workloads where the function processes JSON responses in a loop or batch processing scenario.

The 17% overall speedup represents the weighted average across different response types, with JSON responses (likely common in HF API usage) seeing the most substantial gains.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 13, 2025 01:57
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Nov 13, 2025
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 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant