perf: add fast path and cached pipeline to StripMarkdown - #3838
Open
Mpdreamz wants to merge 1 commit into
Open
Conversation
Markdig.Markdown.ToPlainText(string, TextWriter) with no pipeline argument builds a fresh MarkdownPipeline + renderer on every call. MarkdownFile.cs calls StripMarkdown 2-5 times per file (ctor sets Title = RelativePath, ReadDocumentInstructions reassigns Title and NavigationTitle, plus once per h2+ heading), so the overhead compounds across 435k files. Two changes: 1. Cache a shared plain-text MarkdownPipeline — eliminates the repeated MarkdownPipelineBuilder.Build() cost for every call that does reach Markdig. 2. Add a fast path using SearchValues<char>: titles are overwhelmingly plain text (including the RelativePath seed assignment), so skip the StringWriter + Markdig entirely when no markdown-syntax character is present in the input string. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Markdig.Markdown.ToPlainText(string, TextWriter)with no pipeline builds a freshMarkdownPipeline+ renderer on every call.MarkdownFile.cscallsStripMarkdown2–5 times per file:Title = RelativePath(a plain path string)ReadDocumentInstructionsreassignsTitleandNavigationTitleGetAnchorsTwo changes:
MarkdownPipelineshared across calls eliminates the repeatedMarkdownPipelineBuilder.Build()cost.SearchValues<char>check skips theStringWriter+ Markdig entirely for strings that contain no markdown-syntax characters. Titles are overwhelmingly plain text (including theRelativePathseed assignment in the constructor).Test plan
dotnet test tests/Elastic.Markdown.Tests/ --filter StripMarkdown|DropdownPlainText— 16 tests passdotnet test tests/Elastic.Markdown.Tests/— full 1960-test suite passes (9 new tests inHelpers/StripMarkdownTests.cs)dotnet run --project src/tooling/docs-migrate -- bench— recordResolveDirectoryTreetime🤖 Generated with Claude Code