⚡️ Speed up function _has_script_tag_without_src by 17%
#470
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.
📄 17% (0.17x) speedup for
_has_script_tag_without_srcinmarimo/_output/formatters/iframe.py⏱️ Runtime :
46.9 milliseconds→40.2 milliseconds(best of82runs)📝 Explanation and details
The optimization replaces the string containment check
"<script" not in html_contentwithhtml_content.find("<script")and then passes only the substring starting from the first<script>tag to the HTML parser instead of the entire HTML content.Key changes:
find()returns the index (-1 if not found) which is slightly more efficient than theinoperator for this use case<script>tag onward usingparser.feed(html_content[idx:])Why this is faster:
ScriptTagParser) no longer needs to process potentially large amounts of HTML content before the first<script>tag. This is especially beneficial for large documents where<script>tags appear later in the HTML.StopIterationwhen it finds a script tag withoutsrc, parsing only the relevant portion means less overall work.Test case performance patterns:
find()call overheadThe optimization is particularly effective when the HTML content has substantial non-script content before the first
<script>tag, making it a worthwhile trade-off despite minor overhead on tiny inputs.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
_output/formatters/test_iframe.py::test_has_script_tag_without_src_inline_output/formatters/test_iframe.py::test_has_script_tag_without_src_multiple_output/formatters/test_iframe.py::test_has_script_tag_without_src_no_script_output/formatters/test_iframe.py::test_has_script_tag_without_src_with_attributes_output/formatters/test_iframe.py::test_has_script_tag_without_src_with_src🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_hg3s6k0k/tmp3pixswt5/test_concolic_coverage.py::test__has_script_tag_without_srccodeflash_concolic_hg3s6k0k/tmp3pixswt5/test_concolic_coverage.py::test__has_script_tag_without_src_2codeflash_concolic_hg3s6k0k/tmp3pixswt5/test_concolic_coverage.py::test__has_script_tag_without_src_3To edit these changes
git checkout codeflash/optimize-_has_script_tag_without_src-mhb3eczgand push.