Repository navigation
fix: reset _escape_table and _code_table on each convert() call (closes #763) - #768
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
|
Looks reasonable, can you just add tests to the existing test/test_markdown2.py file instead of making a new one? |
test.py only collects the cases yielded by test_markdown2.test_cases(), so the separate file was never run by make test. The timing check is replaced by asserting that the escape and code tables keep their size, which fails on master without depending on how fast the runner is.
|
Done in 64ad005. The tests are now in I also replaced the timing-based check with a deterministic one: after 50 |
|
Thanks! |
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