From 1862b37bbd51b5185797e53f24340e179b20ea46 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 12 Mar 2022 02:18:50 +0100 Subject: [PATCH 1/9] Optimize LineReader.ReadLine --- src/Markdig/Helpers/LineReader.cs | 68 ++++++++++++++++++------------ src/Markdig/Helpers/StringSlice.cs | 9 ++++ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/Markdig/Helpers/LineReader.cs b/src/Markdig/Helpers/LineReader.cs index 27c32195..59033e50 100644 --- a/src/Markdig/Helpers/LineReader.cs +++ b/src/Markdig/Helpers/LineReader.cs @@ -4,6 +4,8 @@ using System; using System.IO; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace Markdig.Helpers { @@ -39,41 +41,55 @@ namespace Markdig.Helpers /// A new line or null if the end of has been reached public StringSlice ReadLine() { - string text = _text; + string? text = _text; + int end = text.Length; int sourcePosition = SourcePosition; + int newSourcePosition = int.MaxValue; + NewLine newLine = NewLine.None; - for (int i = sourcePosition; i < text.Length; i++) + if ((uint)sourcePosition >= (uint)end) { - char c = text[i]; - if (c == '\r') + text = null; + } + else + { +#if NETCOREAPP3_1_OR_GREATER + ReadOnlySpan span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.Add(ref Unsafe.AsRef(text.GetPinnableReference()), sourcePosition), end - sourcePosition); +#else + ReadOnlySpan span = text.AsSpan(sourcePosition); +#endif + + int crlf = span.IndexOfAny('\r', '\n'); + if (crlf >= 0) { - int length = 1; - var newLine = NewLine.CarriageReturn; - if (c == '\r' && (uint)(i + 1) < (uint)text.Length && text[i + 1] == '\n') + end = sourcePosition + crlf; + newSourcePosition = end + 1; + +#if NETCOREAPP3_1_OR_GREATER + if (Unsafe.Add(ref Unsafe.AsRef(text.GetPinnableReference()), end) == '\r') +#else + if ((uint)end < (uint)text.Length && text[end] == '\r') +#endif { - i++; - length = 2; - newLine = NewLine.CarriageReturnLineFeed; + if ((uint)(newSourcePosition) < (uint)text.Length && text[newSourcePosition] == '\n') + { + newLine = NewLine.CarriageReturnLineFeed; + newSourcePosition++; + } + else + { + newLine = NewLine.CarriageReturn; + } + } + else + { + newLine = NewLine.LineFeed; } - - var slice = new StringSlice(text, sourcePosition, i - length, newLine); - SourcePosition = i + 1; - return slice; - } - - if (c == '\n') - { - var slice = new StringSlice(text, sourcePosition, i - 1, NewLine.LineFeed); - SourcePosition = i + 1; - return slice; } } - if (sourcePosition >= text.Length) - return default; - - SourcePosition = int.MaxValue; - return new StringSlice(text, sourcePosition, text.Length - 1); + SourcePosition = newSourcePosition; + return new StringSlice(text, sourcePosition, end - 1, newLine, dummy: false); } } } \ No newline at end of file diff --git a/src/Markdig/Helpers/StringSlice.cs b/src/Markdig/Helpers/StringSlice.cs index 15577955..6eda9de7 100644 --- a/src/Markdig/Helpers/StringSlice.cs +++ b/src/Markdig/Helpers/StringSlice.cs @@ -82,6 +82,15 @@ namespace Markdig.Helpers NewLine = newLine; } + // Internal ctor to skip the null check + internal StringSlice(string text, int start, int end, NewLine newLine, bool dummy) + { + Text = text; + Start = start; + End = end; + NewLine = newLine; + } + /// /// The text of this slice. /// From 9145f47f89ccf8ba8502043a45a502a3924e22c2 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 12 Mar 2022 22:25:52 +0100 Subject: [PATCH 2/9] Move TryParseInlineLinkTrivia to cold path --- .../Parsers/Inlines/LinkInlineParser.cs | 158 +++++++++--------- 1 file changed, 76 insertions(+), 82 deletions(-) diff --git a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs index 875f7028..48c7c750 100644 --- a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs @@ -5,6 +5,7 @@ using Markdig.Helpers; using Markdig.Syntax; using Markdig.Syntax.Inlines; +using System.Diagnostics.CodeAnalysis; namespace Markdig.Parsers.Inlines { @@ -233,74 +234,18 @@ namespace Markdig.Parsers.Inlines if (text.CurrentChar == '(') { + LinkInline? link = null; + if (inlineState.TrackTrivia) { - if (LinkHelper.TryParseInlineLinkTrivia( - ref text, - out string? url, - out SourceSpan unescapedUrlSpan, - out string? title, - out SourceSpan unescapedTitleSpan, - out char titleEnclosingCharacter, - out SourceSpan linkSpan, - out SourceSpan titleSpan, - out SourceSpan triviaBeforeLink, - out SourceSpan triviaAfterLink, - out SourceSpan triviaAfterTitle, - out bool urlHasPointyBrackets)) - { - var wsBeforeLink = new StringSlice(text.Text, triviaBeforeLink.Start, triviaBeforeLink.End); - var wsAfterLink = new StringSlice(text.Text, triviaAfterLink.Start, triviaAfterLink.End); - var wsAfterTitle = new StringSlice(text.Text, triviaAfterTitle.Start, triviaAfterTitle.End); - var unescapedUrl = new StringSlice(text.Text, unescapedUrlSpan.Start, unescapedUrlSpan.End); - var unescapedTitle = new StringSlice(text.Text, unescapedTitleSpan.Start, unescapedTitleSpan.End); - // Inline Link - var link = new LinkInline() - { - TriviaBeforeUrl = wsBeforeLink, - Url = HtmlHelper.Unescape(url), - UnescapedUrl = unescapedUrl, - UrlHasPointyBrackets = urlHasPointyBrackets, - TriviaAfterUrl = wsAfterLink, - Title = HtmlHelper.Unescape(title), - UnescapedTitle = unescapedTitle, - TitleEnclosingCharacter = titleEnclosingCharacter, - TriviaAfterTitle = wsAfterTitle, - IsImage = openParent.IsImage, - LabelSpan = openParent.LabelSpan, - UrlSpan = inlineState.GetSourcePositionFromLocalSpan(linkSpan), - TitleSpan = inlineState.GetSourcePositionFromLocalSpan(titleSpan), - Span = new SourceSpan(openParent.Span.Start, inlineState.GetSourcePosition(text.Start - 1)), - Line = openParent.Line, - Column = openParent.Column, - }; - - openParent.ReplaceBy(link); - // Notifies processor as we are creating an inline locally - inlineState.Inline = link; - - // Process emphasis delimiters - inlineState.PostProcessInlines(0, link, null, false); - - // If we have a link (and not an image), - // we also set all [ delimiters before the opening delimiter to inactive. - // (This will prevent us from getting links within links.) - if (!openParent.IsImage) - { - MarkParentAsInactive(parentDelimiter); - } - - link.IsClosed = true; - - return true; - } + link = TryParseInlineLinkTrivia(ref text, inlineState, openParent); } else { if (LinkHelper.TryParseInlineLink(ref text, out string? url, out string? title, out SourceSpan linkSpan, out SourceSpan titleSpan)) { // Inline Link - var link = new LinkInline() + link = new LinkInline() { Url = HtmlHelper.Unescape(url), Title = HtmlHelper.Unescape(title), @@ -312,34 +257,36 @@ namespace Markdig.Parsers.Inlines Line = openParent.Line, Column = openParent.Column, }; - - openParent.ReplaceBy(link); - // Notifies processor as we are creating an inline locally - inlineState.Inline = link; - - // Process emphasis delimiters - inlineState.PostProcessInlines(0, link, null, false); - - // If we have a link (and not an image), - // we also set all [ delimiters before the opening delimiter to inactive. - // (This will prevent us from getting links within links.) - if (!openParent.IsImage) - { - MarkParentAsInactive(parentDelimiter); - } - - link.IsClosed = true; - - return true; } } + if (link is not null) + { + openParent.ReplaceBy(link); + // Notifies processor as we are creating an inline locally + inlineState.Inline = link; + + // Process emphasis delimiters + inlineState.PostProcessInlines(0, link, null, false); + + // If we have a link (and not an image), + // we also set all [ delimiters before the opening delimiter to inactive. + // (This will prevent us from getting links within links.) + if (!openParent.IsImage) + { + MarkParentAsInactive(parentDelimiter); + } + + link.IsClosed = true; + + return true; + } + text = savedText; } var labelSpan = SourceSpan.Empty; string? label = null; - SourceSpan labelWithTrivia = SourceSpan.Empty; bool isLabelSpanLocal = true; bool isShortcut = false; @@ -363,9 +310,10 @@ namespace Markdig.Parsers.Inlines label = openParent.Label; isShortcut = true; } + if (label != null || LinkHelper.TryParseLabelTrivia(ref text, true, out label, out labelSpan)) { - labelWithTrivia = new SourceSpan(labelSpan.Start, labelSpan.End); + SourceSpan labelWithTrivia = new SourceSpan(labelSpan.Start, labelSpan.End); if (isLabelSpanLocal) { labelSpan = inlineState.GetSourcePositionFromLocalSpan(labelSpan); @@ -399,9 +347,55 @@ namespace Markdig.Parsers.Inlines inlineState.Inline = openParent.ReplaceBy(literal); return false; + + static LinkInline? TryParseInlineLinkTrivia(ref StringSlice text, InlineProcessor inlineState, LinkDelimiterInline openParent) + { + if (LinkHelper.TryParseInlineLinkTrivia( + ref text, + out string? url, + out SourceSpan unescapedUrlSpan, + out string? title, + out SourceSpan unescapedTitleSpan, + out char titleEnclosingCharacter, + out SourceSpan linkSpan, + out SourceSpan titleSpan, + out SourceSpan triviaBeforeLink, + out SourceSpan triviaAfterLink, + out SourceSpan triviaAfterTitle, + out bool urlHasPointyBrackets)) + { + var wsBeforeLink = new StringSlice(text.Text, triviaBeforeLink.Start, triviaBeforeLink.End); + var wsAfterLink = new StringSlice(text.Text, triviaAfterLink.Start, triviaAfterLink.End); + var wsAfterTitle = new StringSlice(text.Text, triviaAfterTitle.Start, triviaAfterTitle.End); + var unescapedUrl = new StringSlice(text.Text, unescapedUrlSpan.Start, unescapedUrlSpan.End); + var unescapedTitle = new StringSlice(text.Text, unescapedTitleSpan.Start, unescapedTitleSpan.End); + + return new LinkInline() + { + TriviaBeforeUrl = wsBeforeLink, + Url = HtmlHelper.Unescape(url), + UnescapedUrl = unescapedUrl, + UrlHasPointyBrackets = urlHasPointyBrackets, + TriviaAfterUrl = wsAfterLink, + Title = HtmlHelper.Unescape(title), + UnescapedTitle = unescapedTitle, + TitleEnclosingCharacter = titleEnclosingCharacter, + TriviaAfterTitle = wsAfterTitle, + IsImage = openParent.IsImage, + LabelSpan = openParent.LabelSpan, + UrlSpan = inlineState.GetSourcePositionFromLocalSpan(linkSpan), + TitleSpan = inlineState.GetSourcePositionFromLocalSpan(titleSpan), + Span = new SourceSpan(openParent.Span.Start, inlineState.GetSourcePosition(text.Start - 1)), + Line = openParent.Line, + Column = openParent.Column, + }; + } + + return null; + } } - private void MarkParentAsInactive(Inline? inline) + private static void MarkParentAsInactive(Inline? inline) { while (inline != null) { From fb9561cf8900f809d50e3bf7183c4378c1f94e3c Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 12 Mar 2022 22:26:41 +0100 Subject: [PATCH 3/9] Fix roughLineCountEstimate calculation (min/max order) --- src/Markdig/Parsers/MarkdownParser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Markdig/Parsers/MarkdownParser.cs b/src/Markdig/Parsers/MarkdownParser.cs index b9e5f42d..9ff353a8 100644 --- a/src/Markdig/Parsers/MarkdownParser.cs +++ b/src/Markdig/Parsers/MarkdownParser.cs @@ -43,8 +43,8 @@ namespace Markdig.Parsers if (pipeline.PreciseSourceLocation) { - int roughLineCountEstimate = text.Length / 40; - roughLineCountEstimate = Math.Min(4, Math.Max(512, roughLineCountEstimate)); + int roughLineCountEstimate = text.Length / 32; + roughLineCountEstimate = Math.Max(4, Math.Min(512, roughLineCountEstimate)); document.LineStartIndexes = new List(roughLineCountEstimate); } From 88cdbf3a17dea9f4c2664b5a6868a3f2ca39368d Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 12 Mar 2022 22:42:15 +0100 Subject: [PATCH 4/9] Lazily allocate CodeBlock.CodeBlockLines --- src/Markdig/Syntax/CodeBlock.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Markdig/Syntax/CodeBlock.cs b/src/Markdig/Syntax/CodeBlock.cs index 62c94e9e..884ac097 100644 --- a/src/Markdig/Syntax/CodeBlock.cs +++ b/src/Markdig/Syntax/CodeBlock.cs @@ -21,7 +21,8 @@ namespace Markdig.Syntax public StringSlice TriviaBefore { get; set; } } - public List CodeBlockLines { get; } = new (); + private List? _codeBlockLines; + public List CodeBlockLines => _codeBlockLines ??= new(); /// /// Initializes a new instance of the class. From 9f734ba3c94853a573066ca3ea8ba05a16447fd7 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 12 Mar 2022 23:28:16 +0100 Subject: [PATCH 5/9] Lazily allocate Trivia properties on LinkInline --- .../Parsers/Inlines/LinkInlineParser.cs | 17 ++++--- src/Markdig/Syntax/Inlines/LinkInline.cs | 47 +++++++++++++------ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs index 48c7c750..9caaf15e 100644 --- a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs @@ -138,18 +138,13 @@ namespace Markdig.Parsers.Inlines // Create a default link if the callback was not found if (link is null) { - var labelWithTrivia = new StringSlice(text.Text, labelWithriviaSpan.Start, labelWithriviaSpan.End); // Inline Link - link = new LinkInline() + var linkInline = new LinkInline() { Url = HtmlHelper.Unescape(linkRef.Url), Title = HtmlHelper.Unescape(linkRef.Title), Label = label, LabelSpan = labelSpan, - LabelWithTrivia = labelWithTrivia, - LinkRefDefLabel = linkRef.Label, - LinkRefDefLabelWithTrivia = linkRef.LabelWithTrivia, - LocalLabel = localLabel, UrlSpan = linkRef.UrlSpan, IsImage = parent.IsImage, IsShortcut = isShortcut, @@ -158,6 +153,16 @@ namespace Markdig.Parsers.Inlines Line = parent.Line, Column = parent.Column, }; + + if (state.TrackTrivia) + { + linkInline.LabelWithTrivia = new StringSlice(text.Text, labelWithriviaSpan.Start, labelWithriviaSpan.End); + linkInline.LinkRefDefLabel = linkRef.Label; + linkInline.LinkRefDefLabelWithTrivia = linkRef.LabelWithTrivia; + linkInline.LocalLabel = localLabel; + } + + link = linkInline; } if (link is ContainerInline containerLink) diff --git a/src/Markdig/Syntax/Inlines/LinkInline.cs b/src/Markdig/Syntax/Inlines/LinkInline.cs index ef9af6ab..fbf6152d 100644 --- a/src/Markdig/Syntax/Inlines/LinkInline.cs +++ b/src/Markdig/Syntax/Inlines/LinkInline.cs @@ -7,12 +7,13 @@ using System.Diagnostics; namespace Markdig.Syntax.Inlines { - public enum LocalLabel + public enum LocalLabel : byte { Local, // [foo][bar] Empty, // [foo][] None, // [foo] } + /// /// A Link inline (Section 6.5 CommonMark specs) /// @@ -20,6 +21,9 @@ namespace Markdig.Syntax.Inlines [DebuggerDisplay("Url: {Url} Title: {Title} Image: {IsImage}")] public class LinkInline : ContainerInline { + private TriviaProperties? _trivia; + private TriviaProperties Trivia => _trivia ??= new(); + /// /// A delegate to use if it is setup on this instance to allow late binding /// of a Url. @@ -65,14 +69,14 @@ namespace Markdig.Syntax.Inlines /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice LabelWithTrivia { get; set; } + public StringSlice LabelWithTrivia { get => _trivia?.LabelWithTrivia ?? StringSlice.Empty; set => Trivia.LabelWithTrivia = value; } /// /// Gets or sets the type of label parsed /// Trivia: only parsed when is enabled, otherwise /// . /// - public LocalLabel LocalLabel { get; set; } + public LocalLabel LocalLabel { get => _trivia?.LocalLabel ?? LocalLabel.None; set => Trivia.LocalLabel = value; } /// /// Gets or sets the reference this link is attached to. May be null. @@ -81,21 +85,22 @@ namespace Markdig.Syntax.Inlines /// /// Gets or sets the label as matched against the . + /// Trivia: only parsed when is enabled. /// - public string? LinkRefDefLabel { get; set; } + public string? LinkRefDefLabel { get => _trivia?.LinkRefDefLabel; set => Trivia.LinkRefDefLabel = value; } /// /// Gets or sets the with trivia as matched against /// the /// - public StringSlice LinkRefDefLabelWithTrivia { get; set; } + public StringSlice LinkRefDefLabelWithTrivia { get => _trivia?.LinkRefDefLabelWithTrivia ?? StringSlice.Empty; set => Trivia.LinkRefDefLabelWithTrivia = value; } /// /// Gets or sets the trivia before the . /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice TriviaBeforeUrl { get; set; } + public StringSlice TriviaBeforeUrl { get => _trivia?.TriviaBeforeUrl ?? StringSlice.Empty; set => Trivia.TriviaBeforeUrl = value; } /// /// True if the in the source document is enclosed @@ -103,7 +108,7 @@ namespace Markdig.Syntax.Inlines /// Trivia: only parsed when is enabled, otherwise /// false. /// - public bool UrlHasPointyBrackets { get; set; } + public bool UrlHasPointyBrackets { get => _trivia?.UrlHasPointyBrackets ?? false; set => Trivia.UrlHasPointyBrackets = value; } /// /// Gets or sets the URL. @@ -120,14 +125,14 @@ namespace Markdig.Syntax.Inlines /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice UnescapedUrl { get; set; } + public StringSlice UnescapedUrl { get => _trivia?.UnescapedUrl ?? StringSlice.Empty; set => Trivia.UnescapedUrl = value; } /// /// Any trivia after the . /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice TriviaAfterUrl { get; set; } + public StringSlice TriviaAfterUrl { get => _trivia?.TriviaAfterUrl ?? StringSlice.Empty; set => Trivia.TriviaAfterUrl = value; } /// /// Gets or sets the GetDynamicUrl delegate. If this property is set, @@ -137,10 +142,9 @@ namespace Markdig.Syntax.Inlines /// /// Gets or sets the character used to enclose the . - /// Trivia: only parsed when is enabled, otherwise - /// . + /// Trivia: only parsed when is enabled. /// - public char TitleEnclosingCharacter { get; set; } + public char TitleEnclosingCharacter { get => _trivia?.TitleEnclosingCharacter ?? default; set => Trivia.TitleEnclosingCharacter = value; } /// /// Gets or sets the title. @@ -158,14 +162,14 @@ namespace Markdig.Syntax.Inlines /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice UnescapedTitle { get; set; } + public StringSlice UnescapedTitle { get => _trivia?.UnescapedTitle ?? StringSlice.Empty; set => Trivia.UnescapedTitle = value; } /// /// Gets or sets the trivia after the . /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice TriviaAfterTitle { get; set; } + public StringSlice TriviaAfterTitle { get => _trivia?.TriviaAfterTitle ?? StringSlice.Empty; set => Trivia.TriviaAfterTitle = value; } /// /// Gets or sets a boolean indicating if this link is a shortcut link to a @@ -176,5 +180,20 @@ namespace Markdig.Syntax.Inlines /// Gets or sets a boolean indicating whether the inline link was parsed using markdown syntax or was automatic recognized. /// public bool IsAutoLink { get; set; } + + private sealed class TriviaProperties + { + public StringSlice LabelWithTrivia; + public LocalLabel LocalLabel; + public string? LinkRefDefLabel; + public StringSlice LinkRefDefLabelWithTrivia; + public StringSlice TriviaBeforeUrl; + public bool UrlHasPointyBrackets; + public StringSlice UnescapedUrl; + public StringSlice TriviaAfterUrl; + public char TitleEnclosingCharacter; + public StringSlice UnescapedTitle; + public StringSlice TriviaAfterTitle; + } } } From b697a03c2b84dc77a6825362ecff2c3a338eab28 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 12 Mar 2022 23:34:30 +0100 Subject: [PATCH 6/9] Lazily allocate Trivia properties on LinkReferenceDefinition --- src/Markdig/Syntax/LinkReferenceDefinition.cs | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Markdig/Syntax/LinkReferenceDefinition.cs b/src/Markdig/Syntax/LinkReferenceDefinition.cs index 49d8e7c3..a9479315 100644 --- a/src/Markdig/Syntax/LinkReferenceDefinition.cs +++ b/src/Markdig/Syntax/LinkReferenceDefinition.cs @@ -16,6 +16,9 @@ namespace Markdig.Syntax /// public class LinkReferenceDefinition : LeafBlock { + private TriviaProperties? _trivia; + private TriviaProperties Trivia => _trivia ??= new(); + /// /// Creates an inline link for the specified . /// @@ -62,14 +65,14 @@ namespace Markdig.Syntax /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice LabelWithTrivia { get; set; } + public StringSlice LabelWithTrivia { get => _trivia?.LabelWithTrivia ?? StringSlice.Empty; set => Trivia.LabelWithTrivia = value; } /// /// Whitespace before the . /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice TriviaBeforeUrl { get; set; } + public StringSlice TriviaBeforeUrl { get => _trivia?.TriviaBeforeUrl ?? StringSlice.Empty; set => Trivia.TriviaBeforeUrl = value; } /// /// Gets or sets the URL. @@ -86,21 +89,21 @@ namespace Markdig.Syntax /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice UnescapedUrl { get; set; } + public StringSlice UnescapedUrl { get => _trivia?.UnescapedUrl ?? StringSlice.Empty; set => Trivia.UnescapedUrl = value; } /// /// True when the is enclosed in point brackets in the source document. /// Trivia: only parsed when is enabled, otherwise /// false. /// - public bool UrlHasPointyBrackets { get; set; } + public bool UrlHasPointyBrackets { get => _trivia?.UrlHasPointyBrackets ?? false; set => Trivia.UrlHasPointyBrackets = value; } /// /// gets or sets the whitespace before a . /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice TriviaBeforeTitle { get; set; } + public StringSlice TriviaBeforeTitle { get => _trivia?.TriviaBeforeTitle ?? StringSlice.Empty; set => Trivia.TriviaBeforeTitle = value; } /// /// Gets or sets the title. @@ -117,13 +120,13 @@ namespace Markdig.Syntax /// Trivia: only parsed when is enabled, otherwise /// . /// - public StringSlice UnescapedTitle { get; set; } + public StringSlice UnescapedTitle { get => _trivia?.UnescapedTitle ?? StringSlice.Empty; set => Trivia.UnescapedTitle = value; } /// /// Gets or sets the character the is enclosed in. /// Trivia: only parsed when is enabled, otherwise \0. /// - public char TitleEnclosingCharacter { get; set; } + public char TitleEnclosingCharacter { get => _trivia?.TitleEnclosingCharacter ?? default; set => Trivia.TitleEnclosingCharacter = value; } /// /// Gets or sets the create link inline callback for this instance. @@ -227,5 +230,16 @@ namespace Markdig.Syntax }; return true; } + + private sealed class TriviaProperties + { + public StringSlice LabelWithTrivia; + public StringSlice TriviaBeforeUrl; + public StringSlice UnescapedUrl; + public bool UrlHasPointyBrackets; + public StringSlice TriviaBeforeTitle; + public StringSlice UnescapedTitle; + public char TitleEnclosingCharacter; + } } } \ No newline at end of file From 61452c91e9ecba90fa9d7c9279b87c80042171a8 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sun, 13 Mar 2022 01:21:33 +0100 Subject: [PATCH 7/9] Aggressively avoid TrackTrivia work and allocations when not requested --- src/Markdig/Parsers/FencedBlockParserBase.cs | 10 +- src/Markdig/Parsers/FencedCodeBlockParser.cs | 14 ++- src/Markdig/Parsers/HeadingBlockParser.cs | 17 ++- src/Markdig/Parsers/HtmlBlockParser.cs | 14 ++- .../Parsers/IndentedCodeBlockParser.cs | 30 +++-- src/Markdig/Parsers/ListBlockParser.cs | 28 +++-- src/Markdig/Parsers/ParagraphBlockParser.cs | 30 +++-- src/Markdig/Parsers/QuoteBlockParser.cs | 107 +++++++++++------- src/Markdig/Parsers/ThematicBreakParser.cs | 14 ++- src/Markdig/Syntax/Block.cs | 34 ++++-- src/Markdig/Syntax/FencedCodeBlock.cs | 28 +++-- src/Markdig/Syntax/HeadingBlock.cs | 16 ++- src/Markdig/Syntax/LinkReferenceDefinition.cs | 4 +- src/Markdig/Syntax/ListItemBlock.cs | 12 +- src/Markdig/Syntax/QuoteBlock.cs | 20 +--- 15 files changed, 250 insertions(+), 128 deletions(-) diff --git a/src/Markdig/Parsers/FencedBlockParserBase.cs b/src/Markdig/Parsers/FencedBlockParserBase.cs index 7462d9f1..1097560e 100644 --- a/src/Markdig/Parsers/FencedBlockParserBase.cs +++ b/src/Markdig/Parsers/FencedBlockParserBase.cs @@ -332,9 +332,13 @@ namespace Markdig.Parsers var fencedBlock = (IFencedBlock)block; fencedBlock.ClosingFencedCharCount = closingCount; - fencedBlock.NewLine = processor.Line.NewLine; - fencedBlock.TriviaBeforeClosingFence = processor.UseTrivia(sourcePosition - 1); - fencedBlock.TriviaAfter = new StringSlice(processor.Line.Text, lastFenceCharPosition, endBeforeTrim); + + if (processor.TrackTrivia) + { + fencedBlock.NewLine = processor.Line.NewLine; + fencedBlock.TriviaBeforeClosingFence = processor.UseTrivia(sourcePosition - 1); + fencedBlock.TriviaAfter = new StringSlice(processor.Line.Text, lastFenceCharPosition, endBeforeTrim); + } // Don't keep the last line return BlockState.BreakDiscard; diff --git a/src/Markdig/Parsers/FencedCodeBlockParser.cs b/src/Markdig/Parsers/FencedCodeBlockParser.cs index bbfafa2c..fdfa7d06 100644 --- a/src/Markdig/Parsers/FencedCodeBlockParser.cs +++ b/src/Markdig/Parsers/FencedCodeBlockParser.cs @@ -26,13 +26,19 @@ namespace Markdig.Parsers protected override FencedCodeBlock CreateFencedBlock(BlockProcessor processor) { - return new FencedCodeBlock(this) + var codeBlock = new FencedCodeBlock(this) { IndentCount = processor.Indent, - LinesBefore = processor.UseLinesBefore(), - TriviaBefore = processor.UseTrivia(processor.Start - 1), - NewLine = processor.Line.NewLine, }; + + if (processor.TrackTrivia) + { + codeBlock.LinesBefore = processor.UseLinesBefore(); + codeBlock.TriviaBefore = processor.UseTrivia(processor.Start - 1); + codeBlock.NewLine = processor.Line.NewLine; + } + + return codeBlock; } public override BlockState TryContinue(BlockProcessor processor, Block block) diff --git a/src/Markdig/Parsers/HeadingBlockParser.cs b/src/Markdig/Parsers/HeadingBlockParser.cs index 6c9781ae..8b619d7a 100644 --- a/src/Markdig/Parsers/HeadingBlockParser.cs +++ b/src/Markdig/Parsers/HeadingBlockParser.cs @@ -82,20 +82,25 @@ namespace Markdig.Parsers var headingBlock = new HeadingBlock(this) { HeaderChar = matchingChar, - TriviaAfterAtxHeaderChar = trivia, Level = leadingCount, Column = column, Span = { Start = sourcePosition }, - TriviaBefore = processor.UseTrivia(sourcePosition - 1), - LinesBefore = processor.UseLinesBefore(), - NewLine = processor.Line.NewLine, }; - processor.NewBlocks.Push(headingBlock); - if (!processor.TrackTrivia) + + if (processor.TrackTrivia) + { + headingBlock.TriviaAfterAtxHeaderChar = trivia; + headingBlock.TriviaBefore = processor.UseTrivia(sourcePosition - 1); + headingBlock.LinesBefore = processor.UseLinesBefore(); + headingBlock.NewLine = processor.Line.NewLine; + } + else { processor.GoToColumn(column + leadingCount + 1); } + processor.NewBlocks.Push(headingBlock); + // Gives a chance to parse attributes TryParseAttributes?.Invoke(processor, ref processor.Line, headingBlock); diff --git a/src/Markdig/Parsers/HtmlBlockParser.cs b/src/Markdig/Parsers/HtmlBlockParser.cs index 1a02999a..2dc28dda 100644 --- a/src/Markdig/Parsers/HtmlBlockParser.cs +++ b/src/Markdig/Parsers/HtmlBlockParser.cs @@ -270,16 +270,22 @@ namespace Markdig.Parsers private BlockState CreateHtmlBlock(BlockProcessor state, HtmlBlockType type, int startColumn, int startPosition) { - state.NewBlocks.Push(new HtmlBlock(this) + var htmlBlock = new HtmlBlock(this) { Column = startColumn, Type = type, // By default, setup to the end of line Span = new SourceSpan(startPosition, startPosition + state.Line.End), //BeforeWhitespace = state.PopBeforeWhitespace(startPosition - 1), - LinesBefore = state.UseLinesBefore(), - NewLine = state.Line.NewLine, - }); + }; + + if (state.TrackTrivia) + { + htmlBlock.LinesBefore = state.UseLinesBefore(); + htmlBlock.NewLine = state.Line.NewLine; + } + + state.NewBlocks.Push(htmlBlock); return BlockState.Continue; } diff --git a/src/Markdig/Parsers/IndentedCodeBlockParser.cs b/src/Markdig/Parsers/IndentedCodeBlockParser.cs index 416c560d..f1325b67 100644 --- a/src/Markdig/Parsers/IndentedCodeBlockParser.cs +++ b/src/Markdig/Parsers/IndentedCodeBlockParser.cs @@ -36,9 +36,14 @@ namespace Markdig.Parsers { Column = processor.Column, Span = new SourceSpan(processor.Start, processor.Line.End), - LinesBefore = processor.UseLinesBefore(), - NewLine = processor.Line.NewLine, }; + + if (processor.TrackTrivia) + { + codeBlock.LinesBefore = processor.UseLinesBefore(); + codeBlock.NewLine = processor.Line.NewLine; + } + var codeBlockLine = new CodeBlockLine { TriviaBefore = processor.UseTrivia(sourceStartPosition - 1) @@ -68,8 +73,12 @@ namespace Markdig.Parsers if (line.Slice.IsEmpty) { codeBlock.Lines.RemoveAt(i); - processor.LinesBefore ??= new List(); - processor.LinesBefore.Add(line.Slice); + + if (processor.TrackTrivia) + { + processor.LinesBefore ??= new List(); + processor.LinesBefore.Add(line.Slice); + } } else { @@ -92,12 +101,15 @@ namespace Markdig.Parsers // lines var cb = (CodeBlock)block; - var codeBlockLine = new CodeBlockLine - { - TriviaBefore = processor.UseTrivia(processor.Start - 1) - }; + var codeBlockLine = new CodeBlockLine(); + cb.CodeBlockLines.Add(codeBlockLine); - cb.NewLine = processor.Line.NewLine; // ensure block newline is last newline + + if (processor.TrackTrivia) + { + codeBlockLine.TriviaBefore = processor.UseTrivia(processor.Start - 1); + cb.NewLine = processor.Line.NewLine; // ensure block newline is last newline + } } return BlockState.Continue; diff --git a/src/Markdig/Parsers/ListBlockParser.cs b/src/Markdig/Parsers/ListBlockParser.cs index 0906faae..8f220ebe 100644 --- a/src/Markdig/Parsers/ListBlockParser.cs +++ b/src/Markdig/Parsers/ListBlockParser.cs @@ -93,8 +93,12 @@ namespace Markdig.Parsers { // TODO: We remove the thematic break, as it will be created later, but this is inefficient, try to find another way var thematicBreak = processor.NewBlocks.Pop(); - var linesBefore = thematicBreak.LinesBefore; - processor.LinesBefore = linesBefore; + + if (processor.TrackTrivia) + { + processor.LinesBefore = thematicBreak.LinesBefore; + } + return BlockState.None; } } @@ -276,12 +280,17 @@ namespace Markdig.Parsers Column = initColumn, ColumnWidth = columnWidth, Order = order, - SourceBullet = listInfo.SourceBullet, - TriviaBefore = triviaBefore, Span = new SourceSpan(sourcePosition, sourceEndPosition), - LinesBefore = state.UseLinesBefore(), - NewLine = state.Line.NewLine, }; + + if (state.TrackTrivia) + { + newListItem.TriviaBefore = triviaBefore; + newListItem.LinesBefore = state.UseLinesBefore(); + newListItem.NewLine = state.Line.NewLine; + newListItem.SourceBullet = listInfo.SourceBullet; + } + state.NewBlocks.Push(newListItem); if (currentParent != null) @@ -313,8 +322,13 @@ namespace Markdig.Parsers OrderedDelimiter = listInfo.OrderedDelimiter, DefaultOrderedStart = listInfo.DefaultOrderedStart, OrderedStart = listInfo.OrderedStart, - LinesBefore = state.UseLinesBefore(), }; + + if (state.TrackTrivia) + { + newList.LinesBefore = state.UseLinesBefore(); + } + state.NewBlocks.Push(newList); } return BlockState.Continue; diff --git a/src/Markdig/Parsers/ParagraphBlockParser.cs b/src/Markdig/Parsers/ParagraphBlockParser.cs index 3711513c..f27e6e29 100644 --- a/src/Markdig/Parsers/ParagraphBlockParser.cs +++ b/src/Markdig/Parsers/ParagraphBlockParser.cs @@ -23,13 +23,19 @@ namespace Markdig.Parsers } // We continue trying to match by default - processor.NewBlocks.Push(new ParagraphBlock(this) + var paragraph = new ParagraphBlock(this) { Column = processor.Column, Span = new SourceSpan(processor.Line.Start, processor.Line.End), - LinesBefore = processor.UseLinesBefore(), - NewLine = processor.Line.NewLine, - }); + }; + + if (processor.TrackTrivia) + { + paragraph.LinesBefore = processor.UseLinesBefore(); + paragraph.NewLine = processor.Line.NewLine; + } + + processor.NewBlocks.Push(paragraph); return BlockState.Continue; } @@ -128,15 +134,19 @@ namespace Markdig.Parsers Span = new SourceSpan(paragraph.Span.Start, line.Start), Level = level, Lines = paragraph.Lines, - TriviaBefore = state.UseTrivia(sourcePosition - 1), // remove dashes - TriviaAfter = new StringSlice(state.Line.Text, state.Start, line.End), - LinesBefore = paragraph.LinesBefore, - NewLine = state.Line.NewLine, IsSetext = true, HeaderCharCount = count, - SetextNewline = paragraph.NewLine, }; - if (!state.TrackTrivia) + + if (state.TrackTrivia) + { + heading.LinesBefore = paragraph.LinesBefore; + heading.TriviaBefore = state.UseTrivia(sourcePosition - 1); // remove dashes + heading.TriviaAfter = new StringSlice(state.Line.Text, state.Start, line.End); + heading.NewLine = state.Line.NewLine; + heading.SetextNewline = paragraph.NewLine; + } + else { heading.Lines.Trim(); } diff --git a/src/Markdig/Parsers/QuoteBlockParser.cs b/src/Markdig/Parsers/QuoteBlockParser.cs index f16c54ff..7b6bc96b 100644 --- a/src/Markdig/Parsers/QuoteBlockParser.cs +++ b/src/Markdig/Parsers/QuoteBlockParser.cs @@ -41,9 +41,13 @@ namespace Markdig.Parsers QuoteChar = quoteChar, Column = column, Span = new SourceSpan(sourcePosition, processor.Line.End), - LinesBefore = processor.UseLinesBefore() }; + if (processor.TrackTrivia) + { + quoteBlock.LinesBefore = processor.UseLinesBefore(); + } + bool hasSpaceAfterQuoteChar = false; if (c == ' ') { @@ -56,28 +60,34 @@ namespace Markdig.Parsers processor.NextColumn(); } - var triviaBefore = processor.UseTrivia(sourcePosition - 1); - StringSlice triviaAfter = StringSlice.Empty; - bool wasEmptyLine = false; - if (processor.Line.IsEmptyOrWhitespace()) + if (processor.TrackTrivia) { - processor.TriviaStart = processor.Start; - triviaAfter = processor.UseTrivia(processor.Line.End); - wasEmptyLine = true; + var triviaBefore = processor.UseTrivia(sourcePosition - 1); + StringSlice triviaAfter = StringSlice.Empty; + bool wasEmptyLine = false; + if (processor.Line.IsEmptyOrWhitespace()) + { + processor.TriviaStart = processor.Start; + triviaAfter = processor.UseTrivia(processor.Line.End); + wasEmptyLine = true; + } + + if (!wasEmptyLine) + { + processor.TriviaStart = processor.Start; + } + + quoteBlock.QuoteLines.Add(new QuoteBlockLine + { + TriviaBefore = triviaBefore, + TriviaAfter = triviaAfter, + QuoteChar = true, + HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar, + NewLine = processor.Line.NewLine, + }); } - quoteBlock.QuoteLines.Add(new QuoteBlockLine - { - TriviaBefore = triviaBefore, - TriviaAfter = triviaAfter, - QuoteChar = true, - HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar, - NewLine = processor.Line.NewLine, - }); + processor.NewBlocks.Push(quoteBlock); - if (!wasEmptyLine) - { - processor.TriviaStart = processor.Start; - } return BlockState.Continue; } @@ -94,7 +104,6 @@ namespace Markdig.Parsers // 5.1 Block quotes // A block quote marker consists of 0-3 spaces of initial indent, plus (a) the character > together with a following space, or (b) a single character > not followed by a space. var c = processor.CurrentChar; - bool hasSpaceAfterQuoteChar = false; if (c != quote.QuoteChar) { if (processor.IsBlankLine) @@ -103,14 +112,19 @@ namespace Markdig.Parsers } else { - quote.QuoteLines.Add(new QuoteBlockLine + if (processor.TrackTrivia) { - QuoteChar = false, - NewLine = processor.Line.NewLine, - }); + quote.QuoteLines.Add(new QuoteBlockLine + { + QuoteChar = false, + NewLine = processor.Line.NewLine, + }); + } return BlockState.None; } } + + bool hasSpaceAfterQuoteChar = false; c = processor.NextChar(); // Skip quote marker char if (c == ' ') { @@ -122,28 +136,33 @@ namespace Markdig.Parsers { processor.NextColumn(); } - var TriviaSpaceBefore = processor.UseTrivia(sourcePosition - 1); - StringSlice triviaAfter = StringSlice.Empty; - bool wasEmptyLine = false; - if (processor.Line.IsEmptyOrWhitespace()) - { - processor.TriviaStart = processor.Start; - triviaAfter = processor.UseTrivia(processor.Line.End); - wasEmptyLine = true; - } - quote.QuoteLines.Add(new QuoteBlockLine - { - QuoteChar = true, - HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar, - TriviaBefore = TriviaSpaceBefore, - TriviaAfter = triviaAfter, - NewLine = processor.Line.NewLine, - }); - if (!wasEmptyLine) + if (processor.TrackTrivia) { - processor.TriviaStart = processor.Start; + var triviaSpaceBefore = processor.UseTrivia(sourcePosition - 1); + StringSlice triviaAfter = StringSlice.Empty; + bool wasEmptyLine = false; + if (processor.Line.IsEmptyOrWhitespace()) + { + processor.TriviaStart = processor.Start; + triviaAfter = processor.UseTrivia(processor.Line.End); + wasEmptyLine = true; + } + quote.QuoteLines.Add(new QuoteBlockLine + { + QuoteChar = true, + HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar, + TriviaBefore = triviaSpaceBefore, + TriviaAfter = triviaAfter, + NewLine = processor.Line.NewLine, + }); + + if (!wasEmptyLine) + { + processor.TriviaStart = processor.Start; + } } + block.UpdateSpanEnd(processor.Line.End); return BlockState.Continue; } diff --git a/src/Markdig/Parsers/ThematicBreakParser.cs b/src/Markdig/Parsers/ThematicBreakParser.cs index 1488fd44..9ccfa994 100644 --- a/src/Markdig/Parsers/ThematicBreakParser.cs +++ b/src/Markdig/Parsers/ThematicBreakParser.cs @@ -85,7 +85,7 @@ namespace Markdig.Parsers } // Push a new block - processor.NewBlocks.Push(new ThematicBreakBlock(this) + var thematicBreak = new ThematicBreakBlock(this) { Column = processor.Column, Span = new SourceSpan(startPosition, line.End), @@ -94,10 +94,16 @@ namespace Markdig.Parsers // TODO: should we separate whitespace before/after? //BeforeWhitespace = beforeWhitespace, //AfterWhitespace = processor.PopBeforeWhitespace(processor.CurrentLineStartPosition), - LinesBefore = processor.UseLinesBefore(), Content = new StringSlice(line.Text, processor.TriviaStart, line.End, line.NewLine), //include whitespace for now - NewLine = processor.Line.NewLine, - }); + }; + + if (processor.TrackTrivia) + { + thematicBreak.LinesBefore = processor.UseLinesBefore(); + thematicBreak.NewLine = processor.Line.NewLine; + } + + processor.NewBlocks.Push(thematicBreak); return BlockState.BreakDiscard; } } diff --git a/src/Markdig/Syntax/Block.cs b/src/Markdig/Syntax/Block.cs index 142e8e01..62710446 100644 --- a/src/Markdig/Syntax/Block.cs +++ b/src/Markdig/Syntax/Block.cs @@ -14,6 +14,9 @@ namespace Markdig.Syntax /// public abstract class Block : MarkdownObject, IBlock { + private BlockTriviaProperties? _trivia; + private BlockTriviaProperties Trivia => _trivia ??= new(); + /// /// Initializes a new instance of the class. /// @@ -28,7 +31,7 @@ namespace Markdig.Syntax /// /// Gets the parent of this container. May be null. /// - public ContainerBlock? Parent { get; internal set; } + public ContainerBlock? Parent { get; internal set; } /// /// Gets the parser associated to this instance. @@ -46,7 +49,8 @@ namespace Markdig.Syntax public bool IsBreakable { get; set; } /// - /// The last newline of this block + /// The last newline of this block. + /// Trivia: only parsed when is enabled /// public NewLine NewLine { get; set; } @@ -58,28 +62,28 @@ namespace Markdig.Syntax /// /// Gets or sets the trivia right before this block. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// - public StringSlice TriviaBefore { get; set; } + public StringSlice TriviaBefore { get => _trivia?.TriviaBefore ?? StringSlice.Empty; set => Trivia.TriviaBefore = value; } /// /// Gets or sets trivia occurring after this block. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// - public StringSlice TriviaAfter { get; set; } + public StringSlice TriviaAfter { get => _trivia?.TriviaAfter ?? StringSlice.Empty; set => Trivia.TriviaAfter = value; } /// /// Gets or sets the empty lines occurring before this block. /// Trivia: only parsed when is enabled, otherwise null. /// - public List? LinesBefore { get; set; } + public List? LinesBefore { get => _trivia?.LinesBefore; set => Trivia.LinesBefore = value; } /// /// Gets or sets the empty lines occurring after this block. /// Trivia: only parsed when is enabled, otherwise null. /// - public List? LinesAfter { get; set; } + public List? LinesAfter { get => _trivia?.LinesAfter; set => Trivia.LinesAfter = value; } /// /// Occurs when the process of inlines begin. @@ -134,5 +138,19 @@ namespace Markdig.Syntax } return block; } + + private protected T? TryGetDerivedTrivia() where T : class => _trivia?.DerivedTriviaSlot as T; + private protected T GetOrSetDerivedTrivia() where T : new() => (T)(Trivia.DerivedTriviaSlot ??= new T()); + + private sealed class BlockTriviaProperties + { + // Used by derived types to store their own TriviaProperties + public object? DerivedTriviaSlot; + + public StringSlice TriviaBefore; + public StringSlice TriviaAfter; + public List? LinesBefore; + public List? LinesAfter; + } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/FencedCodeBlock.cs b/src/Markdig/Syntax/FencedCodeBlock.cs index 6ef4aa4e..b38b351c 100644 --- a/src/Markdig/Syntax/FencedCodeBlock.cs +++ b/src/Markdig/Syntax/FencedCodeBlock.cs @@ -15,6 +15,9 @@ namespace Markdig.Syntax /// public class FencedCodeBlock : CodeBlock, IFencedBlock { + private TriviaProperties? _trivia => TryGetDerivedTrivia(); + private TriviaProperties Trivia => GetOrSetDerivedTrivia(); + /// /// Initializes a new instance of the class. /// @@ -41,33 +44,44 @@ namespace Markdig.Syntax public int OpeningFencedCharCount { get; set; } /// - public StringSlice TriviaAfterFencedChar { get; set; } + public StringSlice TriviaAfterFencedChar { get => _trivia?.TriviaAfterFencedChar ?? StringSlice.Empty; set => Trivia.TriviaAfterFencedChar = value; } /// public string? Info { get; set; } /// - public StringSlice UnescapedInfo { get; set; } + public StringSlice UnescapedInfo { get => _trivia?.UnescapedInfo ?? StringSlice.Empty; set => Trivia.UnescapedInfo = value; } /// - public StringSlice TriviaAfterInfo { get; set; } + public StringSlice TriviaAfterInfo { get => _trivia?.TriviaAfterInfo ?? StringSlice.Empty; set => Trivia.TriviaAfterInfo = value; } /// public string? Arguments { get; set; } /// - public StringSlice UnescapedArguments { get; set; } + public StringSlice UnescapedArguments { get => _trivia?.UnescapedArguments ?? StringSlice.Empty; set => Trivia.UnescapedArguments = value; } /// - public StringSlice TriviaAfterArguments { get; set; } + public StringSlice TriviaAfterArguments { get => _trivia?.TriviaAfterArguments ?? StringSlice.Empty; set => Trivia.TriviaAfterArguments = value; } /// - public NewLine InfoNewLine { get; set; } + public NewLine InfoNewLine { get => _trivia?.InfoNewLine ?? NewLine.None; set => Trivia.InfoNewLine = value; } /// - public StringSlice TriviaBeforeClosingFence { get; set; } + public StringSlice TriviaBeforeClosingFence { get => _trivia?.TriviaBeforeClosingFence ?? StringSlice.Empty; set => Trivia.TriviaBeforeClosingFence = value; } /// public int ClosingFencedCharCount { get; set; } + + private sealed class TriviaProperties + { + public StringSlice TriviaAfterFencedChar; + public StringSlice UnescapedInfo; + public StringSlice TriviaAfterInfo; + public StringSlice UnescapedArguments; + public StringSlice TriviaAfterArguments; + public NewLine InfoNewLine; + public StringSlice TriviaBeforeClosingFence; + } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/HeadingBlock.cs b/src/Markdig/Syntax/HeadingBlock.cs index 8fef5faf..c6d18a5a 100644 --- a/src/Markdig/Syntax/HeadingBlock.cs +++ b/src/Markdig/Syntax/HeadingBlock.cs @@ -14,6 +14,9 @@ namespace Markdig.Syntax [DebuggerDisplay("{GetType().Name} Line: {Line}, {Lines} Level: {Level}")] public class HeadingBlock : LeafBlock { + private TriviaProperties? _trivia => TryGetDerivedTrivia(); + private TriviaProperties Trivia => GetOrSetDerivedTrivia(); + /// /// Initializes a new instance of the class. /// @@ -45,14 +48,21 @@ namespace Markdig.Syntax /// /// Gets or sets the newline of the first line when is true. + /// Trivia: only parsed when is enabled. /// - public NewLine SetextNewline { get; set; } + public NewLine SetextNewline { get => _trivia?.SetextNewline ?? NewLine.None; set => Trivia.SetextNewline = value; } /// /// Gets or sets the whitespace after the # character when is false. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// - public StringSlice TriviaAfterAtxHeaderChar { get; set; } + public StringSlice TriviaAfterAtxHeaderChar { get => _trivia?.TriviaAfterAtxHeaderChar ?? StringSlice.Empty; set => Trivia.TriviaAfterAtxHeaderChar = value; } + + private sealed class TriviaProperties + { + public NewLine SetextNewline; + public StringSlice TriviaAfterAtxHeaderChar; + } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/LinkReferenceDefinition.cs b/src/Markdig/Syntax/LinkReferenceDefinition.cs index a9479315..5efa59a1 100644 --- a/src/Markdig/Syntax/LinkReferenceDefinition.cs +++ b/src/Markdig/Syntax/LinkReferenceDefinition.cs @@ -16,8 +16,8 @@ namespace Markdig.Syntax /// public class LinkReferenceDefinition : LeafBlock { - private TriviaProperties? _trivia; - private TriviaProperties Trivia => _trivia ??= new(); + private TriviaProperties? _trivia => TryGetDerivedTrivia(); + private TriviaProperties Trivia => GetOrSetDerivedTrivia(); /// /// Creates an inline link for the specified . diff --git a/src/Markdig/Syntax/ListItemBlock.cs b/src/Markdig/Syntax/ListItemBlock.cs index 2dff8bbe..dda923df 100644 --- a/src/Markdig/Syntax/ListItemBlock.cs +++ b/src/Markdig/Syntax/ListItemBlock.cs @@ -13,6 +13,9 @@ namespace Markdig.Syntax /// public class ListItemBlock : ContainerBlock { + private TriviaProperties? _trivia => TryGetDerivedTrivia(); + private TriviaProperties Trivia => GetOrSetDerivedTrivia(); + /// /// Initializes a new instance of the class. /// @@ -31,8 +34,13 @@ namespace Markdig.Syntax /// /// Gets or sets the bullet as parsed in the source document. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// - public StringSlice SourceBullet { get; set; } + public StringSlice SourceBullet { get => _trivia?.SourceBullet ?? StringSlice.Empty; set => Trivia.SourceBullet = value; } + + private sealed class TriviaProperties + { + public StringSlice SourceBullet; + } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/QuoteBlock.cs b/src/Markdig/Syntax/QuoteBlock.cs index 2de6d131..51116bdc 100644 --- a/src/Markdig/Syntax/QuoteBlock.cs +++ b/src/Markdig/Syntax/QuoteBlock.cs @@ -14,6 +14,8 @@ namespace Markdig.Syntax /// public class QuoteBlock : ContainerBlock { + private List Trivia => GetOrSetDerivedTrivia>(); + /// /// Initializes a new instance of the class. /// @@ -24,10 +26,9 @@ namespace Markdig.Syntax /// /// Gets or sets the trivia per line of this QuoteBlock. - /// Trivia: only parsed when is enabled, otherwise - /// . + /// Trivia: only parsed when is enabled, otherwise null. /// - public List QuoteLines { get; } = new (); + public List QuoteLines => Trivia; /// /// Gets or sets the quote character (usually `>`) @@ -37,30 +38,23 @@ namespace Markdig.Syntax /// /// Represents trivia per line part of a QuoteBlock. - /// Trivia: only parsed when is enabled, otherwise - /// is empty. + /// Trivia: only parsed when is enabled. /// public class QuoteBlockLine { /// /// Gets or sets trivia occuring before the first quote character. - /// Trivia: only parsed when is enabled, otherwise - /// is empty. /// public StringSlice TriviaBefore { get; set; } /// /// True when this QuoteBlock line has a quote character. False when /// this line is a "lazy line". - /// Trivia: only parsed when is enabled, otherwise - /// is empty. /// public bool QuoteChar { get; set; } /// /// True if a space is parsed right after the quote character. - /// Trivia: only parsed when is enabled, otherwise - /// is empty. /// public bool HasSpaceAfterQuoteChar { get; set; } @@ -68,15 +62,11 @@ namespace Markdig.Syntax /// Gets or sets the trivia after the the space after the quote character. /// The first space is assigned to , subsequent /// trivia is assigned to this property. - /// Trivia: only parsed when is enabled, otherwise - /// is empty. /// public StringSlice TriviaAfter { get; set; } /// /// Gets or sets the newline of this QuoeBlockLine. - /// Trivia: only parsed when is enabled, otherwise - /// is empty. /// public NewLine NewLine { get; set; } } From 6f75b5156c079294d56683be2711438bf70e668d Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sun, 13 Mar 2022 01:34:47 +0100 Subject: [PATCH 8/9] Aggressively avoid TrackTrivia work and allocations when not requested for Inlines --- .../Parsers/Inlines/CodeInlineParser.cs | 10 ++++++++-- .../Parsers/Inlines/LinkInlineParser.cs | 11 ++++++++--- src/Markdig/Syntax/Inlines/CodeInline.cs | 18 +++++++++--------- .../Syntax/Inlines/LinkDelimiterInline.cs | 12 ++++++++++-- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Markdig/Parsers/Inlines/CodeInlineParser.cs b/src/Markdig/Parsers/Inlines/CodeInlineParser.cs index cd243ec6..3292b7b6 100644 --- a/src/Markdig/Parsers/Inlines/CodeInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/CodeInlineParser.cs @@ -113,15 +113,21 @@ namespace Markdig.Parsers.Inlines int delimiterCount = Math.Min(openSticks, closeSticks); var spanStart = processor.GetSourcePosition(startPosition, out int line, out int column); var spanEnd = processor.GetSourcePosition(slice.Start - 1); - processor.Inline = new CodeInline(content) + var codeInline = new CodeInline(content) { Delimiter = match, - ContentWithTrivia = new StringSlice(slice.Text, contentStart, contentEnd - 1), Span = new SourceSpan(spanStart, spanEnd), Line = line, Column = column, DelimiterCount = delimiterCount, }; + + if (processor.TrackTrivia) + { + codeInline.ContentWithTrivia = new StringSlice(slice.Text, contentStart, contentEnd - 1); + } + + processor.Inline = codeInline; isMatching = true; } diff --git a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs index 9caaf15e..853bd07c 100644 --- a/src/Markdig/Parsers/Inlines/LinkInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/LinkInlineParser.cs @@ -79,18 +79,23 @@ namespace Markdig.Parsers.Inlines // Else we insert a LinkDelimiter slice.SkipChar(); - var labelWithTrivia = new StringSlice(slice.Text, labelWithTriviaSpan.Start, labelWithTriviaSpan.End); - processor.Inline = new LinkDelimiterInline(this) + var linkDelimiter = new LinkDelimiterInline(this) { Type = DelimiterType.Open, Label = label, - LabelWithTrivia = labelWithTrivia, LabelSpan = processor.GetSourcePositionFromLocalSpan(labelSpan), IsImage = isImage, Span = new SourceSpan(startPosition, processor.GetSourcePosition(slice.Start - 1)), Line = line, Column = column }; + + if (processor.TrackTrivia) + { + linkDelimiter.LabelWithTrivia = new StringSlice(slice.Text, labelWithTriviaSpan.Start, labelWithTriviaSpan.End); + } + + processor.Inline = linkDelimiter; return true; case ']': diff --git a/src/Markdig/Syntax/Inlines/CodeInline.cs b/src/Markdig/Syntax/Inlines/CodeInline.cs index 31e1a9a0..ef336dcf 100644 --- a/src/Markdig/Syntax/Inlines/CodeInline.cs +++ b/src/Markdig/Syntax/Inlines/CodeInline.cs @@ -14,6 +14,9 @@ namespace Markdig.Syntax.Inlines [DebuggerDisplay("`{Content}`")] public class CodeInline : LeafInline { + private TriviaProperties? _trivia; + private TriviaProperties Trivia => _trivia ??= new(); + public CodeInline(string content) { Content = content; @@ -37,16 +40,13 @@ namespace Markdig.Syntax.Inlines /// /// Gets or sets the content with trivia and whitespace. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// - public StringSlice ContentWithTrivia { get; set; } + public StringSlice ContentWithTrivia { get => _trivia?.ContentWithTrivia ?? StringSlice.Empty; set => Trivia.ContentWithTrivia = value; } - /// - /// True if the first and last character of the content enclosed in a backtick ` - /// is a space. - /// Trivia: only parsed when is enabled, otherwise - /// false. - /// - public bool FirstAndLastWasSpace { get; set; } + private sealed class TriviaProperties + { + public StringSlice ContentWithTrivia; + } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/Inlines/LinkDelimiterInline.cs b/src/Markdig/Syntax/Inlines/LinkDelimiterInline.cs index 77c4a314..6b666a18 100644 --- a/src/Markdig/Syntax/Inlines/LinkDelimiterInline.cs +++ b/src/Markdig/Syntax/Inlines/LinkDelimiterInline.cs @@ -13,6 +13,9 @@ namespace Markdig.Syntax.Inlines /// public class LinkDelimiterInline : DelimiterInline { + private TriviaProperties? _trivia; + private TriviaProperties Trivia => _trivia ??= new(); + public LinkDelimiterInline(InlineParser parser) : base(parser) { } @@ -35,13 +38,18 @@ namespace Markdig.Syntax.Inlines /// /// Gets or sets the with trivia. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// - public StringSlice LabelWithTrivia { get; set; } + public StringSlice LabelWithTrivia { get => _trivia?.LabelWithTrivia ?? StringSlice.Empty; set => Trivia.LabelWithTrivia = value; } public override string ToLiteral() { return IsImage ? "![" : "["; } + + private sealed class TriviaProperties + { + public StringSlice LabelWithTrivia; + } } } \ No newline at end of file From b7d02cadbb041ca3f789f7d0cfea07bd5c7cbe6f Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sun, 13 Mar 2022 01:54:44 +0100 Subject: [PATCH 9/9] Fix TrackTrivia /// comments --- src/Markdig/Roundtrip.md | 4 ++-- src/Markdig/Syntax/IBlock.cs | 4 ++-- src/Markdig/Syntax/IFencedBlock.cs | 17 ++++++++--------- src/Markdig/Syntax/Inlines/LinkInline.cs | 12 ++++++------ src/Markdig/Syntax/LinkReferenceDefinition.cs | 10 +++++----- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Markdig/Roundtrip.md b/src/Markdig/Roundtrip.md index c333f2d3..8541747d 100644 --- a/src/Markdig/Roundtrip.md +++ b/src/Markdig/Roundtrip.md @@ -111,14 +111,14 @@ All trivia in a document should be attached to a node. The `Block` class defines /// /// Gets or sets the trivia right before this block. /// Trivia: only parsed when is enabled, otherwise -/// . +/// . /// public StringSlice TriviaBefore { get; set; } /// /// Gets or sets trivia occurring after this block. /// Trivia: only parsed when is enabled, otherwise -/// . +/// . /// public StringSlice TriviaAfter { get; set; } ``` diff --git a/src/Markdig/Syntax/IBlock.cs b/src/Markdig/Syntax/IBlock.cs index e0e4f806..441157c9 100644 --- a/src/Markdig/Syntax/IBlock.cs +++ b/src/Markdig/Syntax/IBlock.cs @@ -61,13 +61,13 @@ namespace Markdig.Syntax /// /// Trivia occurring before this block /// - /// Trivia: only parsed when is enabled, otherwise . + /// Trivia: only parsed when is enabled, otherwise . StringSlice TriviaBefore { get; set; } /// /// Trivia occurring after this block /// - /// Trivia: only parsed when is enabled, otherwise . + /// Trivia: only parsed when is enabled, otherwise . StringSlice TriviaAfter { get; set; } } } \ No newline at end of file diff --git a/src/Markdig/Syntax/IFencedBlock.cs b/src/Markdig/Syntax/IFencedBlock.cs index 4f0c4152..7b072042 100644 --- a/src/Markdig/Syntax/IFencedBlock.cs +++ b/src/Markdig/Syntax/IFencedBlock.cs @@ -3,7 +3,6 @@ // See the license.txt file in the project root for more information. using Markdig.Helpers; -using Markdig.Parsers; namespace Markdig.Syntax { @@ -25,7 +24,7 @@ namespace Markdig.Syntax /// /// Gets or sets the trivia after the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// StringSlice TriviaAfterFencedChar { get; set; } @@ -38,14 +37,14 @@ namespace Markdig.Syntax /// /// Non-escaped exactly as in source markdown. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// StringSlice UnescapedInfo { get; set; } /// /// Gets or sets the trivia after the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// StringSlice TriviaAfterInfo { get; set; } @@ -58,28 +57,28 @@ namespace Markdig.Syntax /// /// Non-escaped exactly as in source markdown. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// StringSlice UnescapedArguments { get; set; } /// /// Gets or sets the trivia after the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// StringSlice TriviaAfterArguments { get; set; } /// /// Newline of the line with the opening fenced chars. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// NewLine InfoNewLine { get; set; } /// /// Trivia before the closing fenced chars /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// StringSlice TriviaBeforeClosingFence { get; set; } @@ -92,7 +91,7 @@ namespace Markdig.Syntax /// Newline after the last line, which is always the line containing the closing fence chars. /// "Inherited" from . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// NewLine NewLine { get; set; } } diff --git a/src/Markdig/Syntax/Inlines/LinkInline.cs b/src/Markdig/Syntax/Inlines/LinkInline.cs index fbf6152d..43d587a5 100644 --- a/src/Markdig/Syntax/Inlines/LinkInline.cs +++ b/src/Markdig/Syntax/Inlines/LinkInline.cs @@ -67,7 +67,7 @@ namespace Markdig.Syntax.Inlines /// /// Gets or sets the with trivia. /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice LabelWithTrivia { get => _trivia?.LabelWithTrivia ?? StringSlice.Empty; set => Trivia.LabelWithTrivia = value; } @@ -98,7 +98,7 @@ namespace Markdig.Syntax.Inlines /// /// Gets or sets the trivia before the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice TriviaBeforeUrl { get => _trivia?.TriviaBeforeUrl ?? StringSlice.Empty; set => Trivia.TriviaBeforeUrl = value; } @@ -123,14 +123,14 @@ namespace Markdig.Syntax.Inlines /// /// The but with trivia and unescaped characters /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice UnescapedUrl { get => _trivia?.UnescapedUrl ?? StringSlice.Empty; set => Trivia.UnescapedUrl = value; } /// /// Any trivia after the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice TriviaAfterUrl { get => _trivia?.TriviaAfterUrl ?? StringSlice.Empty; set => Trivia.TriviaAfterUrl = value; } @@ -160,14 +160,14 @@ namespace Markdig.Syntax.Inlines /// Gets or sets the exactly as parsed from the /// source document including unescaped characters /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice UnescapedTitle { get => _trivia?.UnescapedTitle ?? StringSlice.Empty; set => Trivia.UnescapedTitle = value; } /// /// Gets or sets the trivia after the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice TriviaAfterTitle { get => _trivia?.TriviaAfterTitle ?? StringSlice.Empty; set => Trivia.TriviaAfterTitle = value; } diff --git a/src/Markdig/Syntax/LinkReferenceDefinition.cs b/src/Markdig/Syntax/LinkReferenceDefinition.cs index 5efa59a1..321d07b5 100644 --- a/src/Markdig/Syntax/LinkReferenceDefinition.cs +++ b/src/Markdig/Syntax/LinkReferenceDefinition.cs @@ -63,14 +63,14 @@ namespace Markdig.Syntax /// /// Non-normalized Label (includes trivia) /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice LabelWithTrivia { get => _trivia?.LabelWithTrivia ?? StringSlice.Empty; set => Trivia.LabelWithTrivia = value; } /// /// Whitespace before the . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice TriviaBeforeUrl { get => _trivia?.TriviaBeforeUrl ?? StringSlice.Empty; set => Trivia.TriviaBeforeUrl = value; } @@ -87,7 +87,7 @@ namespace Markdig.Syntax /// /// Non-normalized . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice UnescapedUrl { get => _trivia?.UnescapedUrl ?? StringSlice.Empty; set => Trivia.UnescapedUrl = value; } @@ -101,7 +101,7 @@ namespace Markdig.Syntax /// /// gets or sets the whitespace before a . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice TriviaBeforeTitle { get => _trivia?.TriviaBeforeTitle ?? StringSlice.Empty; set => Trivia.TriviaBeforeTitle = value; } @@ -118,7 +118,7 @@ namespace Markdig.Syntax /// /// Non-normalized . /// Trivia: only parsed when is enabled, otherwise - /// . + /// . /// public StringSlice UnescapedTitle { get => _trivia?.UnescapedTitle ?? StringSlice.Empty; set => Trivia.UnescapedTitle = value; }