Skip to content

Commit

Permalink
Remove usage of Linq in Base62 encoding
Browse files Browse the repository at this point in the history
Should be faster and cause fewer allocations
  • Loading branch information
kzu authored Jan 22, 2024
1 parent ef4f5ef commit 17d370a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions System/Base62.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#endregion

#nullable enable
using System.Linq;
using System.Numerics;
using System.Text;

Expand Down Expand Up @@ -53,7 +52,11 @@ public static string Encode(BigInteger value)
value /= 62;
}

return new string(sb.ToString().Reverse().ToArray());
// Reverse the string, since we're building it backwards,
var chars = sb.ToString().ToCharArray();
Array.Reverse(chars);

return new string(chars);
}

/// <summary>
Expand All @@ -78,4 +81,4 @@ public static BigInteger Decode(string value)
_ => throw new ArgumentException($"Cannot decode char '{c}' from base 62.", nameof(c)),
};
}
}
}

0 comments on commit 17d370a

Please sign in to comment.