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

This closes #2146
diff --git a/lib/netstd/Thrift/Protocol/TProtocol.cs b/lib/netstd/Thrift/Protocol/TProtocol.cs
index 5275c9c..5b1bec1 100644
--- a/lib/netstd/Thrift/Protocol/TProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TProtocol.cs
@@ -32,6 +32,10 @@
 
         protected TTransport Trans;
 
+        protected static readonly TStruct AnonymousStruct = new TStruct(string.Empty);
+        protected static readonly TField StopField = new TField() { Type = TType.Stop };
+
+
         protected TProtocol(TTransport trans)
         {
             Trans = trans;
@@ -98,299 +102,94 @@
         public abstract int GetMinSerializedSize(TType type);
 
 
-        public virtual async Task WriteMessageBeginAsync(TMessage message)
-        {
-            await WriteMessageBeginAsync(message, CancellationToken.None);
-        }
+        public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken = default);
 
-        public abstract Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken);
+        public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteMessageEndAsync()
-        {
-            await WriteMessageEndAsync(CancellationToken.None);
-        }
+        public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken = default);
 
-        public abstract Task WriteMessageEndAsync(CancellationToken cancellationToken);
+        public abstract Task WriteStructEndAsync(CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteStructBeginAsync(TStruct @struct)
-        {
-            await WriteStructBeginAsync(@struct, CancellationToken.None);
-        }
+        public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken = default);
+                
+        public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken = default);
+                
+        public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task WriteStructBeginAsync(TStruct @struct, CancellationToken cancellationToken);
+        public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteStructEndAsync()
-        {
-            await WriteStructEndAsync(CancellationToken.None);
-        }
+        public abstract Task WriteMapEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task WriteStructEndAsync(CancellationToken cancellationToken);
+        public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteFieldBeginAsync(TField field)
-        {
-            await WriteFieldBeginAsync(field, CancellationToken.None);
-        }
+        public abstract Task WriteListEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken);
+        public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteFieldEndAsync()
-        {
-            await WriteFieldEndAsync(CancellationToken.None);
-        }
+        public abstract Task WriteSetEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task WriteFieldEndAsync(CancellationToken cancellationToken);
+        public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteFieldStopAsync()
-        {
-            await WriteFieldStopAsync(CancellationToken.None);
-        }
+        public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken = default);
 
-        public abstract Task WriteFieldStopAsync(CancellationToken cancellationToken);
+        public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteMapBeginAsync(TMap map)
-        {
-            await WriteMapBeginAsync(map, CancellationToken.None);
-        }
+        public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken = default);
 
-        public abstract Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken);
+        public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken = default);
 
-        public virtual async Task WriteMapEndAsync()
-        {
-            await WriteMapEndAsync(CancellationToken.None);
-        }
+        public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken = default);
 
-        public abstract Task WriteMapEndAsync(CancellationToken cancellationToken);
-
-        public virtual async Task WriteListBeginAsync(TList list)
-        {
-            await WriteListBeginAsync(list, CancellationToken.None);
-        }
-
-        public abstract Task WriteListBeginAsync(TList list, CancellationToken cancellationToken);
-
-        public virtual async Task WriteListEndAsync()
-        {
-            await WriteListEndAsync(CancellationToken.None);
-        }
-
-        public abstract Task WriteListEndAsync(CancellationToken cancellationToken);
-
-        public virtual async Task WriteSetBeginAsync(TSet set)
-        {
-            await WriteSetBeginAsync(set, CancellationToken.None);
-        }
-
-        public abstract Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken);
-
-        public virtual async Task WriteSetEndAsync()
-        {
-            await WriteSetEndAsync(CancellationToken.None);
-        }
-
-        public abstract Task WriteSetEndAsync(CancellationToken cancellationToken);
-
-        public virtual async Task WriteBoolAsync(bool b)
-        {
-            await WriteBoolAsync(b, CancellationToken.None);
-        }
-
-        public abstract Task WriteBoolAsync(bool b, CancellationToken cancellationToken);
-
-        public virtual async Task WriteByteAsync(sbyte b)
-        {
-            await WriteByteAsync(b, CancellationToken.None);
-        }
-
-        public abstract Task WriteByteAsync(sbyte b, CancellationToken cancellationToken);
-
-        public virtual async Task WriteI16Async(short i16)
-        {
-            await WriteI16Async(i16, CancellationToken.None);
-        }
-
-        public abstract Task WriteI16Async(short i16, CancellationToken cancellationToken);
-
-        public virtual async Task WriteI32Async(int i32)
-        {
-            await WriteI32Async(i32, CancellationToken.None);
-        }
-
-        public abstract Task WriteI32Async(int i32, CancellationToken cancellationToken);
-
-        public virtual async Task WriteI64Async(long i64)
-        {
-            await WriteI64Async(i64, CancellationToken.None);
-        }
-
-        public abstract Task WriteI64Async(long i64, CancellationToken cancellationToken);
-
-        public virtual async Task WriteDoubleAsync(double d)
-        {
-            await WriteDoubleAsync(d, CancellationToken.None);
-        }
-
-        public abstract Task WriteDoubleAsync(double d, CancellationToken cancellationToken);
-
-        public virtual async Task WriteStringAsync(string s)
-        {
-            await WriteStringAsync(s, CancellationToken.None);
-        }
-
-        public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken)
+        public virtual async Task WriteStringAsync(string s, CancellationToken cancellationToken = default)
         {
             var bytes = Encoding.UTF8.GetBytes(s);
             await WriteBinaryAsync(bytes, cancellationToken);
         }
 
-        public virtual async Task WriteBinaryAsync(byte[] bytes)
-        {
-            await WriteBinaryAsync(bytes, CancellationToken.None);
-        }
+        public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken = default);
 
-        public abstract Task WriteBinaryAsync(byte[] bytes, CancellationToken cancellationToken);
+        public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken = default);
 
-        public virtual async ValueTask<TMessage> ReadMessageBeginAsync()
-        {
-            return await ReadMessageBeginAsync(CancellationToken.None);
-        }
+        public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract ValueTask<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken = default);
 
-        public virtual async Task ReadMessageEndAsync()
-        {
-            await ReadMessageEndAsync(CancellationToken.None);
-        }
+        public abstract Task ReadStructEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task ReadMessageEndAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken = default);
 
-        public virtual async ValueTask<TStruct> ReadStructBeginAsync()
-        {
-            return await ReadStructBeginAsync(CancellationToken.None);
-        }
+        public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken = default);
 
-        public virtual async Task ReadStructEndAsync()
-        {
-            await ReadStructEndAsync(CancellationToken.None);
-        }
+        public abstract Task ReadMapEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task ReadStructEndAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken = default);
 
-        public virtual async ValueTask<TField> ReadFieldBeginAsync()
-        {
-            return await ReadFieldBeginAsync(CancellationToken.None);
-        }
+        public abstract Task ReadListEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken = default);
 
-        public virtual async Task ReadFieldEndAsync()
-        {
-            await ReadFieldEndAsync(CancellationToken.None);
-        }
+        public abstract Task ReadSetEndAsync(CancellationToken cancellationToken = default);
 
-        public abstract Task ReadFieldEndAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken = default);
 
-        public virtual async ValueTask<TMap> ReadMapBeginAsync()
-        {
-            return await ReadMapBeginAsync(CancellationToken.None);
-        }
+        public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken = default);
 
-        public abstract ValueTask<TMap> ReadMapBeginAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken = default);
 
-        public virtual async Task ReadMapEndAsync()
-        {
-            await ReadMapEndAsync(CancellationToken.None);
-        }
+        public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken = default);
 
-        public abstract Task ReadMapEndAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken = default);
 
-        public virtual async ValueTask<TList> ReadListBeginAsync()
-        {
-            return await ReadListBeginAsync(CancellationToken.None);
-        }
+        public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default);
 
-        public abstract ValueTask<TList> ReadListBeginAsync(CancellationToken cancellationToken);
-
-        public virtual async Task ReadListEndAsync()
-        {
-            await ReadListEndAsync(CancellationToken.None);
-        }
-
-        public abstract Task ReadListEndAsync(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<TSet> ReadSetBeginAsync()
-        {
-            return await ReadSetBeginAsync(CancellationToken.None);
-        }
-
-        public abstract ValueTask<TSet> ReadSetBeginAsync(CancellationToken cancellationToken);
-
-        public virtual async Task ReadSetEndAsync()
-        {
-            await ReadSetEndAsync(CancellationToken.None);
-        }
-
-        public abstract Task ReadSetEndAsync(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<bool> ReadBoolAsync()
-        {
-            return await ReadBoolAsync(CancellationToken.None);
-        }
-
-        public abstract ValueTask<bool> ReadBoolAsync(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<sbyte> ReadByteAsync()
-        {
-            return await ReadByteAsync(CancellationToken.None);
-        }
-
-        public abstract ValueTask<sbyte> ReadByteAsync(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<short> ReadI16Async()
-        {
-            return await ReadI16Async(CancellationToken.None);
-        }
-
-        public abstract ValueTask<short> ReadI16Async(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<int> ReadI32Async()
-        {
-            return await ReadI32Async(CancellationToken.None);
-        }
-
-        public abstract ValueTask<int> ReadI32Async(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<long> ReadI64Async()
-        {
-            return await ReadI64Async(CancellationToken.None);
-        }
-
-        public abstract ValueTask<long> ReadI64Async(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<double> ReadDoubleAsync()
-        {
-            return await ReadDoubleAsync(CancellationToken.None);
-        }
-
-        public abstract ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken);
-
-        public virtual async ValueTask<string> ReadStringAsync()
-        {
-            return await ReadStringAsync(CancellationToken.None);
-        }
-
-        public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken)
+        public virtual async ValueTask<string> ReadStringAsync(CancellationToken cancellationToken = default)
         {
             var buf = await ReadBinaryAsync(cancellationToken);
             return Encoding.UTF8.GetString(buf, 0, buf.Length);
         }
 
-        public virtual async ValueTask<byte[]> ReadBinaryAsync()
-        {
-            return await ReadBinaryAsync(CancellationToken.None);
-        }
-
-        public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken);
+        public abstract ValueTask<byte[]> ReadBinaryAsync(CancellationToken cancellationToken = default);
     }
 }