Repository navigation
fix: avoid quadratic sibling rescans in convert_li and convert_tr - #307
Open
Chandan3456 wants to merge 1 commit into
Open
Chandan3456 wants to merge 1 commit into
Chandan3456 wants to merge 1 commit into
Conversation
convert_li counted the <li> siblings before every <ol> item, and convert_tr counted the rows of a <thead> for every one of its rows and searched the whole table for a <thead> for the first row of every <tbody>. Each of these walks the siblings again per item, so converting a long ordered list, a <thead> with many rows or a table with many <tbody> elements was quadratic in their number (128 KiB took 5 to 16 s). process_tag now keeps a per-tag cache while a tag's children are converted, so the <li> positions and the <thead> lookup are computed once per parent, and the <thead> rows are only counted for the first row, the only one that uses the result. Output is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
convert_li numbers each
<ol>item withlen(el.find_previous_siblings('li')), so item k walks the k-1 items before it. convert_tr has the same shape twice: every row of a<thead>runslen(el.parent.find_all('tr')), and the first row of every<tbody>runsfind_all(['thead'])over the whole table. all three are on the default path for any HTML, so a document holding one long ordered list, a<thead>with many rows or a table split into many<tbody>elements converts in time quadratic in the number of items (CWE-407): each doubling of the input roughly quadruples the time, and 256 KiB of<thead>rows took 168 s against 0.56 s for the same rows in a<tbody>. it showed up while timing large list and table inputs after #296, but the cause is tree rescanning, not the regex backtracking fixed there and in #287. process_tag now keeps a small cache per tag only while that tag's children are being converted (dropped in afinally), and_children_info()uses it so the<li>positions of an<ol>and the<thead>lookup of a table are computed once per parent instead of once per child. the<thead>row count now only runs for the first row, the only row whereis_headrowis read.markdownify() on python 3.13, same process, identical output (the machine was under load, so read these as orders of magnitude):
<ol>with 13,107 items<thead>with 6,898 rows<table>with 3,855<tbody><ul>with 13,107 items (path not changed)with the fix, 1 MiB of each shape converts in 1.1 to 1.5 s, the same as the
<ul>control.output is unchanged: compared against develop on 60,000 randomized conversions of nested lists and tables (with and without
table_infer_header) and 6,768 enumerated<ol>/<table>combinations, with no differences. the cache is empty after a conversion and after an exception raised by a converter. the new tests in tests/test_lists.py and tests/test_tables.py fail on develop (about five minutes for the two) and pass in well under a second here. pytest (97 passed on python 3.13 and 3.8, and with beautifulsoup4 4.9.3), flake8, restructuredtext-lint, mypy and the wheel/sdist build are clean.