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: Removed ValueStringBuilder and reworked methods that used it to use stack/array pool explicitly #85

Merged
merged 21 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4d289a0
Directory.Build.targets: Added FEATURE_STRINGBUILDER_COPYTO_SPAN so w…
NightOwl888 Jan 2, 2024
11d0d98
PERFORMANCE: J2N.Character.OffsetByCodepoints(): Switched from ValueS…
NightOwl888 Jan 2, 2024
e5d0ae9
PERFORMANCE: J2N.Character.CodePointCount(): Switched from ValueStrin…
NightOwl888 Jan 2, 2024
369d74c
J2N.Character: Removed ValueStringBuilderIndexer overloads for CodePo…
NightOwl888 Jan 2, 2024
c93377c
PERFORMANCE: J2N.Text.StringExtensions (RegionMatches + ContentEquals…
NightOwl888 Jan 2, 2024
369eca7
PERFORMANCE: J2N.Text.StringExtensions::CompareToOrdinal(StringBuilde…
NightOwl888 Jan 2, 2024
13a1d01
PERFORMANCE: J2N.Text.CharArrayExtensions::CompareToOrdinal(StringBui…
NightOwl888 Jan 2, 2024
3f4d4c8
PERFORMANCE: J2N.Text.CharArrayExtensions::Subsequence(): Use Array.C…
NightOwl888 Jan 2, 2024
0123888
PERFORMANCE: J2N.Text.StringBuilderExtensions::CompareToOrdinal(): Sw…
NightOwl888 Jan 2, 2024
f0e031a
PERFORMANCE: J2N.Text.StringBuilderExtensions (IndexOf() + LastIndexO…
NightOwl888 Jan 2, 2024
78cd9fb
PERFORMANCE: J2N.StringBuilderExtensions::Replace(): Removed the call…
NightOwl888 Jan 2, 2024
362cd37
PERFORMANCE: J2N.Text.StringBuilderExtensions::Reverse(): Copy the ch…
NightOwl888 Jan 1, 2024
5bb6334
PERFORMANCE: J2N.Text (CharArrayCharSequence + StringBuilderCharSeque…
NightOwl888 Jan 2, 2024
e583fdc
PERFORMANCE: J2N.Text.CharSequenceComparer: Switched from ValueString…
NightOwl888 Jan 2, 2024
f262c28
PERFORMANCE: J2N.Text.StringBuilderExtensions::Replace(): Use GetChun…
NightOwl888 Jan 4, 2024
ca28947
BUG: J2N.Text.StringBuilderExtensions: We need to be explicit when ca…
NightOwl888 Jan 5, 2024
ad9d312
J2N.Character::ToString(): Use MemoryMarshal.GetReference() when deal…
NightOwl888 Jan 5, 2024
1433961
J2N.Memory.MemoryExtensions::ReverseText(): Use MemoryMarshal.GetRefe…
NightOwl888 Jan 5, 2024
071352f
J2N.Text: Removed ValueStringBuilderIndexer, ValueStringBuilderChunkI…
NightOwl888 Jan 5, 2024
442a5e2
J2N.Memory: Removed Int32Packer and ValueInt32Packer.
NightOwl888 Jan 5, 2024
0eae45d
J2N.Text.StringBuilderExtensions:Insert(): Removed incorrect comments…
NightOwl888 Jan 6, 2024
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
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_APPEND_READONLYSPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_APPEND_STRINGBUILDER</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_APPENDJOIN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_COPYTO_SPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_EQUALS_READONLYSPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_STRINGBUILDER_INSERT_READONLYSPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_TEXTWRITER_WRITE_READONLYSPAN</DefineConstants>
Expand Down
241 changes: 140 additions & 101 deletions src/J2N/Character.cs

Large diffs are not rendered by default.

134 changes: 0 additions & 134 deletions src/J2N/Memory/Int32Packer.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/J2N/Memory/MemoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;

namespace J2N.Memory
{
Expand Down Expand Up @@ -45,7 +46,7 @@ public unsafe static void ReverseText(this Span<char> text)
{
int count = text.Length;
if (count == 0) return;
fixed (char* textPtr = text)
fixed (char* textPtr = &MemoryMarshal.GetReference(text))
{
ReverseText(textPtr, count);
}
Expand Down
135 changes: 0 additions & 135 deletions src/J2N/Memory/ValueInt32Packer.cs

This file was deleted.

46 changes: 41 additions & 5 deletions src/J2N/Text/CharArrayCharSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#endregion

using System;
#if FEATURE_ARRAYPOOL
using System.Buffers;
#endif
using System.Diagnostics.CodeAnalysis;
using System.Text;

Expand All @@ -34,6 +37,8 @@ public class CharArrayCharSequence : ICharSequence,
IEquatable<CharArrayCharSequence>, IEquatable<StringBuilderCharSequence>, IEquatable<StringCharSequence>,
IEquatable<string>, IEquatable<StringBuilder>, IEquatable<char[]>
{
private const int CharStackBufferSize = 64;

/// <summary>
/// Initializes a new instance of <see cref="CharArrayCharSequence"/> with the provided <paramref name="value"/>.
/// </summary>
Expand Down Expand Up @@ -338,13 +343,44 @@ public bool Equals(StringBuilder? other)
return false;

int len = Length;
if (len != other.Length) return false;
using var otherIndexer = new ValueStringBuilderIndexer(other);
for (int i = 0; i < len; i++)
int otherLength = other.Length;
if (len != otherLength) return false;

#if FEATURE_STRINGBUILDER_COPYTO_SPAN // If this method isn't supported, we are buffering to an array pool to get to the stack, anyway.
bool usePool = otherLength > CharStackBufferSize;
char[]? otherArrayToReturnToPool = usePool ? ArrayPool<char>.Shared.Rent(otherLength) : null;
try
#elif FEATURE_ARRAYPOOL
char[] otherChars = ArrayPool<char>.Shared.Rent(otherLength);
try
#else
char[] otherChars = new char[otherLength];
#endif
{
if (!value[i].Equals(otherIndexer[i])) return false;
#if FEATURE_STRINGBUILDER_COPYTO_SPAN
Span<char> otherChars = usePool ? otherArrayToReturnToPool : stackalloc char[otherLength];
other.CopyTo(0, otherChars, otherLength);
#else
other.CopyTo(0, otherChars, 0, otherLength);
#endif
for (int i = 0; i < len; i++)
{
if (!value[i].Equals(otherChars[i])) return false;
}
return true;
}
return true;
#if FEATURE_STRINGBUILDER_COPYTO_SPAN
finally
{
if (otherArrayToReturnToPool != null)
ArrayPool<char>.Shared.Return(otherArrayToReturnToPool);
}
#elif FEATURE_ARRAYPOOL
finally
{
ArrayPool<char>.Shared.Return(otherChars);
}
#endif
}

/// <summary>
Expand Down
Loading