Skip to content

Commit 6696b97

Browse files
Reduce allocations in ParseSize and PrettySize utility methods (#1404)
Co-authored-by: Badrish Chandramouli <[email protected]>
1 parent ec9cace commit 6696b97

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

libs/client/Utility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static class Utility
2222
/// <returns>The number</returns>
2323
public static long ParseSize(string value)
2424
{
25-
char[] suffix = ['k', 'm', 'g', 't', 'p'];
25+
ReadOnlySpan<char> suffix = ['k', 'm', 'g', 't', 'p'];
2626
long result = 0;
2727
foreach (char c in value)
2828
{
@@ -83,7 +83,7 @@ public static long PreviousPowerOf2(long v)
8383
/// <returns></returns>
8484
internal static string PrettySize(long value)
8585
{
86-
char[] suffix = ['K', 'M', 'G', 'T', 'P'];
86+
ReadOnlySpan<char> suffix = ['K', 'M', 'G', 'T', 'P'];
8787
double v = value;
8888
int exp = 0;
8989
while (v - Math.Floor(v) > 0)

libs/server/Servers/ServerOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void GetSettings<TKey, TValue>()
239239
/// <returns></returns>
240240
public static long ParseSize(string value, out int bytesRead)
241241
{
242-
char[] suffix = ['k', 'm', 'g', 't', 'p'];
242+
ReadOnlySpan<char> suffix = ['k', 'm', 'g', 't', 'p'];
243243
long result = 0;
244244
bytesRead = 0;
245245
for (var i = 0; i < value.Length; i++)
@@ -289,7 +289,7 @@ public static bool TryParseSize(string value, out long size)
289289
/// <returns></returns>
290290
internal static string PrettySize(long value)
291291
{
292-
char[] suffix = ['k', 'm', 'g', 't', 'p'];
292+
ReadOnlySpan<char> suffix = ['k', 'm', 'g', 't', 'p'];
293293
double v = value;
294294
int exp = 0;
295295
while (v - Math.Floor(v) > 0)

libs/storage/Tsavorite/cs/src/core/Utilities/Utility.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static class Utility
3737
/// <returns>The number</returns>
3838
public static long ParseSize(string value)
3939
{
40-
char[] suffix = ['k', 'm', 'g', 't', 'p'];
40+
ReadOnlySpan<char> suffix = ['k', 'm', 'g', 't', 'p'];
4141
long result = 0;
4242
foreach (char c in value)
4343
{
@@ -97,7 +97,7 @@ internal static long PreviousPowerOf2(long v)
9797
/// <returns></returns>
9898
internal static string PrettySize(long value)
9999
{
100-
char[] suffix = ['K', 'M', 'G', 'T', 'P'];
100+
ReadOnlySpan<char> suffix = ['K', 'M', 'G', 'T', 'P'];
101101
double v = value;
102102
int exp = 0;
103103
while (v - Math.Floor(v) > 0)
@@ -345,7 +345,7 @@ private static async Task<T> SlowWithCancellationAsync<T>(Task<T> task, Cancella
345345
}
346346

347347
/// <summary>
348-
///
348+
///
349349
/// </summary>
350350
/// <returns></returns>
351351
public static ulong GetCurrentMilliseconds()

0 commit comments

Comments
 (0)