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

Add array pool and increase max stackalloc #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jetersen
Copy link

Added support for use ArrayPool.Shared and increasing the max stackalloc size.

512 or even 1024 should be totally safe if it is the only stack allocation that happens in the Encode or Decode method body's stack frame.

Span<char> alphabetTemp = _alphabet.Length * sizeof(char) > MaxStackallocSize // NOTE: We multiply the number of characters by the size of a `char` to get the actual amount of memory that would be allocated.
? new char[_alphabet.Length]
? (pooledArray = ArrayPool<char>.Shared.Rent(_alphabet.Length)).AsSpan(_alphabet.Length)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AsSpan is to ensure the correct length. As Rent most likely will give us an Array that is too big and avoids the implicit conversion.

@jetersen
Copy link
Author

jetersen commented Jan 31, 2024

Not sure the _alphabet.Length * sizeof(char) is required cause if the alphabet was length 256 it would have the same length as stackalloc char[256]

@jetersen
Copy link
Author

Oh in any case the single-byte character requirement in the constructor would prevent ever needing an array that is larger than 256 or even 512 😓 So not even sure why we need this check in this code 🤔

Because we are specifically looking for single-byte characters that are you unique, we can only fit up to 128 unique characters in a char array.
So ya, we basically do not need an array pool. We could settle for stackalloc of 128 char making it an constant and then just slicing the Span to the alphabet length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant