Skip to content

fix: avoid quadratic sibling rescans in convert_li and convert_tr - #307

Open
Chandan3456 wants to merge 1 commit into
matthewwithanm:developfrom
Chandan3456:ol-table-quadratic
Open

Chandan3456 wants to merge 1 commit into
matthewwithanm:developfrom
Chandan3456:ol-table-quadratic

Conversation

@Chandan3456

Copy link
Copy Markdown
Contributor

convert_li numbers each <ol> item with len(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> runs len(el.parent.find_all('tr')), and the first row of every <tbody> runs find_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 a finally), 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 where is_headrow is read.

markdownify() on python 3.13, same process, identical output (the machine was under load, so read these as orders of magnitude):

input, 128 KiB develop this branch
<ol> with 13,107 items 5.26 s 0.36 s
<thead> with 6,898 rows 11.52 s 0.31 s
<table> with 3,855 <tbody> 16.17 s 0.17 s
<ul> with 13,107 items (path not changed) 0.16 s 0.18 s

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant