Skip to content

Commit

Permalink
eliminate Lazy<string> from Address (#5068)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Jun 3, 2021
1 parent 4bd3ef9 commit b1a9aa6
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/core/Akka/Actor/Address.cs
Expand Up @@ -59,7 +59,7 @@ public int Compare(Address x, Address y)
/// </summary>
public static readonly Address AllSystems = new Address("akka", "all-systems");

private readonly Lazy<string> _toString;
private string _toString;
private readonly string _host;
private readonly int? _port;
private readonly string _system;
Expand All @@ -78,7 +78,7 @@ public Address(string protocol, string system, string host = null, int? port = n
_system = system;
_host = host?.ToLowerInvariant();
_port = port;
_toString = CreateLazyToString();
_toString = null;
}

/// <summary>
Expand Down Expand Up @@ -114,19 +114,14 @@ public Address(string protocol, string system, string host = null, int? port = n
/// </summary>
public bool HasGlobalScope => !string.IsNullOrEmpty(Host);

private Lazy<string> CreateLazyToString()
private static string CreateLazyToString(Address addr)
{
return new Lazy<string>(() =>
{
var sb = new StringBuilder();
sb.AppendFormat("{0}://{1}", Protocol, System);
if (!string.IsNullOrWhiteSpace(Host))
sb.AppendFormat("@{0}", Host);
if (Port.HasValue)
sb.AppendFormat(":{0}", Port.Value);
return sb.ToString();
}, true);
if (!string.IsNullOrWhiteSpace(addr.Host) && addr.Port.HasValue)
return $"{addr.Protocol}://{addr.System}@{addr.Host}:{addr.Port}";
if (!string.IsNullOrWhiteSpace(addr.Host)) // host, but no port - rare case
return $"{addr.Protocol}://{addr.System}@{addr.Host}";

return $"{addr.Protocol}://{addr.System}";
}

/// <summary>
Expand All @@ -140,7 +135,15 @@ public int CompareTo(Address other)
}

/// <inheritdoc/>
public override string ToString() => _toString.Value;
public override string ToString()
{
if (_toString == null)
{
_toString = CreateLazyToString(this);
}

return _toString;
}

/// <inheritdoc/>
public bool Equals(Address other)
Expand Down Expand Up @@ -279,7 +282,7 @@ public static Address Parse(string address)
if (string.IsNullOrEmpty(uri.UserInfo))
{
var systemName = uri.Host;

return new Address(protocol, systemName);
}
else
Expand Down

0 comments on commit b1a9aa6

Please sign in to comment.