diff --git a/X10D/src/Collections/ByteExtensions.cs b/X10D/src/Collections/ByteExtensions.cs index 2d82e1b..ecde8af 100644 --- a/X10D/src/Collections/ByteExtensions.cs +++ b/X10D/src/Collections/ByteExtensions.cs @@ -22,9 +22,9 @@ public static class ByteExtensions [Pure] public static bool[] Unpack(this byte value) { - Span buffer = stackalloc bool[Size]; + bool[] buffer = new bool[Size]; value.Unpack(buffer); - return buffer.ToArray(); + return buffer; } /// diff --git a/X10D/src/Collections/CollectionExtensions.cs b/X10D/src/Collections/CollectionExtensions.cs index 6bad398..ae33488 100644 --- a/X10D/src/Collections/CollectionExtensions.cs +++ b/X10D/src/Collections/CollectionExtensions.cs @@ -69,7 +69,7 @@ public static class CollectionExtensions continue; } - await item.DisposeAsync(); + await item.DisposeAsync().ConfigureAwait(false); } source.Clear(); diff --git a/X10D/src/Collections/DictionaryExtensions.cs b/X10D/src/Collections/DictionaryExtensions.cs index 29d241e..3d25009 100644 --- a/X10D/src/Collections/DictionaryExtensions.cs +++ b/X10D/src/Collections/DictionaryExtensions.cs @@ -58,7 +58,8 @@ public static class DictionaryExtensions if (exists) { value = updateValueFactory(key, value!); - } else + } + else { value = addValue; } @@ -67,11 +68,15 @@ public static class DictionaryExtensions #else if (dictionary.TryGetValue(key, out var old)) { - dictionary[key] = updateValueFactory(key, old); + var newValue = updateValueFactory(key, old); + dictionary[key] = newValue; + + return newValue; } else { dictionary.Add(key, addValue); + return addValue; } #endif } @@ -121,14 +126,16 @@ public static class DictionaryExtensions if (dictionary.TryGetValue(key, out var old)) { - dictionary[key] = updateValueFactory(key, old); + var newValue = updateValueFactory(key, old); + dictionary[key] = newValue; + + return newValue; } else { dictionary.Add(key, addValue); + return addValue; } - - return dictionary[key]; } /// @@ -199,14 +206,18 @@ public static class DictionaryExtensions #else if (dictionary.TryGetValue(key, out var old)) { - dictionary[key] = updateValueFactory(key, old); + var update = updateValueFactory(key, old); + dictionary[key] = update; + + return update; } else { - dictionary.Add(key, addValueFactory(key)); - } + var add = addValueFactory(key); + dictionary.Add(key, add); - return dictionary[key]; + return add; + } #endif } @@ -265,14 +276,18 @@ public static class DictionaryExtensions if (dictionary.TryGetValue(key, out var old)) { - dictionary[key] = updateValueFactory(key, old); + var update = updateValueFactory(key, old); + dictionary[key] = update; + + return update; } else { - dictionary.Add(key, addValueFactory(key)); - } + var add = addValueFactory(key); + dictionary.Add(key, add); - return dictionary[key]; + return add; + } } /// @@ -349,14 +364,18 @@ public static class DictionaryExtensions #else if (dictionary.TryGetValue(key, out var old)) { - dictionary[key] = updateValueFactory(key, old, factoryArgument); + var update = updateValueFactory(key, old, factoryArgument); + dictionary[key] = update; + + return update; } else { - dictionary.Add(key, addValueFactory(key, factoryArgument)); - } + var add = addValueFactory(key, factoryArgument); + dictionary.Add(key, add); - return dictionary[key]; + return add; + } #endif } @@ -421,14 +440,18 @@ public static class DictionaryExtensions if (dictionary.TryGetValue(key, out var old)) { - dictionary[key] = updateValueFactory(key, old, factoryArgument); + var update = updateValueFactory(key, old, factoryArgument); + dictionary[key] = update; + + return update; } else { - dictionary.Add(key, addValueFactory(key, factoryArgument)); - } + var add = addValueFactory(key, factoryArgument); + dictionary.Add(key, add); - return dictionary[key]; + return add; + } } /// diff --git a/X10D/src/Collections/EnumerableExtensions.cs b/X10D/src/Collections/EnumerableExtensions.cs index 4a75370..f05b783 100644 --- a/X10D/src/Collections/EnumerableExtensions.cs +++ b/X10D/src/Collections/EnumerableExtensions.cs @@ -124,7 +124,7 @@ public static class EnumerableExtensions continue; } - await item.DisposeAsync(); + await item.DisposeAsync().ConfigureAwait(false); } } diff --git a/X10D/src/Core/SpanExtensions.cs b/X10D/src/Core/SpanExtensions.cs index 4462d2a..af5dfdd 100644 --- a/X10D/src/Core/SpanExtensions.cs +++ b/X10D/src/Core/SpanExtensions.cs @@ -22,7 +22,7 @@ public static class SpanExtensions #else [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] #endif - public static bool Contains(this Span span, T value) where T : Enum + public static bool Contains(this Span span, T value) where T : struct, Enum { return Contains((ReadOnlySpan)span, value); } @@ -40,12 +40,15 @@ public static class SpanExtensions #else [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] #endif - public static bool Contains(this ReadOnlySpan span, T value) + public static bool Contains(this ReadOnlySpan span, T value) where T : struct, Enum { +#if NET6_0_OR_GREATER // Use MemoryMarshal.CreateSpan instead of using creating new Span instance from pointer will trim down a lot of instructions // on Release mode. // https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABABgAJiBGAOgCUBXAOwwEt8YaBJFmKCAA4BlPgDdWYGLgDcAWABQZSrUYt2nAMIR8A1gBs+IqOMkyFxAExVzFIQAtsUAQBlsweszYc588wGZyGCYGfHIAFSkMAFFg0JByVhZyAG8FcnTyAEE0cgAhHI0cgBE0BQBfBX9KC3INFLSMgG0AKVYMAHEgvgkACgwATwEYCAAzHojcaNiASmmAXQb0xoBZGAw7CAATLh09HtX1rZ2BPQB5ATYIJlwaTIBzO9hcXFZRGB49RMS78kJyA4221250u11uDyeLzeIPYrAAXthQfNFpQAtQkORmLhsCMYORgBAIHp/mtAVQADxhAB8PSEAmwTEpVPIuHpTByYXIomwegYMGm5AA7nY+HjOfEYiF6vIMrLyLARgkkkEQrhyABeeUwRUAVWuOM4mVwlJyiQwNIVJPw0H6y0cuAcehonQwdG1oqYkh6rIZsx8coyxAA7FabXaoA6eTQNLBETA6QyepaVfhcDkfUwaM4gnd1tNo1cMNhErgenrsbjbsawqaWBbtVyeXy/SiKjKMiiWm1OkxumA+oNhmMJlMQrMFu2lgCjrt9qSZycYVcbvdHlIoe8mJ8mN9fiTDkDFxdWMvwWvnq8YDD8PDESemMjJ6jlBisQb8YTidPNhYmbS2UyLJshyja8vyQoirA4TkBKsTSgG6TBuQvaCuQCaMmaNLlgaVYAAoQGafBJg2qzWlAtr2o6zprG6uKwJ6MDemyszpmyWY5nmBYsMW1xlvqlZGiaSrmsRircmBLZPm2ZRAA=== + // Also use reference instead of MemoryMarshal.Cast to remove boundary check (or something, it just result in something like that). + // TODO: Figure out some kind of way to directly pass the Span directly into Contains call, which make method smaller and more prone to inlining... unsafe { @@ -85,5 +88,16 @@ public static class SpanExtensions } #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type } +#else // NET6_0_OR_GREATER + foreach (var it in span) + { + if (EqualityComparer.Default.Equals(it, value)) + { + return true; + } + } + + return false; +#endif // NET6_0_OR_GREATER } } diff --git a/X10D/src/IO/DoubleExtensions.cs b/X10D/src/IO/DoubleExtensions.cs index a981d23..f71e4be 100644 --- a/X10D/src/IO/DoubleExtensions.cs +++ b/X10D/src/IO/DoubleExtensions.cs @@ -17,9 +17,9 @@ public static class DoubleExtensions [Pure] public static byte[] GetBytes(this double value) { - Span buffer = stackalloc byte[8]; + byte[] buffer = new byte[8]; value.TryWriteBytes(buffer); - return buffer.ToArray(); + return buffer; } /// @@ -31,9 +31,9 @@ public static class DoubleExtensions [Pure] public static byte[] GetBytes(this double value, Endianness endianness) { - Span buffer = stackalloc byte[8]; + byte[] buffer = new byte[8]; value.TryWriteBytes(buffer, endianness); - return buffer.ToArray(); + return buffer; } /// diff --git a/X10D/src/IO/Int16Extensions.cs b/X10D/src/IO/Int16Extensions.cs index 61be94a..60b0b87 100644 --- a/X10D/src/IO/Int16Extensions.cs +++ b/X10D/src/IO/Int16Extensions.cs @@ -16,9 +16,9 @@ public static class Int16Extensions [Pure] public static byte[] GetBytes(this short value) { - Span buffer = stackalloc byte[2]; + byte[] buffer = new byte[2]; value.TryWriteBytes(buffer); - return buffer.ToArray(); + return buffer; } /// @@ -30,9 +30,9 @@ public static class Int16Extensions [Pure] public static byte[] GetBytes(this short value, Endianness endianness) { - Span buffer = stackalloc byte[2]; + byte[] buffer = new byte[2]; value.TryWriteBytes(buffer, endianness); - return buffer.ToArray(); + return buffer; } /// diff --git a/X10D/src/IO/Int32Extensions.cs b/X10D/src/IO/Int32Extensions.cs index 44fa419..a349dec 100644 --- a/X10D/src/IO/Int32Extensions.cs +++ b/X10D/src/IO/Int32Extensions.cs @@ -30,7 +30,7 @@ public static class Int32Extensions [Pure] public static byte[] GetBytes(this int value, Endianness endianness) { - Span buffer = stackalloc byte[4]; + byte[] buffer = new byte[4]; value.TryWriteBytes(buffer, endianness); return buffer.ToArray(); } diff --git a/X10D/src/Math/ComparableExtensions.cs b/X10D/src/Math/ComparableExtensions.cs index 06cd86a..ac6c335 100644 --- a/X10D/src/Math/ComparableExtensions.cs +++ b/X10D/src/Math/ComparableExtensions.cs @@ -133,7 +133,7 @@ public static class ComparableExtensions if (lower.GreaterThan(upper)) { throw new ArgumentException( - string.Format(ExceptionMessages.LowerCannotBeGreaterThanUpper, lower, upper), + string.Format(null, ExceptionMessages.LowerCannotBeGreaterThanUpper, lower, upper), nameof(lower)); }