Skip to content

Commit

Permalink
Removed StringBuilder allocation from Base64Encoding (#5051)
Browse files Browse the repository at this point in the history
* Removed `StringBuilder` allocation from `Base64Encoding`

* Update Base64Encoding.cs

* implemented feedback
  • Loading branch information
Aaronontheweb authored May 29, 2021
1 parent c21289e commit 566dbe3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/Akka/Actor/ActorCell.Children.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
29 changes: 28 additions & 1 deletion src/core/Akka/Util/Base64Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Text;

namespace Akka.Util
Expand All @@ -24,14 +25,40 @@ public static class Base64Encoding
/// </summary>
/// <param name="value">TBD</param>
/// <returns>TBD</returns>
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<char> 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();
}

/// <summary>
/// TBD
/// </summary>
/// <param name="value">TBD</param>
/// <param name="sb">TBD</param>
/// <returns>TBD</returns>
[Obsolete("Do not use. Pass in prefix as a string instead.")]
public static StringBuilder Base64Encode(this long value, StringBuilder sb)
{
var next = value;
Expand Down

0 comments on commit 566dbe3

Please sign in to comment.