Visitar URL original
MarkdownWriter: append unescaped spans in bulk by tballison · Pull Request #456 · commonmark/commonmark-java · GitHub
Skip to content

MarkdownWriter: append unescaped spans in bulk - #456

Merged
robinst merged 2 commits into
commonmark:mainfrom
tballison:markdown-writer-bulk-spans
Oct 10, 2026
Merged

robinst merged 2 commits into
commonmark:mainfrom
tballison:markdown-writer-bulk-spans

Conversation

@tballison

Copy link
Copy Markdown
Contributor

During profiling after the release of Tika 4.0.0, we found we could improve write speed quite a bit in cases where there weren't a lot of characters that needed to be escaped.

🤖

text() always passes a CharMatcher, so every text literal was written 
to the Appendable one character at a time. Scan for the next character 
that needs escaping and append the clean span in one call instead.

JMH, whole CommonMark spec (SpecBenchmark, JDK 17, -f 3 -wi 8 -i 12)

Benchmark before after
renderMarkdownWholeSpec (to StringBuilder) 836.5 ± 34.3 ops/s 956.7 ± 11.3 ops/s +14%
renderMarkdownWholeSpecToWriter 401.9 ± 2.5 ops/s 837.2 ± 20.1 ops/s 2.08x

Rendering 4.7 MB of plain prose (best-of-22 after warmup, JDK 17)

Target Appendable before after
OutputStreamWriter 107 ms 14.5 ms 7.4x
StringWriter 75 ms 12.3 ms 6.1x
StringBuilder 26 ms 12.7 ms 2.0x

Same measurement, 4.0 MB of the spec (escape-dense, closer to a worst case)

Target Appendable before after
OutputStreamWriter 54.9 ms 25.8 ms 2.1x
StringWriter 46.1 ms 21.5 ms 2.1x
StringBuilder 24.2 ms 20.8 ms 1.2x

Comment thread commonmark/src/main/java/org/commonmark/renderer/markdown/MarkdownWriter.java Outdated

@robinst robinst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM, thanks!

@robinst
robinst merged commit 73d76d0 into commonmark:main Oct 10, 2026
5 checks passed
@robinst

robinst commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

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.

3 participants