You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2021/01/14 12:15:58 UTC

[avro] branch branch-1.10 updated: AVRO-2983: Set Span length of ArrayPool Rent buffer (#1013)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.10
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.10 by this push:
     new 266ba6e  AVRO-2983: Set Span length of ArrayPool Rent buffer (#1013)
266ba6e is described below

commit 266ba6e7afa8f67a11eed747a9688c8058e67b34
Author: Brian Davidson <br...@gmail.com>
AuthorDate: Thu Jan 14 07:08:15 2021 -0500

    AVRO-2983: Set Span length of ArrayPool Rent buffer (#1013)
    
    ArrayPool<T>.Rent(Int32) returns a buffer that is at least
    minimumLength in length, but can be more.  The span that is used is
    read into until it is empty. When this buffer returned from ArrayPool
    is not exactly the same as the requested minimumLength this causes an
    Avro.AvroException : End of stream reached
---
 .../main/IO/BinaryDecoder.notnetstandard2.0.cs     |  2 +-
 lang/csharp/src/apache/test/Avro.test.csproj       |  1 +
 lang/csharp/src/apache/test/IO/BinaryCodecTests.cs | 22 +++++++++++++++++++---
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/lang/csharp/src/apache/main/IO/BinaryDecoder.notnetstandard2.0.cs b/lang/csharp/src/apache/main/IO/BinaryDecoder.notnetstandard2.0.cs
index 206fe86..17bd841 100644
--- a/lang/csharp/src/apache/main/IO/BinaryDecoder.notnetstandard2.0.cs
+++ b/lang/csharp/src/apache/main/IO/BinaryDecoder.notnetstandard2.0.cs
@@ -68,7 +68,7 @@ namespace Avro.IO
             int length = ReadInt();
             Span<byte> buffer = length <= StackallocThreshold ?
                 stackalloc byte[length] :
-                (bufferArray = ArrayPool<byte>.Shared.Rent(length));
+                (bufferArray = ArrayPool<byte>.Shared.Rent(length)).AsSpan(0, length);
 
             Read(buffer);
 
diff --git a/lang/csharp/src/apache/test/Avro.test.csproj b/lang/csharp/src/apache/test/Avro.test.csproj
index 15054ac..95744b6 100644
--- a/lang/csharp/src/apache/test/Avro.test.csproj
+++ b/lang/csharp/src/apache/test/Avro.test.csproj
@@ -44,6 +44,7 @@
 
   <ItemGroup>
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
+    <PackageReference Include="System.Buffers" Version="4.5.1" />
   </ItemGroup>
 
   <ItemGroup>
diff --git a/lang/csharp/src/apache/test/IO/BinaryCodecTests.cs b/lang/csharp/src/apache/test/IO/BinaryCodecTests.cs
index d3aa69d..d5c0b13 100644
--- a/lang/csharp/src/apache/test/IO/BinaryCodecTests.cs
+++ b/lang/csharp/src/apache/test/IO/BinaryCodecTests.cs
@@ -16,11 +16,10 @@
  * limitations under the License.
  */
 using System;
-using System.Collections.Generic;
-using System.Text;
+using System.Buffers;
 using NUnit.Framework;
 using System.IO;
-
+using System.Linq;
 using Avro.IO;
 
 namespace Avro.Test
@@ -214,6 +213,23 @@ namespace Avro.Test
             TestSkip(n, (Decoder d) => d.SkipString(), (Encoder e, string t) => e.WriteString(t), overhead + n.Length);
         }
 
+#if NETCOREAPP3_1
+        [Test]
+        public void TestLargeString()
+        {
+            // Create a 16KB buffer in the Array Pool
+            var largeBufferToSeedPool = ArrayPool<byte>.Shared.Rent(2 << 14);
+            ArrayPool<byte>.Shared.Return(largeBufferToSeedPool);
+
+            // Create a slightly less than 16KB buffer, which will use the 16KB buffer in the pool
+            var n = string.Concat(Enumerable.Repeat("1234567890", 1600));
+            var overhead = 3;
+
+            TestRead(n, (Decoder d) => d.ReadString(), (Encoder e, string t) => e.WriteString(t), overhead + n.Length);
+            TestSkip(n, (Decoder d) => d.SkipString(), (Encoder e, string t) => e.WriteString(t), overhead + n.Length);
+        }
+#endif
+
         [TestCase(0, 1)]
         [TestCase(1, 1)]
         [TestCase(64, 2)]