Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 828% (8.28x) speedup for assert_configs_are_equivalent_besides_ids in gradio/utils.py

⏱️ Runtime : 60.0 milliseconds 6.46 milliseconds (best of 11 runs)

📝 Explanation and details

The optimized code achieves an 828% speedup through three key performance improvements:

1. O(1) Component Lookup (Major Impact)

  • Original: Used filter() to find components by ID, resulting in O(n) linear search for each lookup
  • Optimized: Pre-builds id->component dictionaries once, enabling O(1) hash table lookups
  • Impact: In the line profiler, assert_same_components calls dropped from ~120ms total to ~1.7ms total - a massive reduction

2. Eliminated Unnecessary Deep Copies

  • Original: Called copy.deepcopy() on entire configs upfront, then again on components during comparison
  • Optimized: Leverages the fact that json.loads(json.dumps()) already creates fresh objects, only using shallow .copy() when needed for mutation protection
  • Impact: Removed 89ms of deep copy overhead (22.3% of original runtime)

3. Reduced Dictionary Mutations

  • Original: Used .pop() directly on dependency dictionaries, requiring defensive deep copies
  • Optimized: Creates shallow copies of dependency dictionaries before mutation, avoiding the need for expensive deep copies
  • Impact: Dependencies processing became more efficient while preserving the same comparison logic

Performance Characteristics:

  • Small configs: 50-110% faster (most test cases)
  • Large configs: Dramatic improvements - 1819% faster for 500 components, 865% for nested layouts
  • Component-heavy workloads: Benefits scale with number of components due to O(1) vs O(n) lookup difference

The optimization is particularly effective for Gradio's use case where configs often contain many components that need cross-referencing during validation, making the O(1) lookup transformation the primary performance driver.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 154 Passed
🌀 Generated Regression Tests 31 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_blocks.py::TestBlocksMethods.test_load_from_config 1.08ms 293μs 268%✅
test_interfaces.py::TestTabbedInterface.test_tabbed_interface_config_matches_manual_tab 5.01ms 812μs 517%✅
test_utils.py::test_assert_configs_are_equivalent 3.55ms 779μs 355%✅
🌀 Generated Regression Tests and Runtime
import copy
import json

# imports
import pytest
from gradio.utils import assert_configs_are_equivalent_besides_ids

# unit tests

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

def test_equivalent_simple_configs():
    # Two configs with same structure and values except for ids
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "text", "label": "Name"}],
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 42, "type": "text", "label": "Name"}],
    }
    # Should not raise
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 46.2μs -> 26.6μs (73.9% faster)

def test_equivalent_with_multiple_components():
    # Two configs with multiple components, ids are different
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "text", "label": "Name"},
            {"id": 2, "type": "number", "label": "Age"},
        ],
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "text", "label": "Name"},
            {"id": 20, "type": "number", "label": "Age"},
        ],
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 42.7μs -> 27.9μs (53.0% faster)

def test_equivalent_with_layout_and_children():
    # Configs with layout and nested children, ids differ
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "container"},
            {"id": 2, "type": "text", "label": "A"},
            {"id": 3, "type": "text", "label": "B"},
        ],
        "layout": {
            "children": [
                {"id": 1, "children": [
                    {"id": 2},
                    {"id": 3}
                ]}
            ]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "container"},
            {"id": 20, "type": "text", "label": "A"},
            {"id": 30, "type": "text", "label": "B"},
        ],
        "layout": {
            "children": [
                {"id": 10, "children": [
                    {"id": 20},
                    {"id": 30}
                ]}
            ]
        }
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 93.2μs -> 42.2μs (121% faster)

def test_equivalent_with_dependencies():
    # Configs with dependencies, ids differ
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "button"},
            {"id": 2, "type": "text"},
        ],
        "dependencies": [
            {
                "targets": [[1]],
                "inputs": [2],
                "outputs": [1],
                "trigger": "click"
            }
        ]
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 100, "type": "button"},
            {"id": 200, "type": "text"},
        ],
        "dependencies": [
            {
                "targets": [[100]],
                "inputs": [200],
                "outputs": [100],
                "trigger": "click"
            }
        ]
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 84.6μs -> 39.3μs (115% faster)

def test_equivalent_with_extra_root_keys():
    # Only keys in root_keys are compared, so other keys can differ
    config1 = {
        "mode": "blocks",
        "version": "1.0",
        "components": [{"id": 1, "type": "text"}],
    }
    config2 = {
        "mode": "blocks",
        "version": "2.0",
        "components": [{"id": 2, "type": "text"}],
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 36.7μs -> 24.4μs (50.6% faster)

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

def test_different_modes_raises():
    # Should raise ValueError if root key "mode" differs
    config1 = {"mode": "blocks", "components": [{"id": 1, "type": "text"}]}
    config2 = {"mode": "interface", "components": [{"id": 2, "type": "text"}]}
    with pytest.raises(ValueError, match="Configs have different: mode"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 36.2μs -> 22.9μs (58.2% faster)

def test_different_number_of_components_raises():
    # Should raise if number of components differs
    config1 = {"mode": "blocks", "components": [{"id": 1, "type": "text"}]}
    config2 = {"mode": "blocks", "components": [
        {"id": 2, "type": "text"},
        {"id": 3, "type": "number"}
    ]}
    with pytest.raises(ValueError, match="# of components are different"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 36.8μs -> 24.6μs (49.3% faster)

def test_missing_component_id_raises():
    # Should raise if a referenced child id does not exist in components
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "container"}],
        "layout": {
            "children": [{"id": 1, "children": [{"id": 2}]}]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 10, "type": "container"}],
        "layout": {
            "children": [{"id": 10, "children": [{"id": 20}]}]
        }
    }
    # No component with id 2 in config1 or 20 in config2
    with pytest.raises(ValueError, match="Could not find component with id 2"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 65.8μs -> 33.8μs (94.7% faster)


def test_layout_missing_in_one_config_raises():
    # Should raise if layout present in one config but not the other
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "text"}],
        "layout": {"children": [{"id": 1}]}
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 2, "type": "text"}],
        # No layout key
    }
    with pytest.raises(ValueError, match="layout key"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 47.3μs -> 29.2μs (62.0% faster)

def test_dependencies_missing_in_one_config_raises():
    # Should raise if dependencies present in one config but not the other
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "text"}],
        "dependencies": []
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 2, "type": "text"}],
        # No dependencies key
    }
    with pytest.raises(ValueError, match="dependencies key"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 38.9μs -> 23.7μs (64.2% faster)

def test_dependency_targets_inputs_outputs_differ_raises():
    # Should raise if dependency targets/inputs/outputs differ
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "button"},
            {"id": 2, "type": "text"},
        ],
        "dependencies": [
            {
                "targets": [[1]],
                "inputs": [2],
                "outputs": [1],
                "trigger": "click"
            }
        ]
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "button"},
            {"id": 20, "type": "text"},
        ],
        "dependencies": [
            {
                "targets": [[10]],
                "inputs": [999],  # different id
                "outputs": [10],
                "trigger": "click"
            }
        ]
    }
    with pytest.raises(ValueError, match="Could not find component with id 999"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 73.4μs -> 37.7μs (94.5% faster)

def test_dependency_other_fields_differ_raises():
    # Should raise if dependency fields (besides targets/inputs/outputs) differ
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "button"},
            {"id": 2, "type": "text"},
        ],
        "dependencies": [
            {
                "targets": [[1]],
                "inputs": [2],
                "outputs": [1],
                "trigger": "click"
            }
        ]
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "button"},
            {"id": 20, "type": "text"},
        ],
        "dependencies": [
            {
                "targets": [[10]],
                "inputs": [20],
                "outputs": [10],
                "trigger": "change"  # different trigger
            }
        ]
    }
    with pytest.raises(ValueError, match="does not match"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 82.2μs -> 38.3μs (115% faster)

def test_tuple_conversion_to_list():
    # Should treat tuples and lists as equivalent after json roundtrip
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "tuple", "values": (1, 2)}]
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 2, "type": "tuple", "values": [1, 2]}]
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 42.3μs -> 25.4μs (66.6% faster)

def test_empty_layout_and_dependencies():
    # Should handle empty layout and dependencies gracefully
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "text"}],
        "layout": {"children": []},
        "dependencies": []
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 2, "type": "text"}],
        "layout": {"children": []},
        "dependencies": []
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 45.1μs -> 26.8μs (68.4% faster)

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

def test_large_number_of_components_equivalent():
    # Test with 500 components, ids offset by 1000
    n = 500
    config1 = {
        "mode": "blocks",
        "components": [{"id": i, "type": "text", "label": f"Field{i}"} for i in range(n)],
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": i+1000, "type": "text", "label": f"Field{i}"} for i in range(n)],
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 2.50ms -> 750μs (233% faster)

def test_large_nested_layout():
    # Deeply nested layout with 100 levels
    n = 100
    components1 = [{"id": i, "type": "container"} for i in range(n)]
    components2 = [{"id": i+1000, "type": "container"} for i in range(n)]
    # Build nested children structure
    children1 = []
    children2 = []
    for i in reversed(range(n)):
        children1 = [{"id": i, "children": children1}]
        children2 = [{"id": i+1000, "children": children2}]
    config1 = {
        "mode": "blocks",
        "components": components1,
        "layout": {"children": children1}
    }
    config2 = {
        "mode": "blocks",
        "components": components2,
        "layout": {"children": children2}
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 2.15ms -> 332μs (548% faster)
import copy
import json

# imports
import pytest
from gradio.utils import assert_configs_are_equivalent_besides_ids

# unit tests

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

def test_identical_minimal_configs():
    # Both configs are identical except for component ids
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "textbox", "label": "Name"}],
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 99, "type": "textbox", "label": "Name"}],
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 56.0μs -> 26.8μs (109% faster)

def test_different_component_ids_but_equivalent():
    # Different ids, but otherwise equivalent
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "textbox", "label": "First"},
            {"id": 2, "type": "slider", "min": 0, "max": 10},
        ],
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "textbox", "label": "First"},
            {"id": 20, "type": "slider", "min": 0, "max": 10},
        ],
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 47.6μs -> 28.3μs (68.5% faster)

def test_equivalent_with_layout_and_nested_children():
    # Layout with nested children, ids differ
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "row"},
            {"id": 2, "type": "textbox", "label": "A"},
            {"id": 3, "type": "slider", "min": 0, "max": 1},
        ],
        "layout": {
            "children": [
                {
                    "id": 1,
                    "children": [
                        {"id": 2},
                        {"id": 3}
                    ]
                }
            ]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 11, "type": "row"},
            {"id": 22, "type": "textbox", "label": "A"},
            {"id": 33, "type": "slider", "min": 0, "max": 1},
        ],
        "layout": {
            "children": [
                {
                    "id": 11,
                    "children": [
                        {"id": 22},
                        {"id": 33}
                    ]
                }
            ]
        }
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 94.9μs -> 40.7μs (133% faster)

def test_equivalent_with_dependencies():
    # Configs with dependencies, ids differ
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "textbox"},
            {"id": 2, "type": "slider"}
        ],
        "dependencies": [
            {
                "targets": [[1]],
                "inputs": [2],
                "outputs": [1],
                "trigger": "change"
            }
        ]
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "textbox"},
            {"id": 20, "type": "slider"}
        ],
        "dependencies": [
            {
                "targets": [[10]],
                "inputs": [20],
                "outputs": [10],
                "trigger": "change"
            }
        ]
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 78.5μs -> 37.9μs (107% faster)

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

def test_different_mode_raises():
    # Different root key (mode) should raise
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "textbox"}],
    }
    config2 = {
        "mode": "interface",
        "components": [{"id": 2, "type": "textbox"}],
    }
    with pytest.raises(ValueError, match="Configs have different: mode"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 33.9μs -> 20.8μs (63.0% faster)

def test_different_components_length_raises():
    # Different number of components should raise
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "textbox"},
            {"id": 2, "type": "slider"}
        ]
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "textbox"}
        ]
    }
    with pytest.raises(ValueError, match="# of components are different"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 38.5μs -> 23.3μs (65.4% faster)

def test_missing_component_id_raises():
    # Layout refers to component id that doesn't exist
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "row"}],
        "layout": {
            "children": [{"id": 1}, {"id": 2}]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 10, "type": "row"}],
        "layout": {
            "children": [{"id": 10}, {"id": 20}]
        }
    }
    with pytest.raises(ValueError, match="Could not find component with id 2"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 59.8μs -> 32.9μs (81.8% faster)


def test_layout_key_missing_in_one_config_raises():
    # Layout key present in one config but not the other
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "row"}],
        "layout": {"children": [{"id": 1}]}
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 2, "type": "row"}]
        # No layout key
    }
    with pytest.raises(ValueError, match="The first config has a layout key, but the second does not"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 46.2μs -> 28.9μs (60.0% faster)

def test_dependencies_key_missing_in_one_config_raises():
    # Dependencies key present in one config but not the other
    config1 = {
        "mode": "blocks",
        "components": [{"id": 1, "type": "textbox"}],
        "dependencies": [
            {
                "targets": [[1]],
                "inputs": [1],
                "outputs": [1],
                "trigger": "change"
            }
        ]
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": 2, "type": "textbox"}]
        # No dependencies key
    }
    with pytest.raises(ValueError, match="The first config has a dependencies key, but the second does not"):
        assert_configs_are_equivalent_besides_ids(config1, config2) # 47.1μs -> 27.9μs (68.7% faster)

def test_nested_children_and_missing_children_key():
    # Children key missing in one child but not the other (should treat as empty)
    config1 = {
        "mode": "blocks",
        "components": [
            {"id": 1, "type": "row"},
            {"id": 2, "type": "textbox"}
        ],
        "layout": {
            "children": [
                {"id": 1, "children": [{"id": 2}]}
            ]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [
            {"id": 10, "type": "row"},
            {"id": 20, "type": "textbox"}
        ],
        "layout": {
            "children": [
                {"id": 10, "children": [{"id": 20}]}
            ]
        }
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 71.6μs -> 35.8μs (100% faster)

def test_empty_components_lists():
    # Both configs have empty components lists
    config1 = {"mode": "blocks", "components": []}
    config2 = {"mode": "blocks", "components": []}
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 28.4μs -> 20.0μs (42.2% faster)

def test_empty_layout_children():
    # Both configs have empty layout children
    config1 = {
        "mode": "blocks",
        "components": [],
        "layout": {"children": []}
    }
    config2 = {
        "mode": "blocks",
        "components": [],
        "layout": {"children": []}
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 35.5μs -> 23.0μs (54.7% faster)

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

def test_large_number_of_components_and_layout():
    # Test with 500 components and a flat layout
    n = 500
    config1 = {
        "mode": "blocks",
        "components": [{"id": i, "type": "textbox", "label": f"Comp{i}"} for i in range(n)],
        "layout": {
            "children": [{"id": i} for i in range(n)]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": i + 1000, "type": "textbox", "label": f"Comp{i}"} for i in range(n)],
        "layout": {
            "children": [{"id": i + 1000} for i in range(n)]
        }
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 24.9ms -> 1.30ms (1819% faster)

def test_large_nested_layout():
    # Test with 100 nested rows, each with a textbox
    n = 100
    config1 = {
        "mode": "blocks",
        "components": [{"id": i, "type": "row"} for i in range(n)] +
                      [{"id": i + n, "type": "textbox", "label": f"Textbox{i}"} for i in range(n)],
        "layout": {
            "children": [
                {"id": i, "children": [{"id": i + n}]} for i in range(n)
            ]
        }
    }
    config2 = {
        "mode": "blocks",
        "components": [{"id": i + 1000, "type": "row"} for i in range(n)] +
                      [{"id": i + 2000, "type": "textbox", "label": f"Textbox{i}"} for i in range(n)],
        "layout": {
            "children": [
                {"id": i + 1000, "children": [{"id": i + 2000}]} for i in range(n)
            ]
        }
    }
    codeflash_output = assert_configs_are_equivalent_besides_ids(config1, config2) # 5.72ms -> 592μs (865% faster)

To edit these changes git checkout codeflash/optimize-assert_configs_are_equivalent_besides_ids-mhx2pupn and push.

Codeflash Static Badge

The optimized code achieves an **828% speedup** through three key performance improvements:

**1. O(1) Component Lookup (Major Impact)**
- **Original**: Used `filter()` to find components by ID, resulting in O(n) linear search for each lookup
- **Optimized**: Pre-builds `id->component` dictionaries once, enabling O(1) hash table lookups
- **Impact**: In the line profiler, `assert_same_components` calls dropped from ~120ms total to ~1.7ms total - a massive reduction

**2. Eliminated Unnecessary Deep Copies**
- **Original**: Called `copy.deepcopy()` on entire configs upfront, then again on components during comparison
- **Optimized**: Leverages the fact that `json.loads(json.dumps())` already creates fresh objects, only using shallow `.copy()` when needed for mutation protection
- **Impact**: Removed 89ms of deep copy overhead (22.3% of original runtime)

**3. Reduced Dictionary Mutations**
- **Original**: Used `.pop()` directly on dependency dictionaries, requiring defensive deep copies
- **Optimized**: Creates shallow copies of dependency dictionaries before mutation, avoiding the need for expensive deep copies
- **Impact**: Dependencies processing became more efficient while preserving the same comparison logic

**Performance Characteristics:**
- **Small configs**: 50-110% faster (most test cases)
- **Large configs**: Dramatic improvements - 1819% faster for 500 components, 865% for nested layouts
- **Component-heavy workloads**: Benefits scale with number of components due to O(1) vs O(n) lookup difference

The optimization is particularly effective for Gradio's use case where configs often contain many components that need cross-referencing during validation, making the O(1) lookup transformation the primary performance driver.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 13, 2025 06:54
@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