Skip to content

Move a few stackallocs out of loops#34149

Merged
stephentoub merged 1 commit intodotnet:masterfrom
stephentoub:movestackalloc
Mar 27, 2020
Merged

Move a few stackallocs out of loops#34149
stephentoub merged 1 commit intodotnet:masterfrom
stephentoub:movestackalloc

Conversation

@stephentoub
Copy link
Member

@VSadov
Copy link
Member

VSadov commented Mar 26, 2020

One example where value arrays could be handy.

You could do the following inside the loop or outside and either way it would work as expected:

     byte[256] buffer = default;

     for (int i = 0; i < buffer.Length; i++) 
           buffer[i] = ... ; 

     byte* ptrNeeded = &buffer[0];
     . . .

@stephentoub
Copy link
Member Author

One example where value arrays could be handy.

Did you mean pinned arrays? If not, what's a value array?

@stephentoub
Copy link
Member Author

Oh, I see, your syntax byte[buffSize] buffer = .... Presumably a stack-based array that doesn't behave like stackalloc.

@VSadov
Copy link
Member

VSadov commented Mar 26, 2020

Yes. That would be just a struct with a length and indexer. (like Go arrays)
Length would have to be a constant expr though, which is not a problem here.

@VSadov
Copy link
Member

VSadov commented Mar 26, 2020

You could use pinned array also.
That would make the finally unnecessary, but then you would need to get the pointer and KeepAlive the array until done. Not sure if that is worth the trouble for this case.

@stephentoub
Copy link
Member Author

Not sure if that is worth the trouble for this case.

Yeah i considered doing that and decided it wasn't :)

@stephentoub stephentoub merged commit 1d9067b into dotnet:master Mar 27, 2020
@stephentoub stephentoub deleted the movestackalloc branch April 17, 2020 20:02
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants