Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 28, 2025

📄 7% (0.07x) speedup for update_example_values_to_use_public_url in gradio/route_utils.py

⏱️ Runtime : 1.69 milliseconds 1.58 milliseconds (best of 60 runs)

📝 Explanation and details

Optimizations made:

  • Cached client_utils function lookups (is_file_obj_with_url, is_http_url_like, and traverse) to local variables to avoid repeated attribute lookups, improving runtime especially in large traversals.
  • Used .get("url") instead of direct ["url"] access to avoid potential KeyErrors, matching the performance profile of the original's use for other keys.
  • Preserved all logic, comments, and code structure per your instructions—no behavioral change.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 34 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import copy
# Patch the client_utils module expected by the function
import types

# imports
import pytest  # used for our unit tests
from gradio.route_utils import update_example_values_to_use_public_url

# ---- MOCKS for gradio_client.utils ----
# Since we cannot import actual gradio_client.utils, provide minimal mocks.

def is_file_obj_with_url(obj):
    # Checks if obj is a dict with a 'url' key (simulates file object with url)
    return isinstance(obj, dict) and "url" in obj

def is_http_url_like(url):
    # Very basic check for http(s) url
    return isinstance(url, str) and (url.startswith("http://") or url.startswith("https://"))

def traverse(obj, fn, cond):
    # Recursively traverse dicts/lists, applying fn if cond is True
    if isinstance(obj, dict):
        if cond(obj):
            obj = fn(obj)
        return {k: traverse(v, fn, cond) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [traverse(x, fn, cond) for x in obj]
    else:
        return obj

client_utils = types.SimpleNamespace()
client_utils.is_file_obj_with_url = is_file_obj_with_url
client_utils.is_http_url_like = is_http_url_like
client_utils.traverse = traverse
from gradio.route_utils import update_example_values_to_use_public_url

# ------------------- UNIT TESTS -------------------

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

def test_basic_already_public_url():
    # parameter_default already has a public URL; should remain unchanged
    api_info = {
        "input": {
            "parameter_default": {"url": "https://public.com/file.jpg"},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.62μs -> 3.64μs (0.440% slower)
    # Should not be replaced

def test_basic_non_public_url_replaced():
    # parameter_default has a non-public URL; should be replaced with example_input's URL
    api_info = {
        "input": {
            "parameter_default": {"url": "/private/file.jpg"},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.53μs -> 3.45μs (2.29% faster)

def test_basic_no_parameter_default():
    # No parameter_default key; should remain unchanged
    api_info = {
        "input": {
            "not_a_param": 123,
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.52μs -> 3.75μs (5.97% slower)

def test_basic_parameter_default_not_file_obj():
    # parameter_default is not a dict with "url"; should remain unchanged
    api_info = {
        "input": {
            "parameter_default": 42,
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.35μs -> 3.45μs (2.84% slower)

def test_basic_parameter_default_is_none():
    # parameter_default is None; should remain unchanged
    api_info = {
        "input": {
            "parameter_default": None,
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 2.54μs -> 2.87μs (11.2% slower)

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




def test_edge_parameter_default_url_is_none():
    # parameter_default is a dict but url is None; should replace with example_input url
    api_info = {
        "input": {
            "parameter_default": {"url": None},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.67μs -> 3.70μs (0.730% slower)

def test_edge_parameter_default_url_is_empty_string():
    # parameter_default is a dict but url is empty string; should replace with example_input url
    api_info = {
        "input": {
            "parameter_default": {"url": ""},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.37μs -> 3.31μs (1.87% faster)

def test_edge_parameter_default_url_is_http_but_not_https():
    # parameter_default is a dict with "http://" url; should remain unchanged
    api_info = {
        "input": {
            "parameter_default": {"url": "http://public.com/file.jpg"},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.29μs -> 3.37μs (2.37% slower)

def test_edge_parameter_default_url_is_not_a_string():
    # parameter_default is a dict with url not a string; should replace with example_input url
    api_info = {
        "input": {
            "parameter_default": {"url": 12345},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.23μs -> 3.28μs (1.50% slower)

def test_edge_multiple_levels_of_nesting():
    # parameter_default is nested inside multiple levels
    api_info = {
        "level1": {
            "level2": {
                "parameter_default": {"url": "/private/file.jpg"},
                "example_input": {"url": "https://public.com/example.jpg"}
            }
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.59μs -> 3.71μs (3.21% slower)

def test_edge_list_of_inputs():
    # parameter_default appears inside a list
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": "/private/file1.jpg"},
                "example_input": {"url": "https://public.com/example1.jpg"}
            },
            {
                "parameter_default": {"url": "https://public.com/file2.jpg"},
                "example_input": {"url": "https://public.com/example2.jpg"}
            }
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 4.58μs -> 4.64μs (1.21% slower)

def test_edge_parameter_default_url_is_https_with_query():
    # parameter_default is a dict with "https://" url with query params; should remain unchanged
    api_info = {
        "input": {
            "parameter_default": {"url": "https://public.com/file.jpg?token=abc"},
            "example_input": {"url": "https://public.com/example.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 3.19μs -> 3.27μs (2.69% slower)

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

def test_large_many_inputs():
    # Test with a large number of input dicts in a list
    N = 500
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": f"/private/file{i}.jpg"},
                "example_input": {"url": f"https://public.com/example{i}.jpg"}
            }
            for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 125μs -> 113μs (10.0% faster)
    for i in range(N):
        pass

def test_large_deeply_nested():
    # Test with a deeply nested structure
    N = 50
    api_info = current = {}
    for i in range(N):
        current["level"] = {}
        current = current["level"]
    current["parameter_default"] = {"url": "/private/file.jpg"}
    current["example_input"] = {"url": "https://public.com/example.jpg"}
    # Should traverse all levels and update the innermost parameter_default
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 14.6μs -> 14.0μs (3.93% faster)
    # Traverse down to the deepest level
    current = result
    for i in range(N):
        current = current["level"]

def test_large_mixed_types():
    # Test with a mix of lists, dicts, and irrelevant types
    N = 100
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": f"/private/file{i}.jpg"},
                "example_input": {"url": f"https://public.com/example{i}.jpg"},
                "extra": [None, 123, {"foo": "bar"}]
            }
            if i % 2 == 0 else
            {
                "not_a_param": 123,
                "example_input": {"url": f"https://public.com/example{i}.jpg"},
                "extra": [None, 123, {"foo": "bar"}]
            }
            for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 105μs -> 101μs (3.57% faster)
    for i in range(N):
        if i % 2 == 0:
            pass
        else:
            pass

def test_large_parameter_default_file_obj_with_public_url():
    # All parameter_defaults already have public urls; should remain unchanged
    N = 200
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": f"https://public.com/file{i}.jpg"},
                "example_input": {"url": f"https://public.com/example{i}.jpg"}
            }
            for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 53.5μs -> 48.7μs (9.77% faster)
    for i in range(N):
        pass

def test_large_no_parameter_defaults():
    # Large structure with no parameter_defaults; should remain unchanged
    N = 300
    api_info = {
        "inputs": [
            {
                "not_a_param": {"url": f"/private/file{i}.jpg"},
                "example_input": {"url": f"https://public.com/example{i}.jpg"}
            }
            for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(copy.deepcopy(api_info)); result = codeflash_output # 286μs -> 283μs (0.841% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from copy import deepcopy

# imports
import pytest  # used for our unit tests
from gradio.route_utils import update_example_values_to_use_public_url


# --- Begin function to test ---
# Simulate gradio_client.utils for test purposes
def is_file_obj_with_url(obj):
    # Returns True if obj is a dict with a 'url' key
    return isinstance(obj, dict) and "url" in obj

def is_http_url_like(url):
    # Returns True if url is a string that starts with 'http://' or 'https://'
    return isinstance(url, str) and (url.startswith("http://") or url.startswith("https://"))

def traverse(obj, fn, filter_fn):
    # Recursively traverse obj, applying fn to dicts where filter_fn is True
    if isinstance(obj, dict):
        if filter_fn(obj):
            obj = fn(obj)
        return {k: traverse(v, fn, filter_fn) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [traverse(v, fn, filter_fn) for v in obj]
    else:
        return obj
from gradio.route_utils import \
    update_example_values_to_use_public_url  # --- End function to test ---

# --- Begin unit tests ---

# Basic Test Cases

def test_basic_http_url_remains_unchanged():
    # If parameter_default url is already public, it should not change
    api_info = {
        "input": {
            "parameter_default": {"url": "https://public.com/image.jpg"},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.55μs -> 3.62μs (2.04% slower)

def test_basic_non_http_url_gets_replaced():
    # If parameter_default url is not public, it should be replaced by example_input url
    api_info = {
        "input": {
            "parameter_default": {"url": "/private/image.jpg"},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    expected = {
        "input": {
            "parameter_default": {"url": "https://public.com/image.jpg"},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.27μs -> 3.41μs (3.85% slower)

def test_basic_no_parameter_default():
    # If parameter_default is missing, nothing should change
    api_info = {
        "input": {
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.08μs -> 3.14μs (1.91% slower)

def test_basic_parameter_default_not_a_file_obj():
    # If parameter_default is not a dict with url, nothing should change
    api_info = {
        "input": {
            "parameter_default": "some string",
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.28μs -> 3.37μs (2.70% slower)

def test_basic_parameter_default_url_is_none():
    # If parameter_default url is None, nothing should change
    api_info = {
        "input": {
            "parameter_default": {"url": None},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    expected = {
        "input": {
            "parameter_default": {"url": "https://public.com/image.jpg"},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.34μs -> 3.34μs (0.000% faster)

# Edge Test Cases


def test_edge_parameter_default_is_none():
    # If parameter_default is None, nothing should change
    api_info = {
        "input": {
            "parameter_default": None,
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 2.89μs -> 3.10μs (6.74% slower)

def test_edge_parameter_default_url_is_empty_string():
    # If parameter_default url is empty string, should be replaced
    api_info = {
        "input": {
            "parameter_default": {"url": ""},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    expected = {
        "input": {
            "parameter_default": {"url": "https://public.com/image.jpg"},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.26μs -> 3.46μs (5.83% slower)

def test_edge_parameter_default_url_is_relative_path():
    # If parameter_default url is a relative path, should be replaced
    api_info = {
        "input": {
            "parameter_default": {"url": "images/test.jpg"},
            "example_input": {"url": "https://public.com/test.jpg"}
        }
    }
    expected = {
        "input": {
            "parameter_default": {"url": "https://public.com/test.jpg"},
            "example_input": {"url": "https://public.com/test.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.22μs -> 3.22μs (0.124% slower)

def test_edge_parameter_default_url_is_unusual_string():
    # If parameter_default url is an unusual string, should be replaced
    api_info = {
        "input": {
            "parameter_default": {"url": "ftp://somewhere.com/file"},
            "example_input": {"url": "https://public.com/file"}
        }
    }
    expected = {
        "input": {
            "parameter_default": {"url": "https://public.com/file"},
            "example_input": {"url": "https://public.com/file"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.31μs -> 3.18μs (4.15% faster)

def test_edge_nested_dicts():
    # Test nested structure
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": "/private/image1.jpg"},
                "example_input": {"url": "https://public.com/image1.jpg"}
            },
            {
                "parameter_default": {"url": "https://public.com/image2.jpg"},
                "example_input": {"url": "https://public.com/image2.jpg"}
            }
        ]
    }
    expected = {
        "inputs": [
            {
                "parameter_default": {"url": "https://public.com/image1.jpg"},
                "example_input": {"url": "https://public.com/image1.jpg"}
            },
            {
                "parameter_default": {"url": "https://public.com/image2.jpg"},
                "example_input": {"url": "https://public.com/image2.jpg"}
            }
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 4.58μs -> 4.46μs (2.51% faster)

def test_edge_multiple_levels_of_nesting():
    # Test deeply nested structure
    api_info = {
        "outer": {
            "middle": {
                "inner": {
                    "parameter_default": {"url": "/private/image.jpg"},
                    "example_input": {"url": "https://public.com/image.jpg"}
                }
            }
        }
    }
    expected = {
        "outer": {
            "middle": {
                "inner": {
                    "parameter_default": {"url": "https://public.com/image.jpg"},
                    "example_input": {"url": "https://public.com/image.jpg"}
                }
            }
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.77μs -> 3.77μs (0.027% slower)

def test_edge_parameter_default_url_is_integer():
    # If parameter_default url is an integer, should be replaced
    api_info = {
        "input": {
            "parameter_default": {"url": 12345},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    expected = {
        "input": {
            "parameter_default": {"url": "https://public.com/image.jpg"},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.28μs -> 3.21μs (2.15% faster)

def test_edge_parameter_default_is_empty_dict():
    # If parameter_default is empty dict, nothing should change
    api_info = {
        "input": {
            "parameter_default": {},
            "example_input": {"url": "https://public.com/image.jpg"}
        }
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 3.29μs -> 3.35μs (1.88% slower)


def test_large_scale_many_inputs():
    # Test with a large number of input dicts
    N = 500  # Keep under 1000 for performance
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": f"/private/img_{i}.jpg"},
                "example_input": {"url": f"https://public.com/img_{i}.jpg"}
            } for i in range(N)
        ]
    }
    expected = {
        "inputs": [
            {
                "parameter_default": {"url": f"https://public.com/img_{i}.jpg"},
                "example_input": {"url": f"https://public.com/img_{i}.jpg"}
            } for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 124μs -> 113μs (9.65% faster)

def test_large_scale_mixed_inputs():
    # Test with a mix of public and non-public urls
    N = 200
    api_info = {
        "inputs": [
            {
                "parameter_default": {"url": f"https://public.com/img_{i}.jpg" if i % 2 == 0 else f"/private/img_{i}.jpg"},
                "example_input": {"url": f"https://public.com/img_{i}.jpg"}
            } for i in range(N)
        ]
    }
    expected = {
        "inputs": [
            {
                "parameter_default": {"url": f"https://public.com/img_{i}.jpg"},
                "example_input": {"url": f"https://public.com/img_{i}.jpg"}
            } for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 53.9μs -> 48.7μs (10.6% faster)

def test_large_scale_deeply_nested():
    # Test with a large, deeply nested structure
    N = 50
    api_info = {
        "level1": [
            {
                "level2": [
                    {
                        "parameter_default": {"url": f"/private/img_{i}_{j}.jpg"},
                        "example_input": {"url": f"https://public.com/img_{i}_{j}.jpg"}
                    } for j in range(N)
                ]
            } for i in range(N)
        ]
    }
    expected = {
        "level1": [
            {
                "level2": [
                    {
                        "parameter_default": {"url": f"https://public.com/img_{i}_{j}.jpg"},
                        "example_input": {"url": f"https://public.com/img_{i}_{j}.jpg"}
                    } for j in range(N)
                ]
            } for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 672μs -> 605μs (11.1% faster)

def test_large_scale_no_parameter_defaults():
    # Test with a large number of dicts missing parameter_default
    N = 300
    api_info = {
        "inputs": [
            {
                "example_input": {"url": f"https://public.com/img_{i}.jpg"}
            } for i in range(N)
        ]
    }
    codeflash_output = update_example_values_to_use_public_url(deepcopy(api_info)); result = codeflash_output # 170μs -> 166μs (2.74% faster)

To edit these changes git checkout codeflash/optimize-update_example_values_to_use_public_url-mhao7m5n and push.

Codeflash

**Optimizations made:**
- Cached `client_utils` function lookups (`is_file_obj_with_url`, `is_http_url_like`, and `traverse`) to local variables to avoid repeated attribute lookups, improving runtime especially in large traversals.
- Used `.get("url")` instead of direct `["url"]` access to avoid potential KeyErrors, matching the performance profile of the original's use for other keys.
- Preserved all logic, comments, and code structure per your instructions—no behavioral change.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 28, 2025 14:37
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 28, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant