Skip to content

Commit 566dbe3

Browse files
Removed StringBuilder allocation from Base64Encoding (#5051)
* Removed `StringBuilder` allocation from `Base64Encoding` * Update Base64Encoding.cs * implemented feedback
1 parent c21289e commit 566dbe3

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,6 +4810,7 @@ namespace Akka.Util
48104810
{
48114811
public const string Base64Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+~";
48124812
public static string Base64Encode(this long value) { }
4813+
[System.ObsoleteAttribute("Do not use. Pass in prefix as a string instead.")]
48134814
public static System.Text.StringBuilder Base64Encode(this long value, System.Text.StringBuilder sb) { }
48144815
public static string Base64Encode(this string s) { }
48154816
}

src/core/Akka/Actor/ActorCell.Children.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ private IActorRef ActorOf(Props props, string name, bool isAsync, bool isSystemS
135135
private string GetRandomActorName(string prefix = "$")
136136
{
137137
var id = Interlocked.Increment(ref _nextRandomNameDoNotCallMeDirectly);
138-
var sb = new StringBuilder(prefix);
139-
return id.Base64Encode(sb).ToString();
138+
return id.Base64Encode(prefix);
140139
}
141140

142141
/// <summary>

src/core/Akka/Util/Base64Encoding.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// </copyright>
66
//-----------------------------------------------------------------------
77

8+
using System;
89
using System.Text;
910

1011
namespace Akka.Util
@@ -24,14 +25,40 @@ public static class Base64Encoding
2425
/// </summary>
2526
/// <param name="value">TBD</param>
2627
/// <returns>TBD</returns>
27-
public static string Base64Encode(this long value) => Base64Encode(value, new StringBuilder()).ToString();
28+
public static string Base64Encode(this long value)
29+
{
30+
return Base64Encode(value, "");
31+
}
32+
33+
internal static string Base64Encode(this long value, string prefix)
34+
{
35+
// 11 is the number of characters it takes to represent long.MaxValue
36+
// so we will never need a larger size for encoding longs
37+
Span<char> sb = stackalloc char[11 + prefix?.Length ?? 0];
38+
var spanIndex = 0;
39+
if (!string.IsNullOrWhiteSpace(prefix) && prefix.Length > 0)
40+
{
41+
prefix.AsSpan().CopyTo(sb);
42+
spanIndex = prefix.Length;
43+
}
44+
45+
var next = value;
46+
do
47+
{
48+
var index = (int)(next & 63);
49+
sb[spanIndex++] = Base64Chars[index];
50+
next = next >> 6;
51+
} while (next != 0);
52+
return sb.Slice(0, spanIndex).ToString();
53+
}
2854

2955
/// <summary>
3056
/// TBD
3157
/// </summary>
3258
/// <param name="value">TBD</param>
3359
/// <param name="sb">TBD</param>
3460
/// <returns>TBD</returns>
61+
[Obsolete("Do not use. Pass in prefix as a string instead.")]
3562
public static StringBuilder Base64Encode(this long value, StringBuilder sb)
3663
{
3764
var next = value;

0 commit comments

Comments
 (0)