Files
swift-nio-imap/Sources/NIOIMAPCore/Parser/Grammar
Daniel Eggert ab79acbdc5 Add parser input types
ParseInput: a ~Escapable, BitwiseCopyable cursor over a table of region
pointers, with the primitives and combinators as methods. Input buffers:
ParserInputBuffer (+ Consumable, StableRegion refinements), conformances
for ByteBuffer / Array / Data, and IMAPInputBuffer, the staging deque that
can hand out payloads by reference.

IMAPInputBuffer keeps reads of at least copyThreshold (1 KiB) by
reference and copies smaller ones into a 1 KiB tail (an OutputSegment),
so a peer sending a byte per read costs a region per KiB, not a region,
an object and a retained receive buffer per byte. A tail is written
only past its end, only while the buffer holds the sole reference, and
never reallocated; a used-up tail with room is continued by the next
small read into an empty buffer. Only a StableAddressByteRegion (arrays,
ByteBuffer) or an IMAPBuffer over one is kept by reference; any other
large region, e.g. Data, is copied once.

A bare DispatchData.Region is copied too: it points into its
DispatchData without keeping it alive, so it is not a
StableAddressByteRegion. append(contentsOf:) is the zero-copy path for
DispatchData.

Public API additions: IMAPInputBuffer.append(contentsOf: DispatchData)
walks a read's regions with enumerateBytes, so it builds no regions
array; a large region is kept with the DispatchData that owns it (also
the owner a makeDispatchData() donation retains), small ones go to the
tail. append(_:) gains overloads for a StableAddressByteRegion and an
IMAPBuffer, so appending a ByteBuffer, an array or an IMAPBuffer pays no
dynamic cast; IMAPByteStorage.init(_:) gains one for an IMAPBuffer.

Additive; the grammar still runs on ParseBuffer. Review focus: _span(_:)
and RegionSink (the two read-side unsafe seams), the head invariant in
advance(by:), the gather's 64-frame recursion cap,
IMAPInputBuffer.writableTail(fitting:), and DispatchDataRegion, what
append(contentsOf:) keeps: its @unchecked Sendable and its
StableAddressByteRegion (fixed-address) promise.

Temporary: main's SubParser is renamed LegacySubParser so both coexist.
2026-11-24 09:47:33 +09:00
..
2026-11-24 09:47:33 +09:00