Skip to content

Commit 6e760d5

Browse files
authored
Optimize memory usage and improve code readability (#3425)
Added System.Buffers to StringUtils.cs. Refactored FromMemoryStream to use MemoryStream.TryGetBuffer and ArrayPool<byte> for efficient memory handling. Modified FromString method signature without changing implementation. Made minor formatting changes in FromValueTypeList and headerListEntry processing logic to enhance readability.
1 parent 886dd05 commit 6e760d5

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

sdk/src/Core/Amazon.Runtime/Internal/Util/StringUtils.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Amazon.Util;
2222
using System.Linq;
2323
using System.Diagnostics.CodeAnalysis;
24+
using System.Buffers;
2425

2526
namespace Amazon.Runtime.Internal.Util
2627
{
@@ -32,7 +33,7 @@ public static class StringUtils
3233
private static readonly Encoding UTF_8 = Encoding.UTF8;
3334
private static readonly char[] rfc7230HeaderFieldValueDelimeters = "\"(),/:;<=>?@[\\]{}".ToCharArray();
3435

35-
public static string FromString(String value)
36+
public static string FromString(String value)
3637
{
3738
return value;
3839
}
@@ -49,14 +50,30 @@ public static string FromString(ConstantClass value)
4950

5051
public static string FromMemoryStream(MemoryStream value)
5152
{
52-
return Convert.ToBase64String(value.ToArray());
53+
if (value.TryGetBuffer(out var buffer))
54+
{
55+
return Convert.ToBase64String(buffer.Array, buffer.Offset, buffer.Count);
56+
}
57+
else
58+
{
59+
var array = ArrayPool<byte>.Shared.Rent((int)value.Length);
60+
try
61+
{
62+
value.Read(array, 0, (int)value.Length);
63+
return Convert.ToBase64String(array, 0, (int)value.Length);
64+
}
65+
finally
66+
{
67+
ArrayPool<byte>.Shared.Return(array);
68+
}
69+
}
5370
}
5471

5572
public static string FromInt(int value)
5673
{
5774
return value.ToString(CultureInfo.InvariantCulture);
5875
}
59-
76+
6077
public static string FromInt(int? value)
6178
{
6279
if (!value.HasValue)
@@ -348,7 +365,7 @@ public static string FromValueTypeList<T>(IEnumerable<T> values) where T : struc
348365
/// <param name="values">List of T</param>
349366
/// <returns>Header value representing the list of T</returns>
350367
[SuppressMessage("Microsoft.Globalization", "CA1308", Justification = "Value is not surfaced to user. Booleans have been lowercased by SDK precedent.")]
351-
public static string FromValueTypeList<T>(List<T> values) where T : struct
368+
public static string FromValueTypeList<T>(List<T> values) where T : struct
352369
{
353370
// ToString() on boolean types automatically Pascal Cases. Xml-based protocols
354371
// are case sensitive and accept "true" and "false" as the valid set of booleans.
@@ -404,7 +421,7 @@ private static string EscapeHeaderListEntry(string headerListEntry)
404421
if (headerListEntry.IndexOfAny(rfc7230HeaderFieldValueDelimeters) != -1)
405422
{
406423
return $"\"{headerListEntry.Replace("\"", "\\\"")}\"";
407-
}
424+
}
408425

409426
return headerListEntry;
410427
}

0 commit comments

Comments
 (0)