Markdown.ToHtml takes more than two minutes to complete when processing the 32K file #273

Closed
opened 2026-01-29 14:32:28 +00:00 by claunia · 3 comments
Owner

Originally created by @Metalnem on GitHub (Feb 9, 2019).

Markdown.ToHtml takes more than two minutes to complete when processing the 32K file from the attached archive. You can reproduce this by running the following program and passing it the path to the extracted file as a command line argument:

using System.IO;

namespace Markdig.Run
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var text = File.ReadAllText(args[0]);
      var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
      Markdown.ToHtml(text, pipeline);
    }
  }
}

I'm using Markdig 0.15.7 and .NET Core 2.2.103.

Found via SharpFuzz.

Originally created by @Metalnem on GitHub (Feb 9, 2019). **Markdown.ToHtml** takes more than two minutes to complete when processing the 32K file from the [attached archive](https://github.com/lunet-io/markdig/files/2847748/Test.zip). You can reproduce this by running the following program and passing it the path to the extracted file as a command line argument: ```csharp using System.IO; namespace Markdig.Run { public class Program { public static void Main(string[] args) { var text = File.ReadAllText(args[0]); var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); Markdown.ToHtml(text, pipeline); } } } ``` I'm using Markdig 0.15.7 and .NET Core 2.2.103. Found via [SharpFuzz](https://github.com/Metalnem/sharpfuzz).
claunia added the invalid label 2026-01-29 14:32:29 +00:00
Author
Owner

@MihaZupan commented on GitHub (Feb 9, 2019):

This does not appear to be an issue on the master branch. There is a slight difference in output
https://gist.github.com/MihaZupan/a5ce072fffec28315cbdcfd779b203e9/revisions#diff-20e4e18dfae3d1b38df3bce24863b74dR3
See the end of the huge block

@MihaZupan commented on GitHub (Feb 9, 2019): This does not appear to be an issue on the master branch. There is a slight difference in output https://gist.github.com/MihaZupan/a5ce072fffec28315cbdcfd779b203e9/revisions#diff-20e4e18dfae3d1b38df3bce24863b74dR3 See the end of the huge block
Author
Owner

@MihaZupan commented on GitHub (Feb 9, 2019):

I believe this was addressed by the changes to the Abbreviation parser.

On 15.7, this loop seems to iterate over every character

EDIT:

Said loop iterates over each character in both versions, the master branch is just faster becuase of this check.

Basically, both versions, for each character loop over the remainer of the string => O(n^2), but the 15.7 version does a TextMatcher.TryMatch call for each of those inner iterations.

@MihaZupan commented on GitHub (Feb 9, 2019): I believe this was addressed by the changes to the Abbreviation parser. On 15.7, [this loop](https://github.com/lunet-io/markdig/blob/master/src/Markdig/Parsers/InlineProcessor.cs#L199-L281) seems to iterate over every character EDIT: Said loop iterates over each character in both versions, the master branch is just faster becuase of [this check](https://github.com/lunet-io/markdig/blob/master/src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs#L105-L113). Basically, both versions, for each character loop over the remainer of the string => O(n^2), but the 15.7 version does a TextMatcher.TryMatch call for each of those inner iterations.
Author
Owner

@Metalnem commented on GitHub (Feb 9, 2019):

You are right, the master works fine. Do you want me to close this issue, or is there something that can be done about the O(n^2) complexity?

@Metalnem commented on GitHub (Feb 9, 2019): You are right, the master works fine. Do you want me to close this issue, or is there something that can be done about the O(n^2) complexity?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#273