Optimize SqlClient primitive type writes by Wraith2 · Pull Request #34048 · dotnet/corefx (original) (raw)

When writing some primitive types to the tds packet stream small arrays are allocated and passed to the WriteByteArray function. I have changed WriteByteArray to WriteBytes and added a span parameter so that either a span or an array can be passed in (but not both, the priority is explained in the comments). The function will attempt to write using only a span unless it encounters an end of packet and needs to use an async continuation, in that case it will allocate an array and copy the remainder of the span into it. For primitive types this will rarely be needed.

These changes can be used to allow the new BitConverter.TryWriteBytes overload with stackalloced buffers to remove memory allocations for writing of 16 and 32 bit reals. This can be extended to guids at a later time.

This PR was split from #32811 and into smaller commits for easier review. It has passed the manual and efcore tests in native mode, the tests cannot be successfully run in managed mode due to #33930 . Performance improvements for this and related PR's are in the original discussion.
/cc @keeratsingh @afsanehr @saurabh500 @David-Engel