Skip to content

Conversation

@Rob-Hague
Copy link
Collaborator

Several commonly used Write methods on SshDataStream end up calling Write(ReadOnlySpan) on the base MemoryStream. But since SshDataStream is a derived type, that method just rents a buffer and hands it to Write(byte[], int, int), which defeats any stackalloc'ing or renting that SshDataStream does itself. Instead, with a bit extra accounting we can just write directly into the underlying buffer.

In a test app, the events are showing thousands like this:

image

With this change, the number of "BufferRented" events drops from 13k to 50

Several commonly used Write methods on SshDataStream end up calling Write(ReadOnlySpan)
on the base MemoryStream. But since SshDataStream is a derived type, that method just
rents a buffer and hands it to Write(byte[], int, int), which defeats any stackalloc'ing
or renting that SshDataStream does itself. Instead, with a bit extra accounting we can
just write directly into the underlying buffer.
# Conflicts:
#	src/Renci.SshNet/Common/Extensions.cs
@Rob-Hague Rob-Hague merged commit cbbbdde into sshnet:develop Nov 21, 2025
6 checks passed
@Rob-Hague Rob-Hague deleted the sshdatastream-write branch November 21, 2025 19:13
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