From e2df39e9f6c8b2ce4dddbfb0b815e192b21849c6 Mon Sep 17 00:00:00 2001 From: Sedat Kapanoglu Date: Fri, 18 Oct 2024 20:11:29 -0700 Subject: [PATCH 1/2] avoid double dictionary lookups --- FluentFTP/Helpers/Hashing/HashAlgorithms.cs | 16 ++++++---------- FluentFTP/Streams/FtpSocketStream.cs | 19 +++---------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/FluentFTP/Helpers/Hashing/HashAlgorithms.cs b/FluentFTP/Helpers/Hashing/HashAlgorithms.cs index f2f5e48b5..07282daa0 100644 --- a/FluentFTP/Helpers/Hashing/HashAlgorithms.cs +++ b/FluentFTP/Helpers/Hashing/HashAlgorithms.cs @@ -29,11 +29,9 @@ internal static class HashAlgorithms { /// Name of the hash algorithm /// The FtpHashAlgorithm public static FtpHashAlgorithm FromString(string name) { - if (!NameToEnum.ContainsKey(name.ToUpper())) { - throw new NotImplementedException("Unknown hash algorithm: " + name); - } - - return NameToEnum[name]; + return NameToEnum.TryGetValue(name, out var value) + ? value + : throw new NotImplementedException("Unknown hash algorithm: " + name); } /// @@ -42,11 +40,9 @@ public static FtpHashAlgorithm FromString(string name) { /// FtpHashAlgorithm to be converted into string /// Name of the hash algorithm public static string PrintToString(this FtpHashAlgorithm name) { - if (!EnumToName.ContainsKey(name)) { - return name.ToString(); - } - - return EnumToName[name]; + return EnumToName.TryGetValue(name, out var value) + ? value + : name.ToString(); } private static readonly List AlgoPreference = new List { diff --git a/FluentFTP/Streams/FtpSocketStream.cs b/FluentFTP/Streams/FtpSocketStream.cs index 43540e9de..53eebc106 100644 --- a/FluentFTP/Streams/FtpSocketStream.cs +++ b/FluentFTP/Streams/FtpSocketStream.cs @@ -862,12 +862,8 @@ private static bool IsIpVersionAllowed(IPAddress ipad, FtpIpVersion ipVersions, /// /// The host to query private IPAddress[] GetCachedHostAddresses(string host) { - IPAddress[] ipads; - if (Client.Status.CachedHostIpads.ContainsKey(host)) { - ipads = Client.Status.CachedHostIpads[host]; - } - else { + if (!Client.Status.CachedHostIpads.TryGetValue(host, out IPAddress[] ipads)) { #if NETSTANDARD || NET5_0_OR_GREATER ipads = Dns.GetHostAddressesAsync(host).Result; #else @@ -885,12 +881,7 @@ private IPAddress[] GetCachedHostAddresses(string host) { /// The host to query /// The IP address to store in the cache private void SetCachedHostAddresses(string host, IPAddress ipad) { - if (Client.Status.CachedHostIpads.ContainsKey(host)) { - Client.Status.CachedHostIpads[host] = new IPAddress[1] { ipad }; - } - else { - Client.Status.CachedHostIpads.Add(host, new IPAddress[1] { ipad }); - } + Client.Status.CachedHostIpads[host] = new IPAddress[1] { ipad }; } /// @@ -1021,12 +1012,8 @@ private bool ConnectHelper(IPAddress ipad, int port) { /// The host to query /// The token that can be used to cancel the entire process private async Task GetCachedHostAddressesAsync(string host, CancellationToken token) { - IPAddress[] ipads; - if (Client.Status.CachedHostIpads.ContainsKey(host)) { - ipads = Client.Status.CachedHostIpads[host]; - } - else { + if (!Client.Status.CachedHostIpads.TryGetValue(host, out IPAddress[] ipads)) { #if NET6_0_OR_GREATER ipads = await Dns.GetHostAddressesAsync(host, token); #else From 6a0312825c638c0bdf40640f9d76252bde69440b Mon Sep 17 00:00:00 2001 From: Sedat Kapanoglu Date: Fri, 18 Oct 2024 20:19:26 -0700 Subject: [PATCH 2/2] avoid ArgumentException in multi-thread scenarios --- FluentFTP/Streams/FtpSocketStream.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FluentFTP/Streams/FtpSocketStream.cs b/FluentFTP/Streams/FtpSocketStream.cs index 53eebc106..a5fc71abe 100644 --- a/FluentFTP/Streams/FtpSocketStream.cs +++ b/FluentFTP/Streams/FtpSocketStream.cs @@ -869,7 +869,7 @@ private IPAddress[] GetCachedHostAddresses(string host) { #else ipads = Dns.GetHostAddresses(host); #endif - Client.Status.CachedHostIpads.Add(host, ipads); + Client.Status.CachedHostIpads[host] = ipads; } return ipads; @@ -1019,7 +1019,7 @@ private async Task GetCachedHostAddressesAsync(string host, Cancell #else ipads = await Dns.GetHostAddressesAsync(host); #endif - Client.Status.CachedHostIpads.Add(host, ipads); + Client.Status.CachedHostIpads[host] = ipads; } return ipads;