-
-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #621 from MihaZupan/perf-april
April perf improvements
- Loading branch information
Showing
31 changed files
with
674 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Markdig.Helpers; | ||
using NUnit.Framework; | ||
|
||
namespace Markdig.Tests | ||
{ | ||
public class TestLazySubstring | ||
{ | ||
[Theory] | ||
[TestCase("")] | ||
[TestCase("a")] | ||
[TestCase("foo")] | ||
public void LazySubstring_ReturnsCorrectSubstring(string text) | ||
{ | ||
var substring = new LazySubstring(text); | ||
Assert.AreEqual(0, substring.Offset); | ||
Assert.AreEqual(text.Length, substring.Length); | ||
|
||
Assert.AreEqual(text, substring.AsSpan().ToString()); | ||
Assert.AreEqual(text, substring.AsSpan().ToString()); | ||
Assert.AreEqual(0, substring.Offset); | ||
Assert.AreEqual(text.Length, substring.Length); | ||
|
||
Assert.AreSame(substring.ToString(), substring.ToString()); | ||
Assert.AreEqual(text, substring.ToString()); | ||
Assert.AreEqual(0, substring.Offset); | ||
Assert.AreEqual(text.Length, substring.Length); | ||
|
||
Assert.AreEqual(text, substring.AsSpan().ToString()); | ||
Assert.AreEqual(text, substring.AsSpan().ToString()); | ||
Assert.AreEqual(0, substring.Offset); | ||
Assert.AreEqual(text.Length, substring.Length); | ||
} | ||
|
||
[Theory] | ||
[TestCase("", 0, 0)] | ||
[TestCase("a", 0, 0)] | ||
[TestCase("a", 1, 0)] | ||
[TestCase("a", 0, 1)] | ||
[TestCase("foo", 1, 0)] | ||
[TestCase("foo", 1, 1)] | ||
[TestCase("foo", 1, 2)] | ||
[TestCase("foo", 0, 3)] | ||
public void LazySubstring_ReturnsCorrectSubstring(string text, int start, int length) | ||
{ | ||
var substring = new LazySubstring(text, start, length); | ||
Assert.AreEqual(start, substring.Offset); | ||
Assert.AreEqual(length, substring.Length); | ||
|
||
string expectedSubstring = text.Substring(start, length); | ||
|
||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString()); | ||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString()); | ||
Assert.AreEqual(start, substring.Offset); | ||
Assert.AreEqual(length, substring.Length); | ||
|
||
Assert.AreSame(substring.ToString(), substring.ToString()); | ||
Assert.AreEqual(expectedSubstring, substring.ToString()); | ||
Assert.AreEqual(0, substring.Offset); | ||
Assert.AreEqual(length, substring.Length); | ||
|
||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString()); | ||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString()); | ||
Assert.AreEqual(0, substring.Offset); | ||
Assert.AreEqual(length, substring.Length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 Markdig.Syntax; | ||
using System; | ||
|
||
namespace Markdig.Helpers | ||
{ | ||
// Used to avoid the overhead of type covariance checks | ||
internal readonly struct BlockWrapper : IEquatable<BlockWrapper> | ||
{ | ||
public readonly Block Block; | ||
|
||
public BlockWrapper(Block block) | ||
{ | ||
Block = block; | ||
} | ||
|
||
public static implicit operator Block(BlockWrapper wrapper) => wrapper.Block; | ||
|
||
public static implicit operator BlockWrapper(Block block) => new BlockWrapper(block); | ||
|
||
public bool Equals(BlockWrapper other) => ReferenceEquals(Block, other.Block); | ||
|
||
public override bool Equals(object obj) => Block.Equals(obj); | ||
|
||
public override int GetHashCode() => Block.GetHashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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.Diagnostics; | ||
|
||
namespace Markdig.Helpers | ||
{ | ||
internal struct LazySubstring | ||
{ | ||
private string _text; | ||
public int Offset; | ||
public int Length; | ||
|
||
public LazySubstring(string text) | ||
{ | ||
_text = text; | ||
Offset = 0; | ||
Length = text.Length; | ||
} | ||
|
||
public LazySubstring(string text, int offset, int length) | ||
{ | ||
Debug.Assert((ulong)offset + (ulong)length <= (ulong)text.Length, $"{offset}-{length} in {text}"); | ||
_text = text; | ||
Offset = offset; | ||
Length = length; | ||
} | ||
|
||
public ReadOnlySpan<char> AsSpan() => _text.AsSpan(Offset, Length); | ||
|
||
public override string ToString() | ||
{ | ||
if (Offset != 0 || Length != _text.Length) | ||
{ | ||
_text = _text.Substring(Offset, Length); | ||
Offset = 0; | ||
} | ||
|
||
return _text; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.