Skip to content

Commit

Permalink
Merge pull request robinrodricks#1667 from ssg/master
Browse files Browse the repository at this point in the history
Improve dictionary lookups
  • Loading branch information
FanDjango authored Oct 20, 2024
2 parents 2990048 + 6a03128 commit 3b3f4b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
16 changes: 6 additions & 10 deletions FluentFTP/Helpers/Hashing/HashAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ internal static class HashAlgorithms {
/// <param name="name">Name of the hash algorithm</param>
/// <returns>The FtpHashAlgorithm</returns>
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);
}

/// <summary>
Expand All @@ -42,11 +40,9 @@ public static FtpHashAlgorithm FromString(string name) {
/// <param name="name">FtpHashAlgorithm to be converted into string</param>
/// <returns>Name of the hash algorithm</returns>
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<FtpHashAlgorithm> AlgoPreference = new List<FtpHashAlgorithm> {
Expand Down
23 changes: 5 additions & 18 deletions FluentFTP/Streams/FtpSocketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -862,18 +862,14 @@ private static bool IsIpVersionAllowed(IPAddress ipad, FtpIpVersion ipVersions,
/// </summary>
/// <param name="host">The host to query</param>
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
ipads = Dns.GetHostAddresses(host);
#endif
Client.Status.CachedHostIpads.Add(host, ipads);
Client.Status.CachedHostIpads[host] = ipads;
}

return ipads;
Expand All @@ -885,12 +881,7 @@ private IPAddress[] GetCachedHostAddresses(string host) {
/// <param name="host">The host to query</param>
/// <param name="ipad">The IP address to store in the cache</param>
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 };
}

/// <summary>
Expand Down Expand Up @@ -1021,18 +1012,14 @@ private bool ConnectHelper(IPAddress ipad, int port) {
/// <param name="host">The host to query</param>
/// <param name="token">The token that can be used to cancel the entire process</param>
private async Task<IPAddress[]> 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
ipads = await Dns.GetHostAddressesAsync(host);
#endif
Client.Status.CachedHostIpads.Add(host, ipads);
Client.Status.CachedHostIpads[host] = ipads;
}

return ipads;
Expand Down

0 comments on commit 3b3f4b1

Please sign in to comment.