Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate Lazy<string> from Address #5068

Merged
merged 1 commit into from Jun 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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