Преглед изворни кода

Eliminate unnecessary allocations by using single-element writes (#1965)

Using the collection-based write was wrapping single elements into a collection and write(contentsOf:) was being called.
This always results in the element being wrapped in a Deque in the NIOAsyncWriter.
This commit moves the write(element:) method to be required for the RPCWriter protocol so that we can call the single element write and avoid this boxing to save some allocations.
Gustavo Cairo пре 1 година
родитељ
комит
55f9b2c603

+ 4 - 0
Sources/GRPCHTTP2Core/Client/Connection/Connection.swift

@@ -366,6 +366,10 @@ extension Connection {
         self.http2Stream = http2Stream
       }
 
+      func write(_ element: RPCRequestPart) async throws {
+        try await self.requestWriter.write(element)
+      }
+
       func write(contentsOf elements: some Sequence<Self.Element>) async throws {
         try await self.requestWriter.write(contentsOf: elements)
       }

+ 4 - 0
Sources/GRPCHTTP2Core/Server/Connection/ServerConnection.swift

@@ -35,6 +35,10 @@ public enum ServerConnection {
         self.http2Stream = http2Stream
       }
 
+      public func write(_ element: RPCResponsePart) async throws {
+        try await self.responseWriter.write(element)
+      }
+
       public func write(contentsOf elements: some Sequence<Self.Element>) async throws {
         try await self.responseWriter.write(contentsOf: elements)
       }