Skip to content

Add a socket receive buffer#1733

Merged
Rob-Hague merged 2 commits intosshnet:developfrom
Rob-Hague:sessionbuffer
Nov 21, 2025
Merged

Add a socket receive buffer#1733
Rob-Hague merged 2 commits intosshnet:developfrom
Rob-Hague:sessionbuffer

Conversation

@Rob-Hague
Copy link
Copy Markdown
Collaborator

Currently an array is allocated to read each packet from the socket, followed by decryption which allocates another array for the plaintext payload. We can save one of these two allocations by adding a persistent buffer for socket receives, and allowing the cipher implementations to decrypt into the given payload array.

We can save the other allocation similarly, but in a separate change.

Comment on lines +1993 to +1996
if (_socket is { } s)
{
s.ReceiveTimeout = 0;
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just moved out from the previous implementation of TrySocketRead

Currently an array is allocated to read each packet from the socket, followed by
decryption which allocates another array for the plaintext payload. We can save one
of these two allocations by adding a persistent buffer for socket receives, and
allowing the cipher implementations to decrypt into the given payload array.

We can save the other allocation similarly, but in a separate change.
@WojciechNagorski
Copy link
Copy Markdown
Collaborator

@Rob-Hague How does this affect performance?

@Rob-Hague
Copy link
Copy Markdown
Collaborator Author

I am not expecting any noticeable speed improvements (will check). I will demonstrate the memory improvements in the next PR 🙂

@Rob-Hague Rob-Hague merged commit 8712c99 into sshnet:develop Nov 21, 2025
11 of 12 checks passed
@Rob-Hague Rob-Hague deleted the sessionbuffer branch November 21, 2025 20:21
Rob-Hague added a commit to Rob-Hague/SSH.NET that referenced this pull request Apr 27, 2026
PR#1752 added a persistent buffer into which to decrypt packets, rather than allocating
a new array for each packet. This was on the back of sshnet#1733 which added support in the
cipher types for decrypting into a given buffer, but for the case of AES-CTR, not into
the same buffer in-place. sshnet#1787 adds that missing support, meaning we can now decrypt
in-place and remove the plaintext buffer.
Rob-Hague added a commit to Rob-Hague/SSH.NET that referenced this pull request Apr 27, 2026
sshnet#1752 added a persistent buffer into which to decrypt packets, rather than allocating
a new array for each packet. This was on the back of sshnet#1733 which added support in the
cipher types for decrypting into a given buffer, but for the case of AES-CTR, not into
the same buffer in-place. sshnet#1787 adds that missing support, meaning we can now decrypt
in-place, and the plaintext buffer becomes unnecessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants