Encode into an EncoderOutputBuffer sink

EncodeBuffer appends to `any EncoderOutputBuffer` (IMAPOutputBuffer by
default) instead of holding a ByteBuffer. Chunks come out as
IMAPBufferList; the handlers write one buffer per payload via
makeOutboundBuffers(). CommandEncoder is deleted: it kept only the first
chunk of a synchronizing literal and had no users but its own test.
nextChunk() caps a chunk at 64 regions, coalescing the tail, whatever
the sink returned, so no sink (a caller's own included) has to.

EncodeBuffer stages writes under 256 bytes inline and appends them to
the sink in one call: one existential call per ~256 bytes, not per
write. It flushes before every hand-out and before any direct sink
append (writeBytes(IMAPBuffer), larger writes), so payloads still reach
the sink by reference and in order. Conformers see batched appends;
byteCount may lag until a hand-out (documented on EncoderOutputBuffer).
Per-write cost therefore no longer separates the two sinks, so the
docs choosing IMAPOutputBuffer over ByteBufferOutput (ByteBufferOutput's,
EncoderOutputBuffer's, IMAPOutputBuffer's, the handlers') rest on the
hand-out instead.

Review focus: totalWritten / handedOut and absolute stop points in
nextChunk() (an off-by-one deadlocks the protocol, see
EncodeBufferSinkTests), the staging flush points, and
IMAPClientHandler.writeChunk's promise handling across several writes.

The CLI example's round-trip handler compares all chunks via the public
nextChunk() (it compared only the first) and drops its SPI import.

Public API breaks:
- CommandEncodeBuffer / ResponseEncodeBuffer: `init(buffer:…)` ->
  `init(sink: = IMAPOutputBuffer(), …)`; no longer Sendable.
- EncodeBuffer.Chunk.bytes and ResponseEncodeBuffer.readBytes() are
  IMAPBufferList.
- EncodeBuffer.writeBytes takes a Collection; writeBytes(IMAPBuffer) added;
  writeBuffer(inout ByteBuffer) removed.
- EncodeBuffer (SPI) is no longer Sendable; == / hash compare the pending
  bytes.

Public API addition: CommandEncodeBuffer.nextChunk() -> Chunk?, with
Chunk { bytes, nextStep } and NextStep { proceed,
awaitContinuationRequest }, so a client with its own transport needs no
SPI. nextStep awaits whenever a chunk ends at a synchronizing literal
header, also at the last byte written (APPEND .beginMessage,
.catenateData(.begin)), where the SPI Chunk.waitForContinuation says no
wait. ClientStateMachine uses it and imports no NIOIMAPCore SPI.

Behavior change: an APPEND part that ends in a literal the APPEND flow
doesn't expect (zero-length, e.g. an empty mailbox with quoted strings
off) waits for `+` before the next part instead of running ahead; its
write promise completes after the `+`.

Examples/PerformanceTester (IMAPDispatchClient) does not build from here
until "Measure IMAPDispatchClient against either library".

Temporary: EncodeBuffer+ByteBufferShim.swift keeps an internal
writeBuffer(inout ByteBuffer) for the model types that still hold one.
This commit is contained in:
Daniel Eggert committed 2026-11-24 09:47:33 +09:00
1 parent ab79acbdc5
commit cbbce83ab6
48 files changed
+1563 -800

No files matched your search

@@ -30,18 +30,13 @@
/// ```swift
/// // the default: segments the library allocates
/// var encoder = CommandEncodeBuffer(options: options, loggingMode: false)
/// // or, for payload-heavy traffic headed to a NIO Channel
/// var encoder = CommandEncodeBuffer(sink: ByteBufferOutput(), options: options, loggingMode: false)
/// // or a sink of your own
/// var encoder = CommandEncodeBuffer(sink: MyOutput(), options: options, loggingMode: false)
/// ```
///
/// - ``IMAPOutputBuffer`` is the shipped conformer, the default argument, and what `NIOIMAP`'s
/// handlers use. Reach for it unless you have measured a reason not to: it never moves a byte it has
/// already written, which is what makes handing out a chunk while encoding continues free.
/// - `ByteBufferOutput`, in `NIOIMAP`, writes straight into a `ByteBuffer` and hands out slices of it,
/// so nothing is copied between the encoder and a `Channel`. It allocates less and wins on traffic
/// dominated by large payloads; it loses on traffic made of many small writes, because a
/// `ByteBuffer` write costs more per call than a segment write. It is a caller's choice, not the
/// default.
/// - Conform yourself when the bytes have to land in storage this package cannot know about — a file,
/// shared memory, a ring buffer. The six members below are the whole obligation, plus value
/// semantics: copying an encoder copies its sink, so a class-backed sink would leave both copies
@@ -50,6 +45,12 @@
/// This mirrors the read side, where ``ParserInputBuffer`` is the seam and ``IMAPInputBuffer`` is the
/// conformer to reach for.
///
/// ## Batched appends
///
/// The encoder stages small writes and appends them together, so a conformer sees fewer, larger
/// appends than there were writes, and ``byteCount`` and ``withPendingRegions(_:)`` may lag what has
/// been encoded. Bytes always arrive in order; staged ones arrive before the next ``takeFirst(_:)``.
///
/// ## What is deliberately not here
///
/// - **Stop points and chunking.** They belong to the encoder, which expresses them as offsets