⚡️ Speed up function can_unstructured_elements_be_merged by 97%
#5
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.
📄 97% (0.97x) speedup for
can_unstructured_elements_be_mergedinunstructured/partition/html/transformations.py⏱️ Runtime :
206 milliseconds→105 milliseconds(best of67runs)📝 Explanation and details
The optimization achieves a 97% speedup by introducing LRU caching for HTML parsing - the primary bottleneck in the original code.
Key Optimization: Cached HTML Parsing
@functools.lru_cache(maxsize=4096)decorator to a new_cached_html_tags()function that wrapsBeautifulSoup().find_all()can_unstructured_elements_be_merged()with cached lookupsBeautifulSoup+find_all) consumed ~49% of total runtime in the original codeWhy This Works:
Performance Impact by Test Case:
test_edge_empty_html_stringsshows 4567% speedup (44.7μs → 958ns)test_large_scale_all_mergeableimproves 238% (48.8ms → 14.4ms)Hot Path Context:
Based on
function_references, this function is called fromcombine_inline_elements()which processes consecutive element pairs during HTML document partitioning. This optimization significantly accelerates document processing pipelines where HTML merging is a frequent operation.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic__a8m4ody/tmp7showwtr/test_concolic_coverage.py::test_can_unstructured_elements_be_mergedTo edit these changes
git checkout codeflash/optimize-can_unstructured_elements_be_merged-mjcc1y3tand push.