Compare commits

...

4 Commits

Author SHA1 Message Date
Oliver Booth cedc8acaec
Merge bd823ba818 into 5ff7b68b37 2023-08-27 00:26:48 +00:00
Oliver Booth bd823ba818
perf: stackalloc single char span, instead of using ToString, for .NET 7
This removes the allocations caused by ToString(). Unfortunately, the ReadOnlySpan<char> overload is only available from .NET 7, and so this is a conditional performance gain. Shame.
2023-08-27 01:26:37 +01:00
Oliver Booth cfe70a7923
refactor: discard GetBits return 2023-08-26 18:50:49 +01:00
Oliver Booth 799e635577
style: mark WriteBits conditional 2023-08-26 18:50:37 +01:00
3 changed files with 11 additions and 2 deletions

View File

@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
BigEndian/LittleEndian methods.
- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of
TypeInitializationException.
- X10D: `char.IsEmoji` no longer allocates for .NET 7.
### Removed

View File

@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
@ -93,7 +94,7 @@ public static class DecimalExtensions
private static void GetBits(decimal value, Span<int> destination)
{
#if NET5_0_OR_GREATER
decimal.GetBits(value, destination);
_ = decimal.GetBits(value, destination);
#else
Span<byte> buffer = stackalloc byte[16];
MemoryMarshal.Write(buffer, ref value);
@ -101,6 +102,7 @@ public static class DecimalExtensions
#endif
}
[Conditional("NET5_0_OR_GREATER")]
private static void WriteBits(Span<int> destination, Span<byte> buffer)
{
var flags = MemoryMarshal.Read<int>(buffer[..4]);

View File

@ -18,7 +18,13 @@ public static class CharExtensions
[MethodImpl(CompilerResources.MethodImplOptions)]
public static bool IsEmoji(this char value)
{
return value.ToString().IsEmoji();
#if NET7_0_OR_GREATER
Span<char> chars = stackalloc char[1];
chars[0] = value;
return EmojiRegex.Value.IsMatch(chars);
#else
return EmojiRegex.Value.IsMatch(value.ToString());
#endif
}
/// <summary>