Skip to content

Fix quadratic processing time in the tables extension for long blocks - #1704

Open
deadstrobe5 wants to merge 1 commit into
Python-Markdown:masterfrom
deadstrobe5:perf/tables-two-rows
Open

deadstrobe5 wants to merge 1 commit into
Python-Markdown:masterfrom
deadstrobe5:perf/tables-two-rows

Conversation

@deadstrobe5

Copy link
Copy Markdown
Contributor

Description

TableProcessor.test splits and strips every line of the block, but only the first two rows are needed outside the single-column-with-border case. Blocks that other processors take apart one line at a time, such as headers with no blank lines between them, return the rest of the block to be tested again, so the table test alone made those blocks quadratic. tables is enabled by default in MkDocs.

This splits off only the first two rows. The single-column case, which checks that every row has a pipe, still reads all the rows.

Median of 5 alternating runs in one process, CPython 3.14, extensions toc, tables and fenced_code:

Document Before After
4,000 lines of # H 725 ms 172 ms
500 tight headers with text 71.9 ms 48.2 ms
300 KB of the repo docs 180 ms 177 ms
2 KB page 1.68 ms 1.65 ms

The old and new code give the same HTML on 30,000 generated documents mixing tables, single-column tables, headers, lists and other blocks.

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.

  • AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.

    Claude Code (Claude Opus 5.5) was used for everything ticked below. The review attestation in the box above is left for the human submitter.

    AI tools were use to assist with the following:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: ______________

Checklist

@waylan

waylan commented Nov 10, 2026

Copy link
Copy Markdown
Member

I think maybe we should add a couple additional tests to ensure we are not breaking the existing behavior.

# Table
| A | B |
|---|---|
| a | b |

Not a table
| A | B |
|---|---|
| a | b |

We want to ensure that the first one is properly recognized as a table and the second is not. Based on the code, I expect the second to pass, but I am concerned that the first may not.

@waylan waylan added the requires-changes Awaiting updates after a review. label Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requires-changes Awaiting updates after a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants