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/10/31 06:19:21 UTC
[lucenenet] 13/14: PERFORMANCE: Lucene.Net.Analysis.Util.OpenStringBuilder: Added overloads of UnsafeWrite() for string an ICharSequence. Optimized Append() methods to call UnsafeWrite with index and count to optimize the operation depending on the type of object passed.
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 1d74f980a0aaba980c9c79e4cf8fd70b0917ef35
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Fri Oct 28 12:02:56 2022 +0700
PERFORMANCE: Lucene.Net.Analysis.Util.OpenStringBuilder: Added overloads of UnsafeWrite() for string an ICharSequence. Optimized Append() methods to call UnsafeWrite with index and count to optimize the operation depending on the type of object passed.
---
.../Analysis/Util/OpenStringBuilder.cs | 54 +++++++++++++++-------
1 file changed, 38 insertions(+), 16 deletions(-)
diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Util/OpenStringBuilder.cs b/src/Lucene.Net.Analysis.Common/Analysis/Util/OpenStringBuilder.cs
index 90718612b..ad2f687a2 100644
--- a/src/Lucene.Net.Analysis.Common/Analysis/Util/OpenStringBuilder.cs
+++ b/src/Lucene.Net.Analysis.Common/Analysis/Util/OpenStringBuilder.cs
@@ -82,10 +82,7 @@ namespace Lucene.Net.Analysis.Util
public virtual OpenStringBuilder Append(ICharSequence csq, int startIndex, int count) // LUCENENET specific: changed to startIndex/length to match .NET
{
EnsureCapacity(count);
- for (int i = startIndex; i < startIndex + count; i++)
- {
- UnsafeWrite(csq[i]);
- }
+ UnsafeWrite(csq, startIndex, count);
return this;
}
@@ -99,10 +96,7 @@ namespace Lucene.Net.Analysis.Util
public virtual OpenStringBuilder Append(string csq, int startIndex, int count) // LUCENENET specific: changed to startIndex/length to match .NET
{
EnsureCapacity(count);
- for (int i = startIndex; i < startIndex + count; i++)
- {
- UnsafeWrite(csq[i]);
- }
+ UnsafeWrite(csq, startIndex, count);
return this;
}
@@ -116,10 +110,7 @@ namespace Lucene.Net.Analysis.Util
public virtual OpenStringBuilder Append(StringBuilder csq, int startIndex, int count) // LUCENENET specific: changed to startIndex/length to match .NET
{
EnsureCapacity(count);
- for (int i = startIndex; i < startIndex + count; i++)
- {
- UnsafeWrite(csq[i]);
- }
+ UnsafeWrite(csq, startIndex, count);
return this;
}
@@ -134,10 +125,7 @@ namespace Lucene.Net.Analysis.Util
public virtual OpenStringBuilder Append(char[] value, int startIndex, int count)
{
EnsureCapacity(count);
- for (int i = startIndex; i < startIndex + count; i++)
- {
- UnsafeWrite(value[i]);
- }
+ UnsafeWrite(value, startIndex, count);
return this;
}
@@ -210,6 +198,40 @@ namespace Lucene.Net.Analysis.Util
this.m_len += len;
}
+ // LUCENENET specific overload for string
+ public virtual void UnsafeWrite(string b, int off, int len)
+ {
+ b.CopyTo(off, m_buf, this.m_len, len);
+ this.m_len += len;
+ }
+
+ // LUCENENET specific overload for ICharSequence
+ public virtual void UnsafeWrite(ICharSequence b, int off, int len)
+ {
+ if (!b.HasValue) return;
+
+ if (b is StringBuilderCharSequence sb)
+ {
+ UnsafeWrite(sb.Value, off, len);
+ return;
+ }
+ if (b is StringCharSequence str)
+ {
+ UnsafeWrite(str.Value, off, len);
+ return;
+ }
+ if (b is CharArrayCharSequence chars)
+ {
+ UnsafeWrite(chars.Value, off, len);
+ return;
+ }
+
+ for (int i = off; i < off + len; i++)
+ {
+ UnsafeWrite(b[i]);
+ }
+ }
+
protected virtual void Resize(int len)
{
char[] newbuf = new char[Math.Max(m_buf.Length << 1, len)];