From f52ec9962ec80e841bc1f73dda37aa6a08025b77 Mon Sep 17 00:00:00 2001 From: Daniel Eggert Date: Sun, 15 Nov 2026 10:37:18 +0100 Subject: [PATCH] Don't allocate a promise per buffer for writes without one --- Sources/NIOIMAP/Coders/IMAPClientHandler.swift | 8 ++++++-- Sources/NIOIMAP/Coders/IMAPServerHandler.swift | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/NIOIMAP/Coders/IMAPClientHandler.swift b/Sources/NIOIMAP/Coders/IMAPClientHandler.swift index fe5a820d..fc8a523c 100644 --- a/Sources/NIOIMAP/Coders/IMAPClientHandler.swift +++ b/Sources/NIOIMAP/Coders/IMAPClientHandler.swift @@ -193,12 +193,16 @@ public final class IMAPClientHandler: ChannelDuplexHandler { context: ChannelHandlerContext ) { let outbound = self.wrapOutboundOut(bytes) + guard let promise = chunk.promise else { + context.write(outbound, promise: nil) + return + } if position == .last, chunk.shouldSucceedPromise { - context.write(outbound, promise: chunk.promise) + context.write(outbound, promise: promise) } else { // Not the last buffer, or a chunk that will only be finished by a later one: the promise // must not succeed yet, but a failure anywhere has to reach it. - context.write(outbound).cascadeFailure(to: chunk.promise) + context.write(outbound).cascadeFailure(to: promise) } } diff --git a/Sources/NIOIMAP/Coders/IMAPServerHandler.swift b/Sources/NIOIMAP/Coders/IMAPServerHandler.swift index dda0300d..a3912fbf 100644 --- a/Sources/NIOIMAP/Coders/IMAPServerHandler.swift +++ b/Sources/NIOIMAP/Coders/IMAPServerHandler.swift @@ -140,7 +140,8 @@ public final class IMAPServerHandler: ChannelDuplexHandler { let buffers = bytes.makeOutboundBuffers() for (index, buffer) in buffers.enumerated() { let outbound = self.wrapOutboundOut(buffer) - if index == buffers.count - 1 { + // Without a promise there is nothing to order, and no need to allocate one per write. + if index == buffers.count - 1 || promise == nil { context.write(outbound, promise: promise) } else { context.write(outbound).cascadeFailure(to: promise)