From 52719161d5ad1b438e420ec624c4e4da15008493 Mon Sep 17 00:00:00 2001 From: Andrei Sergeev Date: Tue, 23 Jan 2024 23:18:22 +0400 Subject: [PATCH 1/3] JsonConverterFactory.CreateConverter: Use collection expressions --- .../FlatArrayJsonConverterFactory/Factory.CreateConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flatcollections-array/FlatArray/FlatArrayJsonConverterFactory/Factory.CreateConverter.cs b/src/flatcollections-array/FlatArray/FlatArrayJsonConverterFactory/Factory.CreateConverter.cs index 1e326eea..a3a58b61 100644 --- a/src/flatcollections-array/FlatArray/FlatArrayJsonConverterFactory/Factory.CreateConverter.cs +++ b/src/flatcollections-array/FlatArray/FlatArrayJsonConverterFactory/Factory.CreateConverter.cs @@ -20,7 +20,7 @@ partial class FlatArrayJsonConverterFactory type: typeof(FlatArray<>.JsonConverter).MakeGenericType(itemType), bindingAttr: BindingFlags.Instance | BindingFlags.Public, binder: null, - args: new object?[] { options }, + args: [options], culture: null); Debug.Assert(converter is not null); From 1df4028aa3b390beffa1eba0369fcd57ddf091f7 Mon Sep 17 00:00:00 2001 From: Andrei Sergeev Date: Wed, 24 Jan 2024 09:13:59 +0400 Subject: [PATCH 2/3] Minor refactor when initializing spans --- .../FlatArray.Builder.AddRange.Array.cs | 9 ++++--- .../FlatArray.FlatList.Impl.Implicit.cs | 12 ++++----- .../FlatArray.InnerArrayHelper.cs | 26 ++++++++++--------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.AddRange.Array.cs b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.AddRange.Array.cs index aa49c944..4938b274 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.AddRange.Array.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.AddRange.Array.cs @@ -46,10 +46,13 @@ private void InnerAddRange(T[] items, int length) Debug.Assert(length > 0 && length <= items.Length); InnerBuilderBufferHelper.EnsureBufferCapacity(ref this.items, this.length, length); - var sourceSpan = length == items.Length - ? new ReadOnlySpan(items) - : new ReadOnlySpan(items, 0, length); + + ReadOnlySpan sourceSpan = length == items.Length + ? new(items) + : new(items, 0, length); + sourceSpan.CopyTo(new Span(this.items, this.length, length)); + this.length += length; } } diff --git a/src/flatcollections-array/FlatArray/FlatArray.T.FlatList/FlatArray.FlatList.Impl.Implicit.cs b/src/flatcollections-array/FlatArray/FlatArray.T.FlatList/FlatArray.FlatList.Impl.Implicit.cs index 708006d0..1a339b7f 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T.FlatList/FlatArray.FlatList.Impl.Implicit.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T.FlatList/FlatArray.FlatList.Impl.Implicit.cs @@ -52,13 +52,13 @@ public void CopyTo(T[] array, int arrayIndex) return; } - var sourceSpan = length == items.Length - ? new ReadOnlySpan(items) - : new ReadOnlySpan(items, 0, length); + ReadOnlySpan sourceSpan = length == items.Length + ? new(items) + : new(items, 0, length); - var destSpan = arrayIndex == default && length == array.Length - ? new Span(array) - : new Span(array, arrayIndex, length); + Span destSpan = arrayIndex == default && length == array.Length + ? new(array) + : new(array, arrayIndex, length); sourceSpan.CopyTo(destSpan); } diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs b/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs index 72f03835..f06395fb 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs @@ -17,9 +17,9 @@ internal static T[] Copy(T[] array, int length) { Debug.Assert(length >= 0 && length <= array.Length); - var sourceSpan = length == array.Length - ? new ReadOnlySpan(array) - : new ReadOnlySpan(array, 0, length); + ReadOnlySpan sourceSpan = length == array.Length + ? new(array) + : new(array, 0, length); return sourceSpan.ToArray(); } @@ -28,9 +28,9 @@ internal static T[] CopySegment(T[] array, int start, int length) { Debug.Assert(InnerAllocHelper.IsSegmentWithinBounds(start, length, array.Length)); - var sourceSpan = start == default && length == array.Length - ? new ReadOnlySpan(array) - : new ReadOnlySpan(array, start, length); + ReadOnlySpan sourceSpan = start == default && length == array.Length + ? new(array) + : new(array, start, length); return sourceSpan.ToArray(); } @@ -40,17 +40,19 @@ internal static T[] Concat(T[] array1, int length1, T[] array2, int length2) Debug.Assert(length1 >= 0 && length1 <= array1.Length); Debug.Assert(length2 >= 0 && length2 <= array2.Length); - var sourceSpan1 = length1 == array1.Length - ? new ReadOnlySpan(array1) - : new ReadOnlySpan(array1, 0, length1); + ReadOnlySpan sourceSpan1 = length1 == array1.Length + ? new(array1) + : new(array1, 0, length1); - var sourceSpan2 = length2 == array2.Length - ? new ReadOnlySpan(array2) - : new ReadOnlySpan(array2, 0, length2); + ReadOnlySpan sourceSpan2 = length2 == array2.Length + ? new(array2) + : new(array2, 0, length2); var result = new T[length1 + length2]; + sourceSpan1.CopyTo(new Span(result, 0, length1)); sourceSpan2.CopyTo(new Span(result, length1, length2)); + return result; } } From c75ea51e072e4504c75a143eb01401fa020bf209 Mon Sep 17 00:00:00 2001 From: Andrei Sergeev Date: Wed, 24 Jan 2024 09:17:39 +0400 Subject: [PATCH 3/3] Minor refactor: explicitly using unchecked when summing array length for clarity --- .../FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs b/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs index f06395fb..4ef7e41b 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/InnerArrayHelper/FlatArray.InnerArrayHelper.cs @@ -48,7 +48,7 @@ internal static T[] Concat(T[] array1, int length1, T[] array2, int length2) ? new(array2) : new(array2, 0, length2); - var result = new T[length1 + length2]; + var result = new T[unchecked(length1 + length2)]; sourceSpan1.CopyTo(new Span(result, 0, length1)); sourceSpan2.CopyTo(new Span(result, length1, length2));