Skip to content

Commit eb3ca5b

Browse files
authored
const for max buffer size for stackalloc (#73)
introduced constant for max buffer size allegeable for stackalloc
1 parent b2c7877 commit eb3ca5b

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/Hashids.net/Hashids.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public partial class Hashids : IHashids
1313
public const string DEFAULT_ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
1414
public const string DEFAULT_SEPS = "cfhistuCFHISTU";
1515
public const int MIN_ALPHABET_LENGTH = 16;
16+
public const int MAX_STACKALLOC_SIZE = 512;
1617

1718
private const double SEP_DIV = 3.5;
1819
private const double GUARD_DIV = 12.0;
@@ -289,13 +290,13 @@ private string GenerateHashFrom(ReadOnlySpan<long> numbers)
289290

290291
var stringBuilder = StringBuilderPool.Get();
291292

292-
Span<char> alphabet = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
293+
Span<char> alphabet = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
293294
_alphabet.CopyTo(alphabet);
294295

295296
var lottery = alphabet[(int)(numbersHashInt % _alphabet.Length)];
296297
stringBuilder.Append(lottery);
297298

298-
Span<char> shuffleBuffer = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
299+
Span<char> shuffleBuffer = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
299300
shuffleBuffer[0] = lottery;
300301
_salt.AsSpan().Slice(0, Math.Min(_salt.Length, _alphabet.Length - 1)).CopyTo(shuffleBuffer.Slice(1));
301302

@@ -419,10 +420,10 @@ private long[] GetNumbersFrom(string hash)
419420

420421
var result = new long[hashArray.Length];
421422

422-
Span<char> alphabet = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
423+
Span<char> alphabet = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
423424
_alphabet.CopyTo(alphabet);
424425

425-
Span<char> buffer = _alphabet.Length < 512 ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
426+
Span<char> buffer = _alphabet.Length < MAX_STACKALLOC_SIZE ? stackalloc char[_alphabet.Length] : new char[_alphabet.Length];
426427
buffer[0] = lottery;
427428
_salt.AsSpan().Slice(0, Math.Min(_salt.Length, _alphabet.Length - 1)).CopyTo(buffer.Slice(1));
428429

0 commit comments

Comments
 (0)