Skip to content

👌 Fix quadratic-time linkify on blocks with many links - #485

Open
affan-arch wants to merge 1 commit into
executablebooks:masterfrom
affan-arch:linkify-linear-time
Open

affan-arch wants to merge 1 commit into
executablebooks:masterfrom
affan-arch:linkify-linear-time

Conversation

@affan-arch

Copy link
Copy Markdown

make both linkify rules linear in the number of matches in a block by porting markdown-it 15.0.1 (which upstream lists under Security): the inline rule now finds the url scheme before :// with a backwards scan bounded to 10 characters instead of searching the $-anchored SCHEME_RE over the whole of state.pending once per ://, and the core rule collects its replacements and rebuilds the inline token's children in one pass instead of calling arrayReplaceAt per match (the len(url) <= protoLength guard is from the same upstream change); with linkify on, "a://" * 20_000 (80 KiB) used to take 14.9 s and now takes 0.07 s, "*x* a.com " * 20_000 (200 KiB) goes from 17.6 s to 0.61 s, and the output matches the old rules on 9,245 inputs (repo fixtures, the CommonMark spec, scheme edge cases and 9k fuzzed strings) rendered through render and renderInline across five presets.

This branch has not been deployed

No deployments
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