Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BitVector128 in CharacterMap<T> #396

Merged
merged 2 commits into from
Feb 6, 2020

Conversation

KrisVandermotten
Copy link
Contributor

CharacterMap<T> uses a bool[] isOpeningCharacter for quick lookup.

This PR replaces that by a custom BitVector128 that stores 128 bits is two ulong fields using a fixed size buffer.

This results in a measurable speedup:

The benchmark:

public class Program
{
    private string text = File.ReadAllText("spec.md");

    [Benchmark]
    public void TestMarkdig()
    {
        Markdown.Parse(text);
    }

    private static void Main()
    {
        BenchmarkRunner.Run<Program>();
    }
}

The results on .NET Framework:

BenchmarkDotNet=v0.10.6, OS=Windows 10.0.18363
Processor=Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), ProcessorCount=8
Frequency=10000000 Hz, Resolution=100.0000 ns, Timer=UNKNOWN
  [Host]     : Clr 4.0.30319.42000, 64bit RyuJIT-v4.8.4075.0
  DefaultJob : Clr 4.0.30319.42000, 64bit RyuJIT-v4.8.4075.0

Before:

Method Mean Error StdDev
TestMarkdig 3.363 ms 0.0209 ms 0.0196 ms

After:

Method Mean Error StdDev
TestMarkdig 3.275 ms 0.0220 ms 0.0206 ms

The results on .NET Core:

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.1.101
  [Host]     : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
  DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT

Before:

Method Mean Error StdDev
TestMarkdig 3.128 ms 0.0207 ms 0.0193 ms

After:

Method Mean Error StdDev
TestMarkdig 3.034 ms 0.0173 ms 0.0145 ms

Conclusion: parsing time is reduced by about 3 %.

@KrisVandermotten
Copy link
Contributor Author

KrisVandermotten commented Feb 5, 2020

I changed the implementation to use four uint values instead of two ulong values.

The performance is identical on X64, but it can only be better on X86 or other 32 bit platforms.

@xoofx xoofx merged commit 51f9da1 into xoofx:master Feb 6, 2020
@xoofx
Copy link
Owner

xoofx commented Feb 6, 2020

Nice trick, thanks!

@KrisVandermotten KrisVandermotten deleted the CharacterMapBitVector128 branch February 6, 2020 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants