sshnet / SSH.NET

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.
http://sshnet.github.io/SSH.NET/
MIT License
3.96k stars 931 forks source link

Reduces heap allocations for the some byte[] uses #1272

Closed jacobslusser closed 9 months ago

jacobslusser commented 9 months ago

Profiling reveals that creation of byte[4] arrays are our most common heap objects. Many of these come from reading integers from the byte stream where we allocate a small byte[4] or byte[8] array and then convert it to a 32 or 64-bit integer.

Results from a simple test of using the SftpClient to upload and download some files:

Before: when byte[] arrays were created for each integer read Screenshot 2023-12-10 181955

After: when byte[] are allocated and read from the stack Screenshot 2023-12-10 182136

Changes

Rob-Hague commented 9 months ago

Funny, I had very similar changes in #1138 🙂 Feel free to cherry-pick some of those, or otherwise this looks good to me

jacobslusser commented 9 months ago

Funny, I had very similar changes in #1138 🙂 Feel free to cherry-pick some of those, or otherwise this looks good to me

@Rob-Hague wow, your changes are more than a little similar. At one point I had even ported the ReadExactly just as you had but decided instead to follow our existing pattern. I will definitely look at merging the others.

Why didn't you ever merge this?

Rob-Hague commented 9 months ago

I think I wanted to justify it with some profiling like you did, but got stuck into some other work

WojciechNagorski commented 9 months ago

I will merge any optimization if it has justify as goof as this PR 😃. https://github.com/sshnet/SSH.NET/pull/1138 was a draft.

WojciechNagorski commented 9 months ago

The 2023.0.1 version has been released to Nuget: https://www.nuget.org/packages/SSH.NET/2023.0.1