-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
571 additions
and
191 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
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,53 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace RecordParser.Benchmark | ||
{ | ||
internal static class Parse | ||
{ | ||
#if NETSTANDARD2_0 || NETFRAMEWORK | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static string ProcessSpan(ReadOnlySpan<char> span) => span.ToString(); | ||
#else | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static ReadOnlySpan<char> ProcessSpan(ReadOnlySpan<char> span) => span; | ||
#endif | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static byte Byte(ReadOnlySpan<char> utf8Text, IFormatProvider provider = null) => byte.Parse(ProcessSpan(utf8Text), NumberStyles.Integer, provider); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static sbyte SByte(ReadOnlySpan<char> utf8Text, IFormatProvider provider = null) => sbyte.Parse(ProcessSpan(utf8Text), NumberStyles.Integer, provider); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static double Double(ReadOnlySpan<char> utf8Text, IFormatProvider provider = null) => double.Parse(ProcessSpan(utf8Text), NumberStyles.AllowThousands | NumberStyles.Float, provider); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static float Single(ReadOnlySpan<char> utf8Text, IFormatProvider provider = null) => float.Parse(ProcessSpan(utf8Text), NumberStyles.AllowThousands | NumberStyles.Float, provider); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int Int32(ReadOnlySpan<char> utf8Text, IFormatProvider provider = null) => int.Parse(ProcessSpan(utf8Text), NumberStyles.Integer, provider); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static Guid Guid(ReadOnlySpan<char> utf8Text) => System.Guid.Parse(ProcessSpan(utf8Text)); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static DateTime DateTime(ReadOnlySpan<char> utf8Text, IFormatProvider provider = null) => System.DateTime.Parse(ProcessSpan(utf8Text), provider, DateTimeStyles.AllowWhiteSpaces); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool Boolean(ReadOnlySpan<char> utf8Text) => bool.Parse(ProcessSpan(utf8Text)); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static TEnum Enum<TEnum>(ReadOnlySpan<char> utf8Text) where TEnum : struct, Enum | ||
{ | ||
#if NETSTANDARD2_0 || NETFRAMEWORK | ||
return (TEnum)System.Enum.Parse(typeof(TEnum), utf8Text.ToString()); | ||
#elif NETSTANDARD2_1 | ||
return System.Enum.Parse<TEnum>(utf8Text.ToString()); | ||
#else | ||
return System.Enum.Parse<TEnum>(utf8Text); | ||
#endif | ||
} | ||
} | ||
} |
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,72 @@ | ||
#if NETSTANDARD2_0 || NETFRAMEWORK | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace RecordParser.Benchmark | ||
{ | ||
internal static class Shims | ||
{ | ||
public static int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> chars) | ||
{ | ||
unsafe | ||
{ | ||
fixed (byte* b = &MemoryMarshal.GetReference(bytes)) | ||
{ | ||
int charCount = encoding.GetCharCount(b, bytes.Length); | ||
if (charCount > chars.Length) return 0; | ||
|
||
fixed (char* c = &MemoryMarshal.GetReference(chars)) | ||
{ | ||
return encoding.GetChars(b, bytes.Length, c, chars.Length); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static string GetString(this Encoding encoding, scoped ReadOnlySpan<byte> bytes) | ||
{ | ||
if (bytes.IsEmpty) return string.Empty; | ||
|
||
unsafe | ||
{ | ||
fixed (byte* pB = &MemoryMarshal.GetReference(bytes)) | ||
{ | ||
return encoding.GetString(pB, bytes.Length); | ||
} | ||
} | ||
} | ||
|
||
public static Task WriteLineAsync(this TextWriter writer, ReadOnlyMemory<char> value, CancellationToken cancellationToken = default) | ||
{ | ||
if (MemoryMarshal.TryGetArray(value, out var arraySegment)) | ||
{ | ||
return arraySegment.Array is null ? Task.CompletedTask : writer.WriteLineAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count); | ||
} | ||
|
||
return Impl(writer, value); | ||
|
||
static async Task Impl(TextWriter writer, ReadOnlyMemory<char> value) | ||
{ | ||
var pool = ArrayPool<char>.Shared; | ||
var array = pool.Rent(value.Length); | ||
try | ||
{ | ||
value.CopyTo(array.AsMemory()); | ||
await writer.WriteLineAsync(array, 0, value.Length); | ||
} | ||
finally | ||
{ | ||
pool.Return(array); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
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
Oops, something went wrong.