mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-27 05:04:23 +00:00
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.
76 lines
3.5 KiB
Swift
76 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIO
|
|
import NIOIMAPCore
|
|
|
|
// This file is what lets a caller holding a `ByteBuffer` hand it to the parser. `NIOIMAPCore` does not
|
|
// know about NIO, so the conformances live here; `NIOIMAP` re-exports `NIOIMAPCore`, so which of the two
|
|
// modules declares them is invisible to anyone importing `NIOIMAP`.
|
|
//
|
|
// They need no `@retroactive`: the protocols are declared in this package, so this is not a conformance
|
|
// two other modules could also add.
|
|
|
|
extension ByteBuffer: ByteRegion {
|
|
/// Calls the given closure with a pointer to the buffer's readable bytes.
|
|
///
|
|
/// This is `withUnsafeReadableBytes(_:)` under the name `ByteRegion` requires, and it is what
|
|
/// lets a `ByteBuffer` be handed to `NIOIMAPCore` without being copied first.
|
|
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
|
|
try withUnsafeReadableBytes(body)
|
|
}
|
|
}
|
|
|
|
// A copy of a `ByteBuffer` keeps its storage alive, and while copies share storage a write through any of
|
|
// them copies it first — so the readable bytes a copy sees neither move nor change. That lets
|
|
// `IMAPInputBuffer` keep a large one and `makeDispatchData()` donate one, rather than copy.
|
|
extension ByteBuffer: StableAddressByteRegion {
|
|
/// The capacity of the storage, which every copy and slice of this buffer keeps alive: a 1 KiB
|
|
/// slice of a 64 KiB read pins 64 KiB.
|
|
public var pinnedByteCount: Int { storageCapacity }
|
|
}
|
|
|
|
extension ByteBuffer: ParserInputBuffer {
|
|
/// The buffer's readable bytes, as the one region they are.
|
|
public var byteRegions: CollectionOfOne<ByteBuffer> { CollectionOfOne(self) }
|
|
|
|
/// The number of readable bytes.
|
|
///
|
|
/// This is ``ParserInputBuffer/count`` — bytes the parser can see — which for a `ByteBuffer` means
|
|
/// its readable bytes and not its capacity.
|
|
public var count: Int { readableBytes }
|
|
|
|
/// One, always: a `ByteBuffer`'s bytes are contiguous by construction.
|
|
public var regionCount: Int { 1 }
|
|
}
|
|
|
|
extension ByteBuffer: ConsumableParserInputBuffer {
|
|
/// Moves the reader index forward by `k`.
|
|
///
|
|
/// O(1), and it satisfies ``ConsumableParserInputBuffer``'s requirement that discarding a prefix
|
|
/// must not move the remaining bytes — a `ByteBuffer` keeps its read head as an index.
|
|
///
|
|
/// - Parameter k: How many bytes to discard. Must not exceed ``count``.
|
|
public mutating func discardFirst(_ k: Int) {
|
|
moveReaderIndex(forwardBy: k)
|
|
}
|
|
}
|
|
|
|
// A `ByteBuffer` deliberately does *not* conform to `StableRegionParserInputBuffer`, so a payload
|
|
// extracted from one is copied rather than referred to. Vouching for a region means handing the gather
|
|
// an `IMAPByteStorage` that outlives it, and `RegionSink.append` takes that owner *unretained* — so a
|
|
// conformer that boxes inside `withStableRegions` would hand over a box that is already dead. Only
|
|
// something that *stores* the box can promise this, which is what `IMAPInputBuffer` is for: append a
|
|
// `ByteBuffer` to one of those and a multi-megabyte literal reaches the caller uncopied.
|