Skip to content

Commit

Permalink
feat: 性能优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
CYJB committed Jan 15, 2024
1 parent c97817d commit 15b52f6
Show file tree
Hide file tree
Showing 33 changed files with 10,690 additions and 7,684 deletions.
6 changes: 3 additions & 3 deletions Cyjb.Markdown/Cyjb.Markdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cyjb" Version="1.0.15">
<PackageReference Include="Cyjb" Version="1.0.17">
<GeneratePathProperty>True</GeneratePathProperty>
</PackageReference>
<PackageReference Include="Cyjb.Compilers.Design" Version="1.0.15">
<PackageReference Include="Cyjb.Compilers.Design" Version="1.0.16">
<GeneratePathProperty>True</GeneratePathProperty>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Cyjb.Compilers.Runtime" Version="1.0.15" />
<PackageReference Include="Cyjb.Compilers.Runtime" Version="1.0.16" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 18 additions & 12 deletions Cyjb.Markdown/ParseBlock/AutoIdentifierWalker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Globalization;
using System.Text;
using Cyjb.Collections;
using Cyjb.Markdown.Syntax;
using Cyjb.Markdown.Utils;

Expand All @@ -8,7 +8,7 @@ namespace Cyjb.Markdown.ParseBlock;
/// <summary>
/// 自动生成标识符的遍历器。
/// </summary>
internal sealed class AutoIdentifierWalker : SyntaxWalker
internal sealed class AutoIdentifierWalker : SyntaxWalker, IDisposable
{
/// <summary>
/// 唯一标识符。
Expand All @@ -18,10 +18,6 @@ internal sealed class AutoIdentifierWalker : SyntaxWalker
/// Alt 文本的渲染器。
/// </summary>
private readonly AltTextRenderer altTextRenderer = new();
/// <summary>
/// 标识符的构造器。
/// </summary>
private readonly StringBuilder identifierBuilder = new(128);

/// <summary>
/// 初始化 <see cref="AutoIdentifierWalker"/> 类的新实例。
Expand Down Expand Up @@ -49,9 +45,9 @@ public override void VisitHeading(Heading node)
/// </summary>
/// <param name="text">原始文本。</param>
/// <returns>从文本生成的标识符。</returns>
private string GetIdentifier(StringBuilder text)
private string GetIdentifier(ReadOnlySpan<char> text)
{
identifierBuilder.Clear();
using ValueList<char> identifierBuilder = new(stackalloc char[ValueList.StackallocCharSizeLimit]);
for (int i = 0; i < text.Length; i++)
{
char ch = text[i];
Expand Down Expand Up @@ -83,17 +79,18 @@ private string GetIdentifier(StringBuilder text)
continue;
}
}
identifierBuilder.Append(ch);
identifierBuilder.Add(ch);
}
for (; identifierBuilder.Length > 0 && IsPunctuation(identifierBuilder[^1]); identifierBuilder.Length--) ;
int end = identifierBuilder.Length - 1;
for (; end >= 0 && IsPunctuation(identifierBuilder[end]); end--) ;
// 7. 如果结果是空字符串,那么使用 `section` 作为标识符。
if (identifierBuilder.Length == 0)
if (end < 0)
{
return "section";
}
else
{
return identifierBuilder.ToString();
return identifierBuilder.AsSpan(0, end + 1).ToString();
}
}

Expand Down Expand Up @@ -130,4 +127,13 @@ private static bool IsPunctuation(char ch)
{
return ch is '_' or '-' or '.';
}

/// <summary>
/// 释放非托管资源。
/// </summary>
public void Dispose()
{
altTextRenderer.Dispose();
GC.SuppressFinalize(this);
}
}
16 changes: 8 additions & 8 deletions Cyjb.Markdown/ParseBlock/BlockLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ private void ATXHeadingWithAttributesAction()
{
if (Options.UseHeaderAttributes)
{
ReadOnlySpan<char> text = Text;
StringView text = Text;
HtmlAttributeList attrs = new();
if (MarkdownUtil.TryParseAttributes(ref text, attrs))
{
Text = text.ToString();
Text = text;
Accept(attrs);
return;
}
Expand All @@ -65,11 +65,11 @@ private void CodeFenceStartWithAttributesAction()
{
if (Options.UseCodeAttributes)
{
ReadOnlySpan<char> text = Text;
StringView text = Text;
HtmlAttributeList attrs = new();
if (MarkdownUtil.TryParseAttributes(ref text, attrs))
{
Text = text.ToString();
Text = text;
Accept(attrs);
return;
}
Expand Down Expand Up @@ -337,11 +337,11 @@ private void MathFenceStartWithAttributesAction()
{
if (Options.UseMath && Options.UseMathAttributes)
{
ReadOnlySpan<char> text = Text;
StringView text = Text;
HtmlAttributeList attrs = new();
if (MarkdownUtil.TryParseAttributes(ref text, attrs))
{
Text = text.ToString();
Text = text;
Accept(attrs);
return;
}
Expand Down Expand Up @@ -390,11 +390,11 @@ private void CustomContainerFenceStartWithAttributesAction()
{
if (Options.UseCustomContainers && Options.UseCustomContainerAttributes)
{
ReadOnlySpan<char> text = Text;
StringView text = Text;
HtmlAttributeList attrs = new();
if (MarkdownUtil.TryParseAttributes(ref text, attrs))
{
Text = text.ToString();
Text = text;
Accept(attrs);
return;
}
Expand Down
1 change: 1 addition & 0 deletions Cyjb.Markdown/ParseBlock/BlockParser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using Cyjb.Collections;
using Cyjb.Markdown.ParseInline;
using Cyjb.Markdown.Syntax;
Expand Down
18 changes: 9 additions & 9 deletions Cyjb.Markdown/ParseBlock/IndentInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using Cyjb.Collections;
using Cyjb.Text;

namespace Cyjb.Markdown.ParseBlock;
Expand All @@ -23,7 +23,7 @@ internal class IndentInfo
/// <summary>
/// 缩进文本。
/// </summary>
private readonly string text;
private readonly StringView text;
/// <summary>
/// 原始起始位置。
/// </summary>
Expand All @@ -47,7 +47,7 @@ public IndentInfo(int start, LineLocator locator)
this.start = end = start;
originalStart = start;
this.locator = locator;
text = string.Empty;
text = StringView.Empty;
startColumn = endColumn = 0;
}

Expand All @@ -57,7 +57,7 @@ public IndentInfo(int start, LineLocator locator)
/// <param name="span">缩进文本范围。</param>
/// <param name="locator">行定位器。</param>
/// <param name="text">缩进文本。</param>
public IndentInfo(TextSpan span, LineLocator locator, string text)
public IndentInfo(TextSpan span, LineLocator locator, StringView text)
{
start = span.Start;
end = span.End;
Expand Down Expand Up @@ -131,12 +131,12 @@ public void Skip()
/// 返回剩余的缩进文本。
/// </summary>
/// <returns>缩进文本。</returns>
public string GetText()
public StringView GetText()
{
if (startColumn == endColumn)
{
// 所有缩进均已消费。
return string.Empty;
return StringView.Empty;
}
int column = locator.GetPosition(start).Column;
if (column == startColumn)
Expand All @@ -147,13 +147,13 @@ public string GetText()
{
// 当前是部分 Tab,需要使用空格补齐 column(start) 到 startColumn 的位置。
column = locator.GetPosition(start + 1).Column;
StringBuilder result = new();
result.Append(' ', column - startColumn);
using ValueList<char> result = new(stackalloc char[ValueList.StackallocCharSizeLimit]);
result.Add(' ', column - startColumn);
int idx = start + 1 - originalStart;
// 存在 Tab 时,可能会出现列数超出字符数的场景。
if (idx < text.Length)
{
result.Append(text, idx, text.Length - idx);
result.Add(text.AsSpan(idx));
}
return result.ToString();
}
Expand Down
41 changes: 32 additions & 9 deletions Cyjb.Markdown/ParseBlock/LineInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using Cyjb.Collections;
using Cyjb.Text;

Expand Down Expand Up @@ -64,6 +63,16 @@ internal LineInfo(BlockParser parser, LineLocator locator)
this.locator = locator;
}

/// <summary>
/// 返回指定索引的行列位置。
/// </summary>
/// <param name="index">要检查行列位置的索引。</param>
/// <returns>指定索引的行列位置。</returns>
public LinePosition GetPosition(int index)
{
return locator.GetPosition(index);
}

/// <summary>
/// 获取当前缩进宽度。
/// </summary>
Expand Down Expand Up @@ -106,15 +115,22 @@ public MappedText Text
{
if (text == null)
{
StringBuilder builder = new();
List<StringView> texts = new();
int length = 0;
int lastLength = 0;
int lastMappedIndex = 0;
TextSpanBuilder spanBuilder = new();
List<Tuple<int, int>> map = new();
List<Tuple<int, int>> maps = new();
// 添加缩进。
if (indent != null && indent.Width > 0)
{
map.Add(new Tuple<int, int>(0, indent.Start));
spanBuilder.Add(indent.Start);
builder.Append(indent.GetText());
lastMappedIndex = indent.Start;
maps.Add(new Tuple<int, int>(0, lastMappedIndex));
spanBuilder.Add(lastMappedIndex);
StringView text = indent.GetText();
lastLength = text.Length;
length += lastLength;
texts.Add(text);
}
// 添加剩余词法单元。
bool isFirst = true;
Expand All @@ -123,13 +139,20 @@ public MappedText Text
// 首个缩进可能会存在 Tab 部分替换为空格的情况,因此之后的词法单元也需要添加索引。
if (isFirst)
{
map.Add(new Tuple<int, int>(builder.Length, token.Span.Start));
int offset = token.Span.Start - lastMappedIndex;
if (lastLength != offset)
{
maps.Add(new Tuple<int, int>(lastLength, offset));
}
isFirst = false;
}
builder.Append(token.Text);
lastMappedIndex = token.Span.Start;
lastLength = token.Text.Length;
length += lastLength;
texts.Add(token.Text);
spanBuilder.Add(token.Span);
}
text = new MappedText(builder.ToString(), spanBuilder.GetSpan(), map.ToArray());
text = new MappedText(texts, length, spanBuilder.GetSpan(), maps);
}
return text;
}
Expand Down
Loading

0 comments on commit 15b52f6

Please sign in to comment.