⚡️ Speed up function _hungarian_algorithm by 170%
#625
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.
📄 170% (1.70x) speedup for
_hungarian_algorithminmarimo/_utils/cell_matching.py⏱️ Runtime :
181 milliseconds→67.0 milliseconds(best of80runs)📝 Explanation and details
The optimized code achieves a 170% speedup by eliminating redundant matrix traversals and reducing cache misses through more efficient memory access patterns.
Key Optimizations:
1. Precomputed Index Sets
row_assignment[i] == -1andcol_assignment[j] == -1across nested O(n²) loopsuncovered_rowsanduncovered_colsonce per iteration, then operates only on these smaller sets2. Row Reference Caching
row = score_matrix[i]before inner loops to avoid repeatedscore_matrix[i]lookups3. Batch Matrix Updates
Performance Impact:
The line profiler shows the most dramatic improvements in Step 4's nested loops:
The optimization is particularly effective for:
_match_cell_ids_by_similaritywhich processes code similarity matrices, making the performance gains directly beneficial to cell matching operations in the Marimo notebook environmentThe improvements maintain identical algorithmic behavior while significantly reducing the constant factors that dominate the Hungarian algorithm's runtime.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_a_rncq49/tmpfmcrb4_l/test_concolic_coverage.py::test__hungarian_algorithmcodeflash_concolic_a_rncq49/tmpfmcrb4_l/test_concolic_coverage.py::test__hungarian_algorithm_2To edit these changes
git checkout codeflash/optimize-_hungarian_algorithm-mhwqu21yand push.