paulirwin commented on PR #1144:
URL: https://github.com/apache/lucenenet/pull/1144#issuecomment-2728071178

   Ran some benchmarks and this shows a notable improvement for StartsWith and 
EndsWith (net9.0, macOS, arm64). Delete/DeleteN didn't see a huge change 
because the `Arrays.Copy` method it uses is already optimized for span, but at 
least it's a more consistent API with the other StemmerUtil methods (and there 
was a small improvement nonetheless).
   
   ```
   
   | Method              | Mean        | Error     | StdDev    | Median      | 
Gen0   | Allocated |
   |-------------------- 
|------------:|----------:|----------:|------------:|-------:|----------:|
   | StartsWithCharArray |  2,504.6 ns |  42.41 ns |  73.16 ns |  2,472.3 ns | 
0.6447 |   3.95 KB |
   | StartsWithSpan      |    636.0 ns |   3.97 ns |   3.52 ns |    635.8 ns | 
0.3223 |   1.98 KB |
   | EndsWithCharArray   |  2,533.8 ns |  49.05 ns |  63.79 ns |  2,504.3 ns | 
0.6447 |   3.95 KB |
   | EndsWithSpan        |    836.1 ns |  12.62 ns |  11.18 ns |    834.6 ns | 
0.3223 |   1.98 KB |
   | DeleteCharArray     | 21,956.9 ns | 305.08 ns | 456.63 ns | 21,742.9 ns | 
0.6409 |   3.95 KB |
   | DeleteSpan          | 21,900.1 ns | 157.66 ns | 139.76 ns | 21,840.0 ns | 
0.6409 |   3.95 KB |
   | DeleteNCharArray    | 22,027.7 ns | 345.74 ns | 288.71 ns | 21,953.6 ns | 
0.6409 |   3.95 KB |
   | DeleteNSpan         | 21,690.5 ns |  68.90 ns |  57.54 ns | 21,681.4 ns | 
0.6409 |   3.95 KB |
   ```
   
   ```csharp
   using System;
   using BenchmarkDotNet.Attributes;
   using Lucene.Net.Analysis.Util;
   
   namespace LuceneNetBenchmarkPlayground;
   
   [MemoryDiagnoser]
   public class BenchmarkLucene
   {
       public static bool StartsWith(ReadOnlySpan<char> s, int len, 
ReadOnlySpan<char> prefix)
       {
           int prefixLen = prefix.Length;
           if (prefixLen > len)
           {
               return false;
           }
           return s.StartsWith(prefix, StringComparison.Ordinal);
       }
   
       public static bool EndsWith(ReadOnlySpan<char> s, int len, 
ReadOnlySpan<char> suffix)
       {
           int suffixLen = suffix.Length;
           if (suffixLen > len)
           {
               return false;
           }
   
           return s.Slice(0, len).EndsWith(suffix, StringComparison.Ordinal);
       }
   
       public static int Delete(Span<char> s, int pos, int len)
       {
           //if (Debugging.AssertsEnabled) Debugging.Assert(pos < len);
           if (pos < len - 1) // don't arraycopy if asked to delete last 
character
           {
               // Arrays.Copy(s, pos + 1, s, pos, len - pos - 1);
               s.Slice(pos + 1, len - pos - 1).CopyTo(s.Slice(pos, len - pos - 
1));
           }
           return len - 1;
       }
   
       public static int DeleteN(Span<char> s, int pos, int len, int nChars)
       {
           //if (Debugging.AssertsEnabled) Debugging.Assert(pos + nChars <= 
len);
           if (pos + nChars < len) // don't arraycopy if asked to delete the 
last characters
           {
               // Arrays.Copy(s, pos + nChars, s, pos, len - pos - nChars);
               s.Slice(pos + nChars, len - pos - nChars).CopyTo(s.Slice(pos, 
len - pos - nChars));
           }
           return len - nChars;
       }
   
       [Benchmark]
       public void StartsWithCharArray()
       {
           char[] s = new string('f', 1000).ToCharArray();
   
           for (int i = 0; i < 1000; i++)
           {
               StemmerUtil.StartsWith(s, i, "fff");
           }
       }
   
       [Benchmark]
       public void StartsWithSpan()
       {
           ReadOnlySpan<char> s = new string('f', 1000);
   
           for (int i = 0; i < 1000; i++)
           {
               StartsWith(s, i, "fff");
           }
       }
   
       [Benchmark]
       public void EndsWithCharArray()
       {
           char[] s = new string('f', 1000).ToCharArray();
   
           for (int i = 0; i < 1000; i++)
           {
               StemmerUtil.EndsWith(s, i, "fff");
           }
       }
   
       [Benchmark]
       public void EndsWithSpan()
       {
           ReadOnlySpan<char> s = new string('f', 1000);
   
           for (int i = 0; i < 1000; i++)
           {
               EndsWith(s, i, "fff");
           }
       }
   
       [Benchmark]
       public void DeleteCharArray()
       {
           char[] s = new string('f', 1000).ToCharArray();
   
           for (int i = 0; i < 1000; i++)
           {
               StemmerUtil.Delete(s, i, 1000);
           }
       }
   
       [Benchmark]
       public void DeleteSpan()
       {
           char[] s = new string('f', 1000).ToCharArray();
   
           for (int i = 0; i < 1000; i++)
           {
               Delete(s, i, 1000);
           }
       }
   
       [Benchmark]
       public void DeleteNCharArray()
       {
           char[] s = new string('f', 1000).ToCharArray();
   
           for (int i = 0; i < 1000; i++)
           {
               StemmerUtil.DeleteN(s, i, 1000, 2);
           }
       }
   
       [Benchmark]
       public void DeleteNSpan()
       {
           char[] s = new string('f', 1000).ToCharArray();
   
           for (int i = 0; i < 1000; i++)
           {
               DeleteN(s, i, 1000, 2);
           }
       }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to