[PR #606] [MERGED] Improvements when TrackTrivia is disabled #1136

Open
opened 2026-01-29 14:50:14 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/xoofx/markdig/pull/606
Author: @MihaZupan
Created: 3/13/2022
Status: Merged
Merged: 3/14/2022
Merged by: @xoofx

Base: masterHead: perf-march-2022


📝 Commits (9)

  • 1862b37 Optimize LineReader.ReadLine
  • 9145f47 Move TryParseInlineLinkTrivia to cold path
  • fb9561c Fix roughLineCountEstimate calculation (min/max order)
  • 88cdbf3 Lazily allocate CodeBlock.CodeBlockLines
  • 9f734ba Lazily allocate Trivia properties on LinkInline
  • b697a03 Lazily allocate Trivia properties on LinkReferenceDefinition
  • 61452c9 Aggressively avoid TrackTrivia work and allocations when not requested
  • 6f75b51 Aggressively avoid TrackTrivia work and allocations when not requested for Inlines
  • b7d02ca Fix TrackTrivia /// comments

📊 Changes

27 files changed (+499 additions, -301 deletions)

View changed files

📝 src/Markdig/Helpers/LineReader.cs (+42 -26)
📝 src/Markdig/Helpers/StringSlice.cs (+9 -0)
📝 src/Markdig/Parsers/FencedBlockParserBase.cs (+7 -3)
📝 src/Markdig/Parsers/FencedCodeBlockParser.cs (+10 -4)
📝 src/Markdig/Parsers/HeadingBlockParser.cs (+11 -6)
📝 src/Markdig/Parsers/HtmlBlockParser.cs (+10 -4)
📝 src/Markdig/Parsers/IndentedCodeBlockParser.cs (+21 -9)
📝 src/Markdig/Parsers/Inlines/CodeInlineParser.cs (+8 -2)
📝 src/Markdig/Parsers/Inlines/LinkInlineParser.cs (+91 -87)
📝 src/Markdig/Parsers/ListBlockParser.cs (+21 -7)
📝 src/Markdig/Parsers/MarkdownParser.cs (+2 -2)
📝 src/Markdig/Parsers/ParagraphBlockParser.cs (+20 -10)
📝 src/Markdig/Parsers/QuoteBlockParser.cs (+64 -45)
📝 src/Markdig/Parsers/ThematicBreakParser.cs (+10 -4)
📝 src/Markdig/Roundtrip.md (+2 -2)
📝 src/Markdig/Syntax/Block.cs (+26 -8)
📝 src/Markdig/Syntax/CodeBlock.cs (+2 -1)
📝 src/Markdig/Syntax/FencedCodeBlock.cs (+21 -7)
📝 src/Markdig/Syntax/HeadingBlock.cs (+13 -3)
📝 src/Markdig/Syntax/IBlock.cs (+2 -2)

...and 7 more files

📄 Description

A few changes mainly around reducing the overhead of the TrackTrivia impl even when it isn't requested:

  • Span.IndexOfAny('\r', '\n') in LineReader
  • Moving all trivia properties to lazy-allocated objects to reduce the size of Syntax objects.
  • Avoiding work that's only relevant for trivia when it's not needed

I was playing around with two markdown documents for these:

  • The Markdig readme
  • A random doc article

Before

Method Mean Error StdDev Gen 0 Gen 1 Allocated
MarkdigReadme 315.18 us 0.695 us 1.019 us 36.1328 12.2070 169 KB
TracingArticle 43.94 us 0.080 us 0.115 us 5.6763 0.3052 23 KB

After

Method Mean Error StdDev Gen 0 Gen 1 Allocated
MarkdigReadme 305.87 us 3.347 us 5.010 us 32.7148 10.7422 136 KB
TracingArticle 41.33 us 0.136 us 0.200 us 4.4556 - 18 KB

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/xoofx/markdig/pull/606 **Author:** [@MihaZupan](https://github.com/MihaZupan) **Created:** 3/13/2022 **Status:** ✅ Merged **Merged:** 3/14/2022 **Merged by:** [@xoofx](https://github.com/xoofx) **Base:** `master` ← **Head:** `perf-march-2022` --- ### 📝 Commits (9) - [`1862b37`](https://github.com/xoofx/markdig/commit/1862b37bbd51b5185797e53f24340e179b20ea46) Optimize LineReader.ReadLine - [`9145f47`](https://github.com/xoofx/markdig/commit/9145f47f89ccf8ba8502043a45a502a3924e22c2) Move TryParseInlineLinkTrivia to cold path - [`fb9561c`](https://github.com/xoofx/markdig/commit/fb9561cf8900f809d50e3bf7183c4378c1f94e3c) Fix roughLineCountEstimate calculation (min/max order) - [`88cdbf3`](https://github.com/xoofx/markdig/commit/88cdbf3a17dea9f4c2664b5a6868a3f2ca39368d) Lazily allocate CodeBlock.CodeBlockLines - [`9f734ba`](https://github.com/xoofx/markdig/commit/9f734ba3c94853a573066ca3ea8ba05a16447fd7) Lazily allocate Trivia properties on LinkInline - [`b697a03`](https://github.com/xoofx/markdig/commit/b697a03c2b84dc77a6825362ecff2c3a338eab28) Lazily allocate Trivia properties on LinkReferenceDefinition - [`61452c9`](https://github.com/xoofx/markdig/commit/61452c91e9ecba90fa9d7c9279b87c80042171a8) Aggressively avoid TrackTrivia work and allocations when not requested - [`6f75b51`](https://github.com/xoofx/markdig/commit/6f75b5156c079294d56683be2711438bf70e668d) Aggressively avoid TrackTrivia work and allocations when not requested for Inlines - [`b7d02ca`](https://github.com/xoofx/markdig/commit/b7d02cadbb041ca3f789f7d0cfea07bd5c7cbe6f) Fix TrackTrivia /// comments ### 📊 Changes **27 files changed** (+499 additions, -301 deletions) <details> <summary>View changed files</summary> 📝 `src/Markdig/Helpers/LineReader.cs` (+42 -26) 📝 `src/Markdig/Helpers/StringSlice.cs` (+9 -0) 📝 `src/Markdig/Parsers/FencedBlockParserBase.cs` (+7 -3) 📝 `src/Markdig/Parsers/FencedCodeBlockParser.cs` (+10 -4) 📝 `src/Markdig/Parsers/HeadingBlockParser.cs` (+11 -6) 📝 `src/Markdig/Parsers/HtmlBlockParser.cs` (+10 -4) 📝 `src/Markdig/Parsers/IndentedCodeBlockParser.cs` (+21 -9) 📝 `src/Markdig/Parsers/Inlines/CodeInlineParser.cs` (+8 -2) 📝 `src/Markdig/Parsers/Inlines/LinkInlineParser.cs` (+91 -87) 📝 `src/Markdig/Parsers/ListBlockParser.cs` (+21 -7) 📝 `src/Markdig/Parsers/MarkdownParser.cs` (+2 -2) 📝 `src/Markdig/Parsers/ParagraphBlockParser.cs` (+20 -10) 📝 `src/Markdig/Parsers/QuoteBlockParser.cs` (+64 -45) 📝 `src/Markdig/Parsers/ThematicBreakParser.cs` (+10 -4) 📝 `src/Markdig/Roundtrip.md` (+2 -2) 📝 `src/Markdig/Syntax/Block.cs` (+26 -8) 📝 `src/Markdig/Syntax/CodeBlock.cs` (+2 -1) 📝 `src/Markdig/Syntax/FencedCodeBlock.cs` (+21 -7) 📝 `src/Markdig/Syntax/HeadingBlock.cs` (+13 -3) 📝 `src/Markdig/Syntax/IBlock.cs` (+2 -2) _...and 7 more files_ </details> ### 📄 Description A few changes mainly around reducing the overhead of the `TrackTrivia` impl even when it isn't requested: - `Span.IndexOfAny('\r', '\n')` in `LineReader` - Moving all trivia properties to lazy-allocated objects to reduce the size of `Syntax` objects. - Avoiding work that's only relevant for trivia when it's not needed I was playing around with two markdown documents for these: - The Markdig readme - A random doc [article](https://github.com/microsoft/reverse-proxy/blob/main/docs/docfx/articles/distributed-tracing.md) Before | Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated | |--------------- |----------:|---------:|---------:|--------:|--------:|----------:| | MarkdigReadme | 315.18 us | 0.695 us | 1.019 us | 36.1328 | 12.2070 | 169 KB | | TracingArticle | 43.94 us | 0.080 us | 0.115 us | 5.6763 | 0.3052 | 23 KB | After | Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated | |--------------- |----------:|---------:|---------:|--------:|--------:|----------:| | MarkdigReadme | 305.87 us | 3.347 us | 5.010 us | 32.7148 | 10.7422 | 136 KB | | TracingArticle | 41.33 us | 0.136 us | 0.200 us | 4.4556 | - | 18 KB | --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 14:50:14 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#1136