Skip to content

Commit b1a9aa6

Browse files
eliminate Lazy<string> from Address (#5068)
1 parent 4bd3ef9 commit b1a9aa6

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/core/Akka/Actor/Address.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public int Compare(Address x, Address y)
5959
/// </summary>
6060
public static readonly Address AllSystems = new Address("akka", "all-systems");
6161

62-
private readonly Lazy<string> _toString;
62+
private string _toString;
6363
private readonly string _host;
6464
private readonly int? _port;
6565
private readonly string _system;
@@ -78,7 +78,7 @@ public Address(string protocol, string system, string host = null, int? port = n
7878
_system = system;
7979
_host = host?.ToLowerInvariant();
8080
_port = port;
81-
_toString = CreateLazyToString();
81+
_toString = null;
8282
}
8383

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

117-
private Lazy<string> CreateLazyToString()
117+
private static string CreateLazyToString(Address addr)
118118
{
119-
return new Lazy<string>(() =>
120-
{
121-
var sb = new StringBuilder();
122-
sb.AppendFormat("{0}://{1}", Protocol, System);
123-
if (!string.IsNullOrWhiteSpace(Host))
124-
sb.AppendFormat("@{0}", Host);
125-
if (Port.HasValue)
126-
sb.AppendFormat(":{0}", Port.Value);
127-
128-
return sb.ToString();
129-
}, true);
119+
if (!string.IsNullOrWhiteSpace(addr.Host) && addr.Port.HasValue)
120+
return $"{addr.Protocol}://{addr.System}@{addr.Host}:{addr.Port}";
121+
if (!string.IsNullOrWhiteSpace(addr.Host)) // host, but no port - rare case
122+
return $"{addr.Protocol}://{addr.System}@{addr.Host}";
123+
124+
return $"{addr.Protocol}://{addr.System}";
130125
}
131126

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

142137
/// <inheritdoc/>
143-
public override string ToString() => _toString.Value;
138+
public override string ToString()
139+
{
140+
if (_toString == null)
141+
{
142+
_toString = CreateLazyToString(this);
143+
}
144+
145+
return _toString;
146+
}
144147

145148
/// <inheritdoc/>
146149
public bool Equals(Address other)
@@ -279,7 +282,7 @@ public static Address Parse(string address)
279282
if (string.IsNullOrEmpty(uri.UserInfo))
280283
{
281284
var systemName = uri.Host;
282-
285+
283286
return new Address(protocol, systemName);
284287
}
285288
else

0 commit comments

Comments
 (0)