Skip to content

Commit 37e2ec2

Browse files
Optimize memory allocations in BaseClient, SshClient, and SftpClient
Co-authored-by: WojciechNagorski <[email protected]>
1 parent 3fb1834 commit 37e2ec2

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/Renci.SshNet/BaseClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ private void StartKeepAliveTimer()
550550
/// </returns>
551551
private Timer CreateKeepAliveTimer(TimeSpan dueTime, TimeSpan period)
552552
{
553-
return new Timer(state => SendKeepAliveMessage(), Session, dueTime, period);
553+
return new Timer(static state => ((BaseClient)state!).SendKeepAliveMessage(), this, dueTime, period);
554554
}
555555

556556
private ISession CreateAndConnectSession()

src/Renci.SshNet/SftpClient.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Globalization;
87
using System.IO;
98
using System.Linq;
109
using System.Net;
@@ -2120,7 +2119,7 @@ private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string
21202119
{
21212120
if (!Directory.Exists(sourcePath))
21222121
{
2123-
throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));
2122+
throw new FileNotFoundException($"Source directory not found: {sourcePath}");
21242123
}
21252124

21262125
var uploadedFiles = new List<FileInfo>();
@@ -2170,7 +2169,7 @@ private List<FileInfo> InternalSynchronizeDirectories(string sourcePath, string
21702169

21712170
if (isDifferent)
21722171
{
2173-
var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
2172+
var remoteFileName = $"{destinationPath}/{localFile.Name}";
21742173
try
21752174
{
21762175
using (var file = File.OpenRead(localFile.FullName))
@@ -2237,7 +2236,7 @@ private List<ISftpFile> InternalListDirectory(string path, SftpListDirectoryAsyn
22372236
if (!basePath.EndsWith("/", StringComparison.Ordinal))
22382237
#endif
22392238
{
2240-
basePath = string.Format("{0}/", fullPath);
2239+
basePath = $"{fullPath}/";
22412240
}
22422241

22432242
var result = new List<ISftpFile>();
@@ -2249,7 +2248,7 @@ private List<ISftpFile> InternalListDirectory(string path, SftpListDirectoryAsyn
22492248
foreach (var f in files)
22502249
{
22512250
result.Add(new SftpFile(_sftpSession,
2252-
string.Format(CultureInfo.InvariantCulture, "{0}{1}", basePath, f.Key),
2251+
$"{basePath}{f.Key}",
22532252
f.Value));
22542253
}
22552254

src/Renci.SshNet/SshClient.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public class SshClient : BaseClient, ISshClient
1818
/// </summary>
1919
private readonly List<ForwardedPort> _forwardedPorts;
2020

21+
/// <summary>
22+
/// Cached readonly collection of forwarded ports.
23+
/// </summary>
24+
private readonly IEnumerable<ForwardedPort> _forwardedPortsReadOnly;
25+
2126
/// <summary>
2227
/// Holds a value indicating whether the current instance is disposed.
2328
/// </summary>
@@ -33,7 +38,7 @@ public IEnumerable<ForwardedPort> ForwardedPorts
3338
{
3439
get
3540
{
36-
return _forwardedPorts.AsReadOnly();
41+
return _forwardedPortsReadOnly;
3742
}
3843
}
3944

@@ -137,6 +142,7 @@ internal SshClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServ
137142
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
138143
{
139144
_forwardedPorts = new List<ForwardedPort>();
145+
_forwardedPortsReadOnly = _forwardedPorts.AsReadOnly();
140146
}
141147

142148
/// <summary>

0 commit comments

Comments
 (0)