⚡️ Speed up function assert_configs_are_equivalent_besides_ids by 828%
#79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 828% (8.28x) speedup for
assert_configs_are_equivalent_besides_idsingradio/utils.py⏱️ Runtime :
60.0 milliseconds→6.46 milliseconds(best of11runs)📝 Explanation and details
The optimized code achieves an 828% speedup through three key performance improvements:
1. O(1) Component Lookup (Major Impact)
filter()to find components by ID, resulting in O(n) linear search for each lookupid->componentdictionaries once, enabling O(1) hash table lookupsassert_same_componentscalls dropped from ~120ms total to ~1.7ms total - a massive reduction2. Eliminated Unnecessary Deep Copies
copy.deepcopy()on entire configs upfront, then again on components during comparisonjson.loads(json.dumps())already creates fresh objects, only using shallow.copy()when needed for mutation protection3. Reduced Dictionary Mutations
.pop()directly on dependency dictionaries, requiring defensive deep copiesPerformance Characteristics:
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:
⚙️ Existing Unit Tests and Runtime
test_blocks.py::TestBlocksMethods.test_load_from_configtest_interfaces.py::TestTabbedInterface.test_tabbed_interface_config_matches_manual_tabtest_utils.py::test_assert_configs_are_equivalent🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-assert_configs_are_equivalent_besides_ids-mhx2pupnand push.