Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERFORMANCE: Lucene.Net.Analysis.TokenAttributes.CharTermAttribute: Optimized char copying on Append() and Subsequence() #899

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ public ICharSequence Subsequence(int startIndex, int length)
throw new ArgumentOutOfRangeException(nameof(length), $"Index and length must refer to a location within the string. For example {nameof(startIndex)} + {nameof(length)} <= {nameof(Length)}.");

char[] result = new char[length];
for (int i = 0, j = startIndex; i < length; i++, j++)
result[i] = termBuffer[j];

Arrays.Copy(termBuffer, startIndex, result, 0, length);
return new CharArrayCharSequence(result);
}

Expand Down Expand Up @@ -281,7 +279,10 @@ public CharTermAttribute Append(StringBuilder value)
return this; // No-op
}

return Append(value.ToString());
int len = value.Length;
value.CopyTo(0, InternalResizeBuffer(termLength + len), termLength, len);
Length += len;
return this;
}

public CharTermAttribute Append(StringBuilder value, int startIndex, int charCount)
Expand All @@ -303,7 +304,9 @@ public CharTermAttribute Append(StringBuilder value, int startIndex, int charCou
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException(nameof(startIndex), $"Index and length must refer to a location within the string. For example {nameof(startIndex)} + {nameof(charCount)} <= {nameof(Length)}.");

return Append(value.ToString(startIndex, charCount));
value.CopyTo(startIndex, InternalResizeBuffer(termLength + charCount), termLength, charCount);
Length += charCount;
return this;
}

public CharTermAttribute Append(ICharTermAttribute value)
Expand Down