Skip to content

Commit 38608db

Browse files
aryan-25Lukasa
andauthored
Reduce time spent logging EventLoop description in HTTP1ClientChannelHandler (#772)
### Motivation: A performance test executing 100,000 sequential requests against a simple [`NIOHTTP1Server`](https://github.com/apple/swift-nio/blob/main/Sources/NIOHTTP1Server/README.md) revealed that 7% of total run time is spent in the setter of the `request` property in `HTTP1ClientChannelHandler` (GitHub Issue #754). The poor performance comes from [processing the string interpolation `"\(self.eventLoop)"`](https://github.com/swift-server/async-http-client/blob/6df8e1c17e68f0f93de2443b8c8cafca9ddcc89a/Sources/AsyncHTTPClient/ConnectionPool/HTTP1/HTTP1ClientChannelHandler.swift#L39C17-L39C75) which under the hood calls a computed property. This problem can entirely be avoided by storing `eventLoop.description` when initializing `HTTP1ClientChannelHandler`, and using that stored value in `request`'s setter, rather than computing the property each time. ### Modifications: - Created a new property `let eventLoopDescription: Logger.MetadataValue` in `HTTP1ClientChannelHandler` that stores the description of the `eventLoop` argument that is passed into the initializer. - Replaced the string interpolation `"\(self.eventLoop)"` in `request`'s setter with `self.eventLoopDescription`. ### Result: `HTTP1ClientChannelHandler.eventLoop`'s `description` property is cached upon initialization rather than being computed each time in the `request` property's setter. --------- Co-authored-by: Cory Benfield <[email protected]>
1 parent 15dbe6d commit 38608db

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Sources/AsyncHTTPClient/ConnectionPool/HTTP1/HTTP1ClientChannelHandler.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler {
3636
if let newRequest = self.request {
3737
var requestLogger = newRequest.logger
3838
requestLogger[metadataKey: "ahc-connection-id"] = self.connectionIdLoggerMetadata
39-
requestLogger[metadataKey: "ahc-el"] = "\(self.eventLoop)"
39+
requestLogger[metadataKey: "ahc-el"] = self.eventLoopDescription
4040
self.logger = requestLogger
4141

4242
if let idleReadTimeout = newRequest.requestOptions.idleReadTimeout {
@@ -72,11 +72,13 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler {
7272
private let backgroundLogger: Logger
7373
private var logger: Logger
7474
private let eventLoop: EventLoop
75+
private let eventLoopDescription: Logger.MetadataValue
7576
private let connectionIdLoggerMetadata: Logger.MetadataValue
7677

7778
var onConnectionIdle: () -> Void = {}
7879
init(eventLoop: EventLoop, backgroundLogger: Logger, connectionIdLoggerMetadata: Logger.MetadataValue) {
7980
self.eventLoop = eventLoop
81+
self.eventLoopDescription = "\(eventLoop.description)"
8082
self.backgroundLogger = backgroundLogger
8183
self.logger = backgroundLogger
8284
self.connectionIdLoggerMetadata = connectionIdLoggerMetadata

0 commit comments

Comments
 (0)