Skip to content

Commit

Permalink
Merge pull request #377 from MihaZupan/allocation-reductions
Browse files Browse the repository at this point in the history
10% time and 50% memory improvement
  • Loading branch information
xoofx committed Oct 15, 2019
2 parents f8ab1cc + 3606f23 commit 07a7714
Show file tree
Hide file tree
Showing 28 changed files with 567 additions and 309 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: 10.0.{build}
image: Visual Studio 2017
image: Visual Studio 2019
configuration: Release
environment:
COVERALLS_REPO_TOKEN:
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix SmartyPants quote matching ([(PR #360)](https://github.com/lunet-io/markdig/pull/360))
- Fix generic attributes with values of length 1 ([(PR #361)](https://github.com/lunet-io/markdig/pull/361))
- Fix link text balanced bracket matching ([(PR #375)](https://github.com/lunet-io/markdig/pull/375))
- Improve overall performance and substantially reduce allocations ([(PR #377)](https://github.com/lunet-io/markdig/pull/377))

## 0.17.1 (04 July 2019)
- Fix regression when escaping HTML characters ([(PR #340)](https://github.com/lunet-io/markdig/pull/340))
Expand Down
14 changes: 12 additions & 2 deletions src/Markdig.Tests/TestStringSliceList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NUnit.Framework;
using NUnit.Framework;
using System.Collections.Generic;
using System.Text;
using Markdig.Helpers;
Expand Down Expand Up @@ -81,8 +81,18 @@ public void TestStringLineGroupSaveAndRestore()
public void TestSkipWhitespaces()
{
var text = new StringLineGroup(" ABC").ToCharIterator();
Assert.True(text.TrimStart());
Assert.False(text.TrimStart());
Assert.AreEqual('A', text.CurrentChar);

text = new StringLineGroup(" ").ToCharIterator();
Assert.True(text.TrimStart());
Assert.AreEqual('\0', text.CurrentChar);

var slice = new StringSlice(" ABC");
Assert.False(slice.TrimStart());

slice = new StringSlice(" ");
Assert.True(slice.TrimStart());
}

[Test]
Expand Down
33 changes: 24 additions & 9 deletions src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class AutoIdentifierExtension : IMarkdownExtension
{
private const string AutoIdentifierKey = "AutoIdentifier";
private readonly AutoIdentifierOptions options;
private readonly StripRendererCache rendererCache = new StripRendererCache();

/// <summary>
/// Initializes a new instance of the <see cref="AutoIdentifierExtension"/> class.
Expand Down Expand Up @@ -159,17 +160,11 @@ private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline in
}

// Use internally a HtmlRenderer to strip links from a heading
var headingWriter = new StringWriter();
var stripRenderer = new HtmlRenderer(headingWriter)
{
// Set to false both to avoid having any HTML tags in the output
EnableHtmlForInline = false,
EnableHtmlEscape = false
};
var stripRenderer = rendererCache.Get();

stripRenderer.Render(headingBlock.Inline);
var headingText = headingWriter.ToString();
headingWriter.GetStringBuilder().Length = 0;
var headingText = stripRenderer.Writer.ToString();
rendererCache.Release(stripRenderer);

// Urilize the link
headingText = (options & AutoIdentifierOptions.GitHub) != 0
Expand All @@ -195,5 +190,25 @@ private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline in

attributes.Id = headingId;
}

private sealed class StripRendererCache : ObjectCache<HtmlRenderer>
{
protected override HtmlRenderer NewInstance()
{
var headingWriter = new StringWriter();
var stripRenderer = new HtmlRenderer(headingWriter)
{
// Set to false both to avoid having any HTML tags in the output
EnableHtmlForInline = false,
EnableHtmlEscape = false
};
return stripRenderer;
}

protected override void Reset(HtmlRenderer instance)
{
instance.Reset();
}
}
}
}

0 comments on commit 07a7714

Please sign in to comment.