Repository navigation
fix: reset _escape_table and _code_table on each convert() call (closes #763) - #768
Open
HongTin2104 wants to merge 2 commits into
Open
HongTin2104 wants to merge 2 commits into
HongTin2104 wants to merge 2 commits into
Conversation
Reusing a Markdown() instance was retaining entries in _escape_table and _code_table across documents. These tables grew with each call, causing quadratic slowdown and incorrect output: a backslash before a URL seen in an earlier document was silently dropped because the URL hash was already in _escape_table. Fix: extract the table initialisation into _reset_tables() and call it from both __init__() and reset(). The call in reset() is placed before _setup_extras() because the middle-word-em extra reads _escape_table. Closes trentm#763
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.
Summary
Reusing a
Markdown()instance was retaining entries in_escape_tableand_code_tableacross documents. These tables grew with each call, causing two problems:Wrong output: A backslash before a URL that appeared in an earlier document was silently dropped, because the URL hash was already in
_escape_tablefrom that earlier call.Quadratic slowdown:
_escape_tableand_code_tableaccumulated every URL and code span ever seen. With 40 documents of 200 links and 200 code spans each the 40th document took ~37× longer than the 1st.Root cause
reset()clearedurls,titles,html_blocks,html_spans,list_level, andextras, but never touched_escape_tableor_code_table.Fix
Extract the table initialisation from
__init__into_reset_tables()and call it from both__init__andreset(). The call inreset()is placed before_setup_extras()because themiddle-word-emextra reads_escape_table.The convenience function
markdown2.markdown()is unaffected (it creates a new instance per call).Verification
Added
test/test_reuse_instance.pywith 3 tests: backslash escape correctness, code span isolation, and a performance guard.Closes #763