Browse Source

Performance (touchsocket): Optimize PipeOptions scheduling to Inline (#10408)

* 性能(Program.cs): 优化PipeOptions调度为Inline

将ReceivePipeOptions和SendPipeOptions中的readerScheduler与writerScheduler由ThreadPool调整为Inline,减少线程切换开销,提升管道操作性能,但需注意对异步场景的影响

* 优化(DateHeader,MyTcpSessionClientBase):规范HTTP头部格式

调整DateHeader前缀为"\r\nDate: ",修正prefixLength为8,确保符合HTTP标准格式;MyTcpSessionClientBase中Content-Length去除结尾CRLF,便于后续拼接和处理,减少硬编码CRLF
若汝棋茗 14 hours ago
parent
commit
fe17a488a2

+ 4 - 4
frameworks/CSharp/touchsocket/src/TouchSocketHttp/Program.cs

@@ -23,8 +23,8 @@ public class Program
 
                   options.ReceivePipeOptions = new PipeOptions(
                 pool: MemoryPool<byte>.Shared,
-                readerScheduler: PipeScheduler.ThreadPool,
-                writerScheduler: PipeScheduler.ThreadPool,
+                readerScheduler: PipeScheduler.Inline,
+                writerScheduler: PipeScheduler.Inline,
                 pauseWriterThreshold: 1024 * 1024,
                 resumeWriterThreshold: 1024 * 512,
                 minimumSegmentSize: -1,
@@ -32,8 +32,8 @@ public class Program
 
                   options.SendPipeOptions = new PipeOptions(
                 pool: MemoryPool<byte>.Shared,
-                readerScheduler: PipeScheduler.ThreadPool,
-                writerScheduler: PipeScheduler.ThreadPool,
+                readerScheduler: PipeScheduler.Inline,
+                writerScheduler: PipeScheduler.Inline,
                 pauseWriterThreshold: 64 * 1024,
                 resumeWriterThreshold: 32 * 1024,
                 minimumSegmentSize: -1,

+ 2 - 2
frameworks/CSharp/touchsocket/src/TouchSocketHttpPlatform/DateHeader.cs

@@ -20,7 +20,7 @@ namespace TouchSocketHttpPlatform;
 internal static class DateHeader
 {
     private const int dateTimeRLength = 29;
-    private const int prefixLength = 6; // "Date: ".Length is 6 in bytes for ASCII
+    private const int prefixLength = 8; // "\r\nDate: ".Length is 8 in bytes for ASCII
     private const int suffixIndex = dateTimeRLength + prefixLength;
 
     // Wed, 14 Mar 2018 14:20:00 GMT
@@ -35,7 +35,7 @@ internal static class DateHeader
 
     static DateHeader()
     {
-        var utf8 = "Date: "u8;
+        var utf8 = "\r\nDate: "u8;
 
         utf8.CopyTo(s_headerBytesMaster);
         utf8.CopyTo(s_headerBytesScratch);

+ 2 - 2
frameworks/CSharp/touchsocket/src/TouchSocketHttpPlatform/MyTcpSessionClientBase.cs

@@ -52,13 +52,13 @@ internal sealed class MyTcpSessionClientBase : TcpSessionClient
            "HTTP/1.1 200 OK\r\n"u8 +
            "Server: T\r\n"u8 +
            "Content-Type: text/plain\r\n"u8 +
-           "Content-Length: 13\r\n"u8;
+           "Content-Length: 13"u8;
 
     private static ReadOnlySpan<byte> JsonPreamble =>
         "HTTP/1.1 200 OK\r\n"u8 +
         "Server: T\r\n"u8 +
         "Content-Type: application/json\r\n"u8 +
-        "Content-Length: 27\r\n"u8;
+        "Content-Length: 27"u8;
 
     protected override async Task ReceiveLoopAsync(ITransport transport)
     {

+ 4 - 4
frameworks/CSharp/touchsocket/src/TouchSocketHttpPlatform/Program.cs

@@ -31,8 +31,8 @@ internal class Program
 
                 options.ReceivePipeOptions = new PipeOptions(
               pool: MemoryPool<byte>.Shared,
-              readerScheduler: PipeScheduler.ThreadPool,
-              writerScheduler: PipeScheduler.ThreadPool,
+              readerScheduler: PipeScheduler.Inline,
+              writerScheduler: PipeScheduler.Inline,
               pauseWriterThreshold: 1024 * 1024,
               resumeWriterThreshold: 1024 * 512,
               minimumSegmentSize: -1,
@@ -40,8 +40,8 @@ internal class Program
 
                 options.SendPipeOptions = new PipeOptions(
               pool: MemoryPool<byte>.Shared,
-              readerScheduler: PipeScheduler.ThreadPool,
-              writerScheduler: PipeScheduler.ThreadPool,
+              readerScheduler: PipeScheduler.Inline,
+              writerScheduler: PipeScheduler.Inline,
               pauseWriterThreshold: 64 * 1024,
               resumeWriterThreshold: 32 * 1024,
               minimumSegmentSize: -1,

+ 4 - 4
frameworks/CSharp/touchsocket/src/TouchSocketWebApi/Program.cs

@@ -26,8 +26,8 @@ public class Program
 
                 options.ReceivePipeOptions = new PipeOptions(
                 pool: MemoryPool<byte>.Shared,
-                readerScheduler: PipeScheduler.ThreadPool,
-                writerScheduler: PipeScheduler.ThreadPool,
+                readerScheduler: PipeScheduler.Inline,
+                writerScheduler: PipeScheduler.Inline,
                 pauseWriterThreshold: 1024 * 1024,
                 resumeWriterThreshold: 1024 * 512,
                 minimumSegmentSize: -1,
@@ -35,8 +35,8 @@ public class Program
 
                 options.SendPipeOptions = new PipeOptions(
               pool: MemoryPool<byte>.Shared,
-              readerScheduler: PipeScheduler.ThreadPool,
-              writerScheduler: PipeScheduler.ThreadPool,
+              readerScheduler: PipeScheduler.Inline,
+              writerScheduler: PipeScheduler.Inline,
               pauseWriterThreshold: 64 * 1024,
               resumeWriterThreshold: 32 * 1024,
               minimumSegmentSize: -1,