aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Stephan Zehetner <stephan.zehetner@gmail.com>2018-09-14 14:35:45 +0200
committerGravatar Stephan Zehetner <stephan.zehetner@gmail.com>2018-09-14 14:35:45 +0200
commit3729329a3fe9fdb37edd913734f1fbb35f3e3d77 (patch)
treede81ab0d28145f901ed8345ac5d34fae1b80006f
parent1b6c3a01cf292159cd4208e8fbe64f84c8b90a8b (diff)
avoid byte[] allocation when reading empty strings from native memory
-rw-r--r--src/csharp/Grpc.Core/Internal/MarshalUtils.cs3
1 files changed, 3 insertions, 0 deletions
diff --git a/src/csharp/Grpc.Core/Internal/MarshalUtils.cs b/src/csharp/Grpc.Core/Internal/MarshalUtils.cs
index 3f9605bfdd..73b7a2ef95 100644
--- a/src/csharp/Grpc.Core/Internal/MarshalUtils.cs
+++ b/src/csharp/Grpc.Core/Internal/MarshalUtils.cs
@@ -35,6 +35,9 @@ namespace Grpc.Core.Internal
/// </summary>
public static string PtrToStringUTF8(IntPtr ptr, int len)
{
+ if (len == 0)
+ return "";
+
var bytes = new byte[len];
Marshal.Copy(ptr, bytes, 0, len);
return EncodingUTF8.GetString(bytes);