-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Replace local allocations with stack allocated spans #33781
Comments
Estimates:
|
Suggested severity: info C# built in types sizes: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#characteristics-of-the-integral-types Examples: // Restrictions:
// sizeof(T)*length -> Suggested max total size of 1 KB
// Limited to only primitive types
// Not passed to other methods
// Not used inside loops
// Flagged and suggested
int[] arr = new int[5];
Span<int> arrs = stackalloc int[5];
// Flagged and suggested
byte[] arr2 = new byte[] { 1, 2, 3 };
Span<byte> arr2s = stackalloc byte[] { 1, 2, 3 };
// Not flagged due to larger than suggested limit for its size
// Size limitation for flagging:
// sizeof(long) = 64 bit = 8B
// max length for long = 128
long[] p = new long[130];
while (true)
{
// Not flagged due to creation in loop
int[] x = new int[3];
}
// Not flagged due to passed around
byte[] y = new byte[4];
MyMethod(y);
// Not flagged due to not primitive
MyClass[] w = new MyClass[8]; |
I'd suggest two modifications in order to address these concerns:
|
We should probably just close this. There are too many gotchas. |
Flag places where known small temporary arrays of primitives (e.g. with a small constant length / where the total size of
sizeof(T)*length
can be determined to be less than some threshold) not inside any loop and not passed around could be replaced by span-basedstackalloc
.Category: Performance
The text was updated successfully, but these errors were encountered: