Skip to content

Commit 7a9c192

Browse files
committed
Speed up FencedCodeBlock rendering
1 parent 8cfa0cf commit 7a9c192

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Markdig/Polyfills/FrozenCollections.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,17 @@ public static FrozenDictionary<TKey, TValue> ToFrozenDictionary<TKey, TValue>(th
2222
}
2323
}
2424

25+
internal sealed class FrozenSet<T> : HashSet<T>
26+
{
27+
public FrozenSet(HashSet<T> set, IEqualityComparer<T> comparer) : base(set, comparer) { }
28+
}
29+
30+
internal static class FrozenSetExtensions
31+
{
32+
public static FrozenSet<T> ToFrozenSet<T>(this HashSet<T> set, IEqualityComparer<T> comparer)
33+
{
34+
return new FrozenSet<T>(set, comparer);
35+
}
36+
}
37+
2538
#endif

src/Markdig/Renderers/Html/CodeBlockRenderer.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,27 @@ public CodeBlockRenderer() { }
3131
/// </summary>
3232
public Dictionary<string, string> BlockMapping { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
3333

34+
private FrozenSet<string>? _specialBlockMapping;
35+
36+
private FrozenSet<string> SpecialBlockMapping
37+
{
38+
get
39+
{
40+
return _specialBlockMapping ?? CreateNew();
41+
42+
FrozenSet<string> CreateNew()
43+
{
44+
HashSet<string> set = [.. BlocksAsDiv, .. BlockMapping.Keys];
45+
return _specialBlockMapping = set.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
46+
}
47+
}
48+
}
49+
3450
protected override void Write(HtmlRenderer renderer, CodeBlock obj)
3551
{
3652
renderer.EnsureLine();
3753

38-
if ((obj as FencedCodeBlock)?.Info is string info && (BlocksAsDiv.Contains(info) || BlockMapping.ContainsKey(info)))
54+
if ((obj as FencedCodeBlock)?.Info is string info && SpecialBlockMapping.Contains(info))
3955
{
4056
var infoPrefix = (obj.Parser as FencedCodeBlockParser)?.InfoPrefix ??
4157
FencedCodeBlockParser.DefaultInfoPrefix;

0 commit comments

Comments
 (0)