You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2022/11/21 05:03:03 UTC

[lucenenet] 02/05: PERFORMANCE: Lucene.Net.Support.Arrays::Fill(): Replaced for loop implementation with Array.Fill() or Span.Fill() depending on platform.

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 8b0d38340a1ba73a984952745b9c4747e0487ba9
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Mon Nov 21 04:38:11 2022 +0700

    PERFORMANCE: Lucene.Net.Support.Arrays::Fill(): Replaced for loop implementation with Array.Fill() or Span.Fill<T>() depending on platform.
---
 Directory.Build.targets          |  1 +
 src/Lucene.Net/Support/Arrays.cs | 50 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/Directory.Build.targets b/Directory.Build.targets
index cfd7fd835..0caed447e 100644
--- a/Directory.Build.targets
+++ b/Directory.Build.targets
@@ -60,6 +60,7 @@
   <!-- Features in .NET Standard 2.1, .NET 5.x, and .NET 6.x only -->
   <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) ">
 
+    <DefineConstants>$(DefineConstants);FEATURE_ARRAY_FILL</DefineConstants>
     <DefineConstants>$(DefineConstants);FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR</DefineConstants>
     <DefineConstants>$(DefineConstants);FEATURE_CONDITIONALWEAKTABLE_ADDORUPDATE</DefineConstants>
     <DefineConstants>$(DefineConstants);FEATURE_NUMBER_PARSE_READONLYSPAN</DefineConstants>
diff --git a/src/Lucene.Net/Support/Arrays.cs b/src/Lucene.Net/Support/Arrays.cs
index 38b1c0547..62976883f 100644
--- a/src/Lucene.Net/Support/Arrays.cs
+++ b/src/Lucene.Net/Support/Arrays.cs
@@ -76,10 +76,7 @@ namespace Lucene.Net.Support
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static void Fill<T>(T[] a, T val)
         {
-            for (int i = 0; i < a.Length; i++)
-            {
-                a[i] = val;
-            }
+            ArrayFiller<T>.Default.Fill(a, val, 0, a.Length);
         }
 
         /// <summary>
@@ -113,11 +110,52 @@ namespace Lucene.Net.Support
             if (toIndex > a.Length)
                 throw new ArgumentOutOfRangeException(nameof(toIndex));
 
-            for (int i = fromIndex; i < toIndex; i++)
+            int length = toIndex - fromIndex;
+            ArrayFiller<T>.Default.Fill(a, val, fromIndex, length);
+        }
+
+        #region ArrayFiller<T>
+        private class ArrayFiller<T>
+        {
+            public static readonly IArrayFiller<T> Default = LoadArrayFiller();
+
+            private static IArrayFiller<T> LoadArrayFiller()
+            {
+#if FEATURE_ARRAY_FILL
+                if (PlatformDetection.IsNetCore)
+                    return new SpanFillArrayFiller<T>();
+
+                return new ArrayFillArrayFiller<T>();
+#else
+                return new SpanFillArrayFiller<T>();
+#endif
+            }
+
+        }
+
+        private interface IArrayFiller<T>
+        {
+            void Fill(T[] array, T value, int startIndex, int count);
+        }
+
+#if FEATURE_ARRAY_FILL
+        private class ArrayFillArrayFiller<T> : IArrayFiller<T>
+        {
+            public void Fill(T[] array, T value, int startIndex, int count)
             {
-                a[i] = val;
+                Array.Fill(array, value, startIndex, count);
             }
         }
+#endif
+        private class SpanFillArrayFiller<T> : IArrayFiller<T>
+        {
+            public void Fill(T[] array, T value, int startIndex, int count)
+            {
+                array.AsSpan(startIndex, count).Fill(value);
+            }
+        }
+
+        #endregion ArrayFiller<T>
 
         /// <summary>
         /// Copies a range of elements from an Array starting at the first element and pastes them