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

Enable ImplictUsings and use file-scoped namespaces #693

Merged
merged 8 commits into from
Jan 24, 2023
  •  
  •  
  •  
46 changes: 22 additions & 24 deletions src/Markdig.Benchmarks/CommonMarkLib.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// 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.Runtime.InteropServices;
using System.Text;

namespace Testamina.Markdig.Benchmarks
namespace Testamina.Markdig.Benchmarks;

public static class CommonMarkLib
{
public static class CommonMarkLib
public static string ToHtml(string text)
{
public static string ToHtml(string text)
unsafe
{
unsafe
{
var textAsArray = Encoding.UTF8.GetBytes(text);
var textAsArray = Encoding.UTF8.GetBytes(text);

fixed (void* ptext = textAsArray)
fixed (void* ptext = textAsArray)
{
var ptr = (byte*)cmark_markdown_to_html(new IntPtr(ptext), text.Length);
int length = 0;
while (ptr[length] != 0)
{
var ptr = (byte*)cmark_markdown_to_html(new IntPtr(ptext), text.Length);
int length = 0;
while (ptr[length] != 0)
{
length++;
}
var buffer = new byte[length];
Marshal.Copy(new IntPtr(ptr), buffer, 0, length);
var result = Encoding.UTF8.GetString(buffer);
Marshal.FreeHGlobal(new IntPtr(ptr));
return result;
length++;
}
var buffer = new byte[length];
Marshal.Copy(new IntPtr(ptr), buffer, 0, length);
var result = Encoding.UTF8.GetString(buffer);
Marshal.FreeHGlobal(new IntPtr(ptr));
return result;
}
}

// char *cmark_markdown_to_html(const char *text, size_t len, int options);
[DllImport("cmark", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr cmark_markdown_to_html(IntPtr charBuffer, int len, int options = 0);
}

// char *cmark_markdown_to_html(const char *text, size_t len, int options);
[DllImport("cmark", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr cmark_markdown_to_html(IntPtr charBuffer, int len, int options = 0);
}
1 change: 1 addition & 0 deletions src/Markdig.Benchmarks/Markdig.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<None Remove="spec.md" />
Expand Down
113 changes: 56 additions & 57 deletions src/Markdig.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,75 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

using Markdig;


namespace Testamina.Markdig.Benchmarks
namespace Testamina.Markdig.Benchmarks;

//[BenchmarkTask(platform: BenchmarkPlatform.X64, jitVersion: BenchmarkJitVersion.RyuJit, processCount: 1, warmupIterationCount: 2)]
public class Program
{
//[BenchmarkTask(platform: BenchmarkPlatform.X64, jitVersion: BenchmarkJitVersion.RyuJit, processCount: 1, warmupIterationCount: 2)]
public class Program
{
private string text;
private string text;

public Program()
{
//text = File.ReadAllText("progit.md");
text = File.ReadAllText("spec.md");
}
public Program()
{
//text = File.ReadAllText("progit.md");
text = File.ReadAllText("spec.md");
}

//[Benchmark(Description = "TestMarkdig", OperationsPerInvoke = 4096)]
[Benchmark(Description = "markdig")]
public void TestMarkdig()
{
//var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
Markdown.ToHtml(text);
//File.WriteAllText("spec.html", writer.ToString());
}
//[Benchmark(Description = "TestMarkdig", OperationsPerInvoke = 4096)]
[Benchmark(Description = "markdig")]
public void TestMarkdig()
{
//var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
Markdown.ToHtml(text);
//File.WriteAllText("spec.html", writer.ToString());
}

[Benchmark(Description = "cmark")]
public void TestCommonMarkCpp()
{
//var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
CommonMarkLib.ToHtml(text);
//File.WriteAllText("spec.html", writer.ToString());
}
[Benchmark(Description = "cmark")]
public void TestCommonMarkCpp()
{
//var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
CommonMarkLib.ToHtml(text);
//File.WriteAllText("spec.html", writer.ToString());
}

[Benchmark(Description = "CommonMark.NET")]
public void TestCommonMarkNet()
{
////var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
// var reader = new StringReader(text);
//CommonMark.CommonMarkConverter.Parse(reader);
//CommonMark.CommonMarkConverter.Parse(reader);
//reader.Dispose();
//var writer = new StringWriter();
CommonMark.CommonMarkConverter.Convert(text);
//writer.Flush();
//writer.ToString();
}
[Benchmark(Description = "CommonMark.NET")]
public void TestCommonMarkNet()
{
////var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
// var reader = new StringReader(text);
//CommonMark.CommonMarkConverter.Parse(reader);
//CommonMark.CommonMarkConverter.Parse(reader);
//reader.Dispose();
//var writer = new StringWriter();
CommonMark.CommonMarkConverter.Convert(text);
//writer.Flush();
//writer.ToString();
}

[Benchmark(Description = "MarkdownSharp")]
public void TestMarkdownSharp()
{
new MarkdownSharp.Markdown().Transform(text);
}
[Benchmark(Description = "MarkdownSharp")]
public void TestMarkdownSharp()
{
new MarkdownSharp.Markdown().Transform(text);
}

static void Main(string[] args)
{
var config = ManualConfig.Create(DefaultConfig.Instance);
//var gcDiagnoser = new MemoryDiagnoser();
//config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, IterationTime = 1024, TargetCount = 10 });
//config.Add(new Job { Mode = Mode.Throughput, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
//config.Add(gcDiagnoser);
static void Main(string[] args)
{
var config = ManualConfig.Create(DefaultConfig.Instance);
//var gcDiagnoser = new MemoryDiagnoser();
//config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, IterationTime = 1024, TargetCount = 10 });
//config.Add(new Job { Mode = Mode.Throughput, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
//config.Add(gcDiagnoser);

//var config = DefaultConfig.Instance;
BenchmarkRunner.Run<Program>(config);
//BenchmarkRunner.Run<TestDictionary>(config);
//BenchmarkRunner.Run<TestMatchPerf>();
//BenchmarkRunner.Run<TestStringPerf>();
}
//var config = DefaultConfig.Instance;
BenchmarkRunner.Run<Program>(config);
//BenchmarkRunner.Run<TestDictionary>(config);
//BenchmarkRunner.Run<TestMatchPerf>();
//BenchmarkRunner.Run<TestStringPerf>();
}
}
110 changes: 54 additions & 56 deletions src/Markdig.Benchmarks/TestMatchPerf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,79 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Testamina.Markdig.Benchmarks
namespace Testamina.Markdig.Benchmarks;

public class TextRegexHelper
{
public class TextRegexHelper
private readonly Dictionary<string, string> replacers;
private readonly Regex regex;

public TextRegexHelper(Dictionary<string, string> replacers)
{
private readonly Dictionary<string, string> replacers;
private readonly Regex regex;
this.replacers = replacers;
var builder = new StringBuilder();

public TextRegexHelper(Dictionary<string, string> replacers)
// (?<1>:value:?)|(?<1>:noo:?)
foreach (var replace in replacers)
{
this.replacers = replacers;
var builder = new StringBuilder();

// (?<1>:value:?)|(?<1>:noo:?)
foreach (var replace in replacers)
var matchStr = Regex.Escape(replace.Key);
if (builder.Length > 0)
{
var matchStr = Regex.Escape(replace.Key);
if (builder.Length > 0)
{
builder.Append('|');
}
builder.Append("(?<1>").Append(matchStr).Append("?)");
builder.Append('|');
}

regex = new Regex(builder.ToString());
builder.Append("(?<1>").Append(matchStr).Append("?)");
}

public bool TryMatch(string text, int offset, out string matchText, out string replaceText)
{
replaceText = null;
matchText = null;
var result = regex.Match(text, offset);
if (!result.Success)
{
return false;
}
regex = new Regex(builder.ToString());
}

matchText = result.Groups[1].Value;
replaceText = replacers[matchText];
return true;
public bool TryMatch(string text, int offset, out string matchText, out string replaceText)
{
replaceText = null;
matchText = null;
var result = regex.Match(text, offset);
if (!result.Success)
{
return false;
}

matchText = result.Groups[1].Value;
replaceText = replacers[matchText];
return true;
}
}

/*
public class TestMatchPerf
{
private readonly TextMatchHelper matcher;
/*
public class TestMatchPerf
{
private readonly TextMatchHelper matcher;

public TestMatchPerf()
public TestMatchPerf()
{
var replacers = new Dictionary<string, string>();
for (int i = 0; i < 1000; i++)
{
var replacers = new Dictionary<string, string>();
for (int i = 0; i < 1000; i++)
{
replacers.Add($":z{i}:", i.ToString());
}
replacers.Add(":abc:", "yes");

matcher = new TextMatchHelper(new HashSet<string>(replacers.Keys));
replacers.Add($":z{i}:", i.ToString());
}
replacers.Add(":abc:", "yes");

[Benchmark]
public void TestMatch()
{
matcher = new TextMatchHelper(new HashSet<string>(replacers.Keys));
}

for (int i = 0; i < 1000; i++)
{
string matchText;
//var text = ":z150: this is a long string";
var text = ":z1:";
matcher.TryMatch(text, 0, text.Length, out matchText);
}
[Benchmark]
public void TestMatch()
{

for (int i = 0; i < 1000; i++)
{
string matchText;
//var text = ":z150: this is a long string";
var text = ":z1:";
matcher.TryMatch(text, 0, text.Length, out matchText);
}
}
*/
}
}
*/