Skip to content

Commit cf05904

Browse files
committed
The stack_alloc buffer is now created using the maximum needed size for the input string (length of input string * maximum needed lenght for a single char)
1 parent ceb8d2f commit cf05904

File tree

4 files changed

+4
-11
lines changed

4 files changed

+4
-11
lines changed

Unidecoder.Characters.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static partial class Unidecoder
3030
{
3131
private static readonly string[][] characters;
3232
private static readonly int MaxDecodedCharLength;
33-
private static readonly int MaxStringLengthForStackAlloc;
3433

3534
static Unidecoder()
3635
{
@@ -549,7 +548,6 @@ static Unidecoder()
549548
if (str.Length > MaxDecodedCharLength)
550549
MaxDecodedCharLength = str.Length;
551550
}
552-
MaxStringLengthForStackAlloc = STACKALLOC_BUFFER_SIZE / MaxDecodedCharLength - 1;
553551
}
554552
}
555553
}

py2cs.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
{
3737
private static readonly string[][] characters;
3838
private static readonly int MaxDecodedCharLength;
39-
private static readonly int MaxStringLengthForStackAlloc;
4039
4140
static Unidecoder()
4241
{
@@ -97,7 +96,6 @@ def formatch(ch, cc):
9796
if (str.Length > MaxDecodedCharLength)
9897
MaxDecodedCharLength = str.Length;
9998
}
100-
MaxStringLengthForStackAlloc = STACKALLOC_BUFFER_SIZE / MaxDecodedCharLength - 1;
10199
}
102100
}
103101
}

src/Unidecoder.Characters.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static partial class Unidecoder
3030
{
3131
private static readonly string[][] characters;
3232
private static readonly int MaxDecodedCharLength;
33-
private static readonly int MaxStringLengthForStackAlloc;
3433

3534
static Unidecoder()
3635
{
@@ -549,8 +548,6 @@ static Unidecoder()
549548
if (str.Length > MaxDecodedCharLength)
550549
MaxDecodedCharLength = str.Length;
551550
}
552-
MaxStringLengthForStackAlloc = STACKALLOC_BUFFER_SIZE / MaxDecodedCharLength - 1;
553551
}
554552
}
555553
}
556-

src/Unidecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static partial class Unidecoder
2020
{
2121
// for short strings I use a buffer allocated in the stack instead of a stringbuilder.
2222
// (this is faster and gives less work to the garbage collector)
23-
private const int STACKALLOC_BUFFER_SIZE = 8192;
23+
private const int MAX_STACKALLOC_BUFFER_SIZE = 8192;
2424

2525
[SkipLocalsInit] // this is to avoid the local raw buffer variable stackBuffer do be zeroed for every call: we don't need it and is very cpu intensive (this attribute needs unsafe compliation)
2626
/// <summary>
@@ -40,12 +40,12 @@ public static string Unidecode(this string input, int? tempStringBuilderCapacity
4040
{
4141
if (string.IsNullOrEmpty(input))
4242
return "";
43-
44-
if (input.Length >= MaxStringLengthForStackAlloc)
43+
var neededBufferSize = input.Length * MaxDecodedCharLength + 1;
44+
if (neededBufferSize >= MAX_STACKALLOC_BUFFER_SIZE)
4545
return SlowUnidecode(input, tempStringBuilderCapacity);
4646

4747
bool noConversionNeeded = true;
48-
Span<char> stackBuffer = stackalloc char[STACKALLOC_BUFFER_SIZE];
48+
Span<char> stackBuffer = stackalloc char[neededBufferSize];
4949
int buffIdx = 0;
5050
foreach (char c in input)
5151
{

0 commit comments

Comments
 (0)