Optimize memory allocations in BaseClient, SshClient, and SftpClient

Co-authored-by: WojciechNagorski <17333903+WojciechNagorski@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-10-23 11:29:48 +00:00
parent 3fb183460a
commit 37e2ec2358
3 changed files with 12 additions and 7 deletions
+1 -1
View File
@@ -550,7 +550,7 @@ namespace Renci.SshNet
/// </returns>
private Timer CreateKeepAliveTimer(TimeSpan dueTime, TimeSpan period)
{
return new Timer(state => SendKeepAliveMessage(), Session, dueTime, period);
return new Timer(static state => ((BaseClient)state!).SendKeepAliveMessage(), this, dueTime, period);
}
private ISession CreateAndConnectSession()
+4 -5
View File
@@ -4,7 +4,6 @@ using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
@@ -2120,7 +2119,7 @@ namespace Renci.SshNet
{
if (!Directory.Exists(sourcePath))
{
throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));
throw new FileNotFoundException($"Source directory not found: {sourcePath}");
}
var uploadedFiles = new List<FileInfo>();
@@ -2170,7 +2169,7 @@ namespace Renci.SshNet
if (isDifferent)
{
var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
var remoteFileName = $"{destinationPath}/{localFile.Name}";
try
{
using (var file = File.OpenRead(localFile.FullName))
@@ -2237,7 +2236,7 @@ namespace Renci.SshNet
if (!basePath.EndsWith("/", StringComparison.Ordinal))
#endif
{
basePath = string.Format("{0}/", fullPath);
basePath = $"{fullPath}/";
}
var result = new List<ISftpFile>();
@@ -2249,7 +2248,7 @@ namespace Renci.SshNet
foreach (var f in files)
{
result.Add(new SftpFile(_sftpSession,
string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key),
$"{basePath}{f.Key}",
f.Value));
}
+7 -1
View File
@@ -18,6 +18,11 @@ namespace Renci.SshNet
/// </summary>
private readonly List<ForwardedPort> _forwardedPorts;
/// <summary>
/// Cached readonly collection of forwarded ports.
/// </summary>
private readonly IEnumerable<ForwardedPort> _forwardedPortsReadOnly;
/// <summary>
/// Holds a value indicating whether the current instance is disposed.
/// </summary>
@@ -33,7 +38,7 @@ namespace Renci.SshNet
{
get
{
return _forwardedPorts.AsReadOnly();
return _forwardedPortsReadOnly;
}
}
@@ -137,6 +142,7 @@ namespace Renci.SshNet
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
_forwardedPorts = new List<ForwardedPort>();
_forwardedPortsReadOnly = _forwardedPorts.AsReadOnly();
}
/// <summary>