Remove some StringBuilder-related allocations from System.Net.Http by stephentoub · Pull Request #30508 · dotnet/corefx (original) (raw)

picenka21 pushed a commit to picenka21/runtime that referenced this pull request

Feb 18, 2022

@stephentoub

…otnet/corefx#30508)

The current code uses ToString(object), boxng the long? and then calling ToString on it. By instead first checking whether the value is null and only calling ToString(long), we avoid both the box and the string allocations, for both From and To, relying on StringBuilder's ability to format a long directly into its buffer.

We can just write into the existing StringBuilder rather than creating a new one, appending to that, copying it to a string, and then appending that string to the original.

Commit migrated from dotnet/corefx@8c0487b