CurtHagenlocher commented on code in PR #329:
URL: https://github.com/apache/arrow-dotnet/pull/329#discussion_r3141366187


##########
src/Apache.Arrow.Scalars/Variant/VariantValueWriter.cs:
##########
@@ -314,160 +411,232 @@ public void WriteDecimal16(SqlDecimal value)
                 lo = (long)uLo;
             }
 
-            ms.WriteByte(scale);
-            WriteInt64LE(ms, lo);
-            WriteInt64LE(ms, hi);
+            buf.Append(scale);
+            buf.WriteInt64LE(lo);
+            buf.WriteInt64LE(hi);
         }
 
         /// <summary>Writes a string. Uses the short-string encoding when the 
UTF-8 byte length is ≤ 63.</summary>
         public void WriteString(string value)
         {
             if (value == null) throw new ArgumentNullException(nameof(value));
-            MemoryStream ms = BeforeWriteValue();
+            ref Buffer<byte> buf = ref BeforeWriteValue();
             int byteCount = Encoding.UTF8.GetByteCount(value);
             if (byteCount <= 63)
             {
-                
ms.WriteByte(VariantEncodingHelper.MakeShortStringHeader(byteCount));
+                
buf.Append(VariantEncodingHelper.MakeShortStringHeader(byteCount));
             }
             else
             {
-                
ms.WriteByte(VariantEncodingHelper.MakePrimitiveHeader(VariantPrimitiveType.String));
-                WriteInt32LE(ms, byteCount);
+                
buf.Append(VariantEncodingHelper.MakePrimitiveHeader(VariantPrimitiveType.String));
+                buf.WriteInt32LE(byteCount);
             }
 
-            // Encode UTF-8 directly into the MemoryStream's buffer.
-            int dataPos = (int)ms.Position;
-            int needed = dataPos + byteCount;
-            if (needed > ms.Length)
-            {
-                ms.SetLength(needed);
-            }
-            Encoding.UTF8.GetBytes(value, 0, value.Length, ms.GetBuffer(), 
dataPos);
-            ms.Position = needed;
+            // Encode UTF-8 directly into the buffer.
+            Span<byte> dest = buf.GetSpan(byteCount);
+#if NET8_0_OR_GREATER
+            Encoding.UTF8.GetBytes(value, dest);
+#else
+            Encoding.UTF8.GetBytes(value, 0, value.Length, buf.RawBuffer, 
buf.Length);
+#endif
+            buf.Advance(byteCount);
         }
 
         /// <summary>Writes a binary blob.</summary>
-        public void WriteBinary(byte[] data)
+        public void WriteBinary(ReadOnlySpan<byte> data)
         {
-            if (data == null) throw new ArgumentNullException(nameof(data));
-            MemoryStream ms = BeforeWriteValue();
-            
ms.WriteByte(VariantEncodingHelper.MakePrimitiveHeader(VariantPrimitiveType.Binary));
-            WriteInt32LE(ms, data.Length);
-            ms.Write(data, 0, data.Length);
+            ref Buffer<byte> buf = ref BeforeWriteValue();
+            
buf.Append(VariantEncodingHelper.MakePrimitiveHeader(VariantPrimitiveType.Binary));
+            buf.WriteInt32LE(data.Length);
+            buf.Append(data);
         }
 
         /// <summary>Writes a UUID.</summary>
         public void WriteUuid(Guid value)
         {
-            MemoryStream ms = BeforeWriteValue();
-            
ms.WriteByte(VariantEncodingHelper.MakePrimitiveHeader(VariantPrimitiveType.Uuid));
+            ref Buffer<byte> buf = ref BeforeWriteValue();
+            
buf.Append(VariantEncodingHelper.MakePrimitiveHeader(VariantPrimitiveType.Uuid));
+            Span<byte> raw = stackalloc byte[16];
 #if NET8_0_OR_GREATER
-            Span<byte> buf = stackalloc byte[16];
-            value.TryWriteBytes(buf, bigEndian: true, out int _);
-            ms.Write(buf);
+            value.TryWriteBytes(raw, bigEndian: true, out _);
 #else
-            byte[] native = value.ToByteArray();
             // Convert from .NET mixed-endian to big-endian (RFC 4122).
-            _scratch[0] = native[3]; _scratch[1] = native[2]; _scratch[2] = 
native[1]; _scratch[3] = native[0];
-            _scratch[4] = native[5]; _scratch[5] = native[4];
-            _scratch[6] = native[7]; _scratch[7] = native[6];
-            Buffer.BlockCopy(native, 8, _scratch, 8, 8);
-            ms.Write(_scratch, 0, 16);
+            byte[] native = value.ToByteArray();

Review Comment:
   Hmm... we have an unsafe helper for this somewhere



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to