THRIFT-5210 further refactoring and performance optimizations
Client: netstd
Patch: Jens Geyer

This closes #2146
diff --git a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
index a00c5c1..28b7d29 100644
--- a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
@@ -38,9 +38,6 @@
         // The value of 128 is arbitrarily chosen, the required minimum size must be sizeof(long)
         private byte[] PreAllocatedBuffer = new byte[128];
 
-        private static readonly TStruct AnonymousStruct = new TStruct(string.Empty);
-        private static readonly TField StopField = new TField() { Type = TType.Stop };
-
         public TBinaryProtocol(TTransport trans)
             : this(trans, false, true)
         {
@@ -55,10 +52,7 @@
 
         public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             if (StrictWrite)
             {
@@ -75,65 +69,47 @@
             }
         }
 
-        public override async Task WriteMessageEndAsync(CancellationToken cancellationToken)
+        public override Task WriteMessageEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
-        public override async Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken)
+        public override Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
-        public override async Task WriteStructEndAsync(CancellationToken cancellationToken)
+        public override Task WriteStructEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
-
+            cancellationToken.ThrowIfCancellationRequested();
             await WriteByteAsync((sbyte) field.Type, cancellationToken);
             await WriteI16Async(field.ID, cancellationToken);
         }
 
-        public override async Task WriteFieldEndAsync(CancellationToken cancellationToken)
+        public override Task WriteFieldEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async Task WriteFieldStopAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await WriteByteAsync((sbyte) TType.Stop, cancellationToken);
         }
 
         public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             PreAllocatedBuffer[0] = (byte)map.KeyType;
             PreAllocatedBuffer[1] = (byte)map.ValueType;
@@ -142,69 +118,47 @@
             await WriteI32Async(map.Count, cancellationToken);
         }
 
-        public override async Task WriteMapEndAsync(CancellationToken cancellationToken)
-
+        public override Task WriteMapEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
-
+            cancellationToken.ThrowIfCancellationRequested();
             await WriteByteAsync((sbyte) list.ElementType, cancellationToken);
             await WriteI32Async(list.Count, cancellationToken);
         }
 
-        public override async Task WriteListEndAsync(CancellationToken cancellationToken)
+        public override Task WriteListEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
-
+            cancellationToken.ThrowIfCancellationRequested();
             await WriteByteAsync((sbyte) set.ElementType, cancellationToken);
             await WriteI32Async(set.Count, cancellationToken);
         }
 
-        public override async Task WriteSetEndAsync(CancellationToken cancellationToken)
+        public override Task WriteSetEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async Task WriteBoolAsync(bool b, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
-
+            cancellationToken.ThrowIfCancellationRequested();
             await WriteByteAsync(b ? (sbyte) 1 : (sbyte) 0, cancellationToken);
         }
 
         public override async Task WriteByteAsync(sbyte b, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             PreAllocatedBuffer[0] = (byte)b;
 
@@ -212,10 +166,8 @@
         }
         public override async Task WriteI16Async(short i16, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+
             BinaryPrimitives.WriteInt16BigEndian(PreAllocatedBuffer, i16);
 
             await Trans.WriteAsync(PreAllocatedBuffer, 0, 2, cancellationToken);
@@ -223,10 +175,7 @@
 
         public override async Task WriteI32Async(int i32, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             BinaryPrimitives.WriteInt32BigEndian(PreAllocatedBuffer, i32);
 
@@ -236,10 +185,7 @@
       
         public override async Task WriteI64Async(long i64, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             BinaryPrimitives.WriteInt64BigEndian(PreAllocatedBuffer, i64);
 
@@ -248,10 +194,7 @@
 
         public override async Task WriteDoubleAsync(double d, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await WriteI64Async(BitConverter.DoubleToInt64Bits(d), cancellationToken);
         }
@@ -259,10 +202,7 @@
 
         public override async Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return;
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await WriteI32Async(bytes.Length, cancellationToken);
             await Trans.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
@@ -270,10 +210,7 @@
 
         public override async ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<TMessage>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             var message = new TMessage();
             var size = await ReadI32Async(cancellationToken);
@@ -303,39 +240,27 @@
             return message;
         }
 
-        public override async Task ReadMessageEndAsync(CancellationToken cancellationToken)
+        public override Task ReadMessageEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
-        public override async ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
+        public override ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
-
-            return AnonymousStruct;
+            cancellationToken.ThrowIfCancellationRequested();
+            return new ValueTask<TStruct>(AnonymousStruct);
         }
 
-        public override async Task ReadStructEndAsync(CancellationToken cancellationToken)
+        public override Task ReadStructEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<TField>(cancellationToken);
-            }
-
+            cancellationToken.ThrowIfCancellationRequested();
 
             var type = (TType)await ReadByteAsync(cancellationToken);
             if (type == TType.Stop)
@@ -349,21 +274,16 @@
             };
         }
 
-        public override async Task ReadFieldEndAsync(CancellationToken cancellationToken)
+        public override Task ReadFieldEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<TMap>(cancellationToken);
-            }
-
+            cancellationToken.ThrowIfCancellationRequested();
+ 
             var map = new TMap
             {
                 KeyType = (TType) await ReadByteAsync(cancellationToken),
@@ -374,20 +294,15 @@
             return map;
         }
 
-        public override async Task ReadMapEndAsync(CancellationToken cancellationToken)
+        public override Task ReadMapEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<TList>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             var list = new TList
             {
@@ -398,20 +313,15 @@
             return list;
         }
 
-        public override async Task ReadListEndAsync(CancellationToken cancellationToken)
+        public override Task ReadListEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<TSet>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             var set = new TSet
             {
@@ -422,30 +332,22 @@
             return set;
         }
 
-        public override async Task ReadSetEndAsync(CancellationToken cancellationToken)
+        public override Task ReadSetEndAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<bool>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             return await ReadByteAsync(cancellationToken) == 1;
         }
 
         public override async ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<sbyte>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 1, cancellationToken);
             return (sbyte)PreAllocatedBuffer[0];
@@ -453,10 +355,7 @@
 
         public override async ValueTask<short> ReadI16Async(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<short>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 2, cancellationToken);
             var result = BinaryPrimitives.ReadInt16BigEndian(PreAllocatedBuffer);
@@ -465,10 +364,7 @@
 
         public override async ValueTask<int> ReadI32Async(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<int>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 4, cancellationToken);
 
@@ -479,10 +375,7 @@
 
         public override async ValueTask<long> ReadI64Async(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<long>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             await Trans.ReadAllAsync(PreAllocatedBuffer, 0, 8, cancellationToken);
             return BinaryPrimitives.ReadInt64BigEndian(PreAllocatedBuffer);
@@ -490,10 +383,7 @@
 
         public override async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<double>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             var d = await ReadI64Async(cancellationToken);
             return BitConverter.Int64BitsToDouble(d);
@@ -501,10 +391,7 @@
 
         public override async ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<byte[]>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             var size = await ReadI32Async(cancellationToken);
             Transport.CheckReadBytesAvailable(size);
@@ -515,10 +402,7 @@
 
         public override async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                return await Task.FromCanceled<string>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             var size = await ReadI32Async(cancellationToken);
             return size > 0 ? await ReadStringBodyAsync(size, cancellationToken) : string.Empty;
@@ -526,10 +410,7 @@
 
         private async ValueTask<string> ReadStringBodyAsync(int size, CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled<string>(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
 
             if (size <= PreAllocatedBuffer.Length)
             {