diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt index cde266ae92e..e73e49ea93e 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt @@ -4810,6 +4810,7 @@ namespace Akka.Util { public const string Base64Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+~"; public static string Base64Encode(this long value) { } + [System.ObsoleteAttribute("Do not use. Pass in prefix as a string instead.")] public static System.Text.StringBuilder Base64Encode(this long value, System.Text.StringBuilder sb) { } public static string Base64Encode(this string s) { } } diff --git a/src/core/Akka/Actor/ActorCell.Children.cs b/src/core/Akka/Actor/ActorCell.Children.cs index 9c69a74df31..db426b09e5f 100644 --- a/src/core/Akka/Actor/ActorCell.Children.cs +++ b/src/core/Akka/Actor/ActorCell.Children.cs @@ -135,8 +135,7 @@ private IActorRef ActorOf(Props props, string name, bool isAsync, bool isSystemS private string GetRandomActorName(string prefix = "$") { var id = Interlocked.Increment(ref _nextRandomNameDoNotCallMeDirectly); - var sb = new StringBuilder(prefix); - return id.Base64Encode(sb).ToString(); + return id.Base64Encode(prefix); } /// diff --git a/src/core/Akka/Util/Base64Encoding.cs b/src/core/Akka/Util/Base64Encoding.cs index 1adc770a1a1..d702281f7ec 100644 --- a/src/core/Akka/Util/Base64Encoding.cs +++ b/src/core/Akka/Util/Base64Encoding.cs @@ -5,6 +5,7 @@ // //----------------------------------------------------------------------- +using System; using System.Text; namespace Akka.Util @@ -24,7 +25,32 @@ public static class Base64Encoding /// /// TBD /// TBD - public static string Base64Encode(this long value) => Base64Encode(value, new StringBuilder()).ToString(); + public static string Base64Encode(this long value) + { + return Base64Encode(value, ""); + } + + internal static string Base64Encode(this long value, string prefix) + { + // 11 is the number of characters it takes to represent long.MaxValue + // so we will never need a larger size for encoding longs + Span sb = stackalloc char[11 + prefix?.Length ?? 0]; + var spanIndex = 0; + if (!string.IsNullOrWhiteSpace(prefix) && prefix.Length > 0) + { + prefix.AsSpan().CopyTo(sb); + spanIndex = prefix.Length; + } + + var next = value; + do + { + var index = (int)(next & 63); + sb[spanIndex++] = Base64Chars[index]; + next = next >> 6; + } while (next != 0); + return sb.Slice(0, spanIndex).ToString(); + } /// /// TBD @@ -32,6 +58,7 @@ public static class Base64Encoding /// TBD /// TBD /// TBD + [Obsolete("Do not use. Pass in prefix as a string instead.")] public static StringBuilder Base64Encode(this long value, StringBuilder sb) { var next = value;