Skip to content

Commit

Permalink
Merge pull request #417 from KrisVandermotten/StringBuilderCache
Browse files Browse the repository at this point in the history
Cleanup StringBuilderCache
  • Loading branch information
xoofx authored Apr 18, 2020
2 parents edb4c6c + a19f783 commit 47c6c49
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 52 deletions.
2 changes: 0 additions & 2 deletions src/Markdig/Helpers/LinkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ public static bool TryParseTitle<T>(ref T text, out string title) where T : ICha
{
bool isValid = false;
var buffer = StringBuilderCache.Local();
buffer.Length = 0;

// a sequence of zero or more characters between straight double-quote characters ("), including a " character only if it is backslash-escaped, or
// a sequence of zero or more characters between straight single-quote characters ('), including a ' character only if it is backslash-escaped, or
Expand Down Expand Up @@ -517,7 +516,6 @@ public static bool TryParseUrl<T>(ref T text, out string link, bool isAutoLink =
{
bool isValid = false;
var buffer = StringBuilderCache.Local();
buffer.Length = 0;

var c = text.CurrentChar;

Expand Down
16 changes: 2 additions & 14 deletions src/Markdig/Helpers/StringBuilderCache.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System;
using System.Text;

namespace Markdig.Helpers
{
/// <summary>
/// An implementation of <see cref="ObjectCache{T}"/> for <see cref="StringBuilder"/>
/// </summary>
/// <seealso cref="Markdig.Helpers.ObjectCache{StringBuilder}" />
public class StringBuilderCache : DefaultObjectCache<StringBuilder>
public static class StringBuilderCache
{
/// <summary>
/// A StringBuilder that can be used locally in a method body only.
Expand All @@ -31,13 +27,5 @@ public static StringBuilder Local()
}
return sb;
}

protected override void Reset(StringBuilder instance)
{
if (instance.Length > 0)
{
instance.Length = 0;
}
}
}
}
5 changes: 1 addition & 4 deletions src/Markdig/MarkdownPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ public class MarkdownPipeline
/// <summary>
/// Initializes a new instance of the <see cref="MarkdownPipeline" /> class.
/// </summary>
internal MarkdownPipeline(OrderedList<IMarkdownExtension> extensions, BlockParserList blockParsers, InlineParserList inlineParsers, StringBuilderCache cache, TextWriter debugLog, ProcessDocumentDelegate documentProcessed)
internal MarkdownPipeline(OrderedList<IMarkdownExtension> extensions, BlockParserList blockParsers, InlineParserList inlineParsers, TextWriter debugLog, ProcessDocumentDelegate documentProcessed)
{
if (blockParsers == null) ThrowHelper.ArgumentNullException(nameof(blockParsers));
if (inlineParsers == null) ThrowHelper.ArgumentNullException(nameof(inlineParsers));
// Add all default parsers
Extensions = extensions;
BlockParsers = blockParsers;
InlineParsers = inlineParsers;
StringBuilderCache = cache;
DebugLog = debugLog;
DocumentProcessed = documentProcessed;
}
Expand All @@ -44,8 +43,6 @@ internal MarkdownPipeline(OrderedList<IMarkdownExtension> extensions, BlockParse

internal InlineParserList InlineParsers { get; }

internal StringBuilderCache StringBuilderCache { get; }

// TODO: Move the log to a better place
internal TextWriter DebugLog { get; }

Expand Down
8 changes: 0 additions & 8 deletions src/Markdig/MarkdownPipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public MarkdownPipelineBuilder()
};

Extensions = new OrderedList<IMarkdownExtension>();

StringBuilderCache = new StringBuilderCache();
}

/// <summary>
Expand All @@ -68,11 +66,6 @@ public MarkdownPipelineBuilder()
/// </summary>
public OrderedList<IMarkdownExtension> Extensions { get; }

/// <summary>
/// Gets or sets the string builder cache used by the parsers.
/// </summary>
public StringBuilderCache StringBuilderCache { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to enable precise source location (slower parsing but accurate position for block and inline elements)
/// </summary>
Expand Down Expand Up @@ -120,7 +113,6 @@ public MarkdownPipeline Build()
new OrderedList<IMarkdownExtension>(Extensions),
new BlockParserList(BlockParsers),
new InlineParserList(InlineParsers),
StringBuilderCache,
DebugLog,
GetDocumentProcessed)
{
Expand Down
10 changes: 1 addition & 9 deletions src/Markdig/Parsers/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ private BlockProcessor(BlockProcessor root)
// These properties are not changing between a parent and a children BlockProcessor
this.root = root;
this.parserStateCache = root.parserStateCache;
StringBuilders = root.StringBuilders;
Document = root.Document;
Parsers = root.Parsers;

Expand All @@ -41,13 +40,11 @@ private BlockProcessor(BlockProcessor root)
/// <param name="parsers">The list of parsers.</param>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument document, BlockParserList parsers, MarkdownParserContext context)
public BlockProcessor(MarkdownDocument document, BlockParserList parsers, MarkdownParserContext context)
{
if (stringBuilders == null) ThrowHelper.ArgumentNullException(nameof(stringBuilders));
if (document == null) ThrowHelper.ArgumentNullException(nameof(document));
if (parsers == null) ThrowHelper.ArgumentNullException(nameof(parsers));
parserStateCache = new BlockParserStateCache(this);
StringBuilders = stringBuilders;
Document = document;
document.IsOpen = true;
Parsers = parsers;
Expand Down Expand Up @@ -153,11 +150,6 @@ public BlockProcessor(StringBuilderCache stringBuilders, MarkdownDocument docume
/// </summary>
public int StartBeforeIndent { get; private set; }

/// <summary>
/// Gets the cache of string builders.
/// </summary>
public StringBuilderCache StringBuilders { get; }

/// <summary>
/// Gets the current stack of <see cref="Block"/> being processed.
/// </summary>
Expand Down
9 changes: 1 addition & 8 deletions src/Markdig/Parsers/InlineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ public class InlineProcessor
/// <param name="inlineCreated">The inline created event.</param>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument document, InlineParserList parsers, bool preciseSourcelocation, MarkdownParserContext context)
public InlineProcessor(MarkdownDocument document, InlineParserList parsers, bool preciseSourcelocation, MarkdownParserContext context)
{
if (stringBuilders == null) ThrowHelper.ArgumentNullException(nameof(stringBuilders));
if (document == null) ThrowHelper.ArgumentNullException(nameof(document));
if (parsers == null) ThrowHelper.ArgumentNullException(nameof(parsers));
StringBuilders = stringBuilders;
Document = document;
Parsers = parsers;
Context = context;
Expand Down Expand Up @@ -91,11 +89,6 @@ public InlineProcessor(StringBuilderCache stringBuilders, MarkdownDocument docum
/// </summary>
public MarkdownDocument Document { get; }

/// <summary>
/// Gets the cache string builders.
/// </summary>
public StringBuilderCache StringBuilders { get; }

/// <summary>
/// Gets or sets the index of the line from the begining of the document being processed.
/// </summary>
Expand Down
4 changes: 1 addition & 3 deletions src/Markdig/Parsers/Inlines/CodeInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
c = slice.NextChar();
}

var builder = processor.StringBuilders.Get();
var builder = StringBuilderCache.Local();

// A backtick string is a string of one or more backtick characters (`) that is neither preceded nor followed by a backtick.
// A code span begins with a backtick string and ends with a backtick string of equal length.
Expand Down Expand Up @@ -120,8 +120,6 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
isMatching = true;
}

// Release the builder if not used
processor.StringBuilders.Release(builder);
return isMatching;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Markdig/Parsers/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ private MarkdownParser(string text, MarkdownPipeline pipeline, MarkdownParserCon
preciseSourceLocation = pipeline.PreciseSourceLocation;

// Initialize the pipeline
var stringBuilderCache = pipeline.StringBuilderCache ?? new StringBuilderCache();

document = new MarkdownDocument();

// Initialize the block parsers
blockProcessor = new BlockProcessor(stringBuilderCache, document, pipeline.BlockParsers, context);
blockProcessor = new BlockProcessor(document, pipeline.BlockParsers, context);

// Initialize the inline parsers
inlineProcessor = new InlineProcessor(stringBuilderCache, document, pipeline.InlineParsers, pipeline.PreciseSourceLocation, context)
inlineProcessor = new InlineProcessor(document, pipeline.InlineParsers, pipeline.PreciseSourceLocation, context)
{
DebugLog = pipeline.DebugLog
};
Expand Down

0 comments on commit 47c6c49

Please sign in to comment.