//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// /// Bytes the parser can read from, in one or more contiguous runs. /// /// Internal: ``IMAPInputBuffer`` is the only public parse input. This protocol is what the parser's /// gather is written against, so that the library's own input buffer and the package's tests (which /// parse out of arrays) share one code path. /// /// A conformer describes its bytes as an ordered collection of ``ByteRegion``s and says how many /// bytes there are in total. Everything else has a default implementation. /// ``ConsumableParserInputBuffer`` adds "the parser may discard what it consumed", which the stream /// parsers rely on, and ``StableRegionParserInputBuffer`` adds "my region pointers stay valid for a /// borrow of me", which buys a single-pass gather and copy-free extraction of large payloads. /// ``IMAPInputBuffer`` is all three. /// /// - Important: ``byteRegions`` must begin at the read position, and its regions must add up to /// ``count``. A conformer that keeps a read head as an offset has to *slice* its first region /// rather than hand over the whole thing and expect the parser to skip the prefix — the parser /// reports how much it consumed relative to what it was shown, so a skipped prefix gets counted /// twice and bytes go missing. Both halves are asserted in debug builds. /// /// ## Cost /// /// The parser's per-region overhead is small but not free — each move from one region to /// the next costs something, and bulk scans restart at every boundary. That is invisible for /// network-sized reads and dominant for pathologically small ones: one-byte regions parse far slower /// per byte than contiguous bytes do. internal protocol ParserInputBuffer { /// The collection of contiguous byte runs this buffer is made of. associatedtype Regions: Collection where Regions.Element: ByteRegion /// The buffer's bytes, in order, starting at the read position. var byteRegions: Regions { get } /// The total number of bytes, across every region. var count: Int { get } /// How many regions ``byteRegions`` holds. /// /// The default implementation counts ``byteRegions``. Override it when the count is cheaper to /// come by than the regions themselves — `DispatchData` allocates an array to present its /// regions, and the gather only needs the number in order to size its table. var regionCount: Int { get } /// Reads through the buffer's bytes. /// /// This is the gather: it collects the buffer's regions into one flat table and hands `body` a /// cursor over them. Nothing about the cursor may outlive the call. /// /// The default implementation walks ``byteRegions``, holding each region's accessor open for as /// long as the cursor exists. ``StableRegionParserInputBuffer`` conformers replace it with a /// single pass, and must do so with an explicit one-line witness of their own — see that /// protocol. /// /// - Parameters: /// - limit: The most bytes the parser may consume. Bytes past it are invisible to it, which is /// how a caller parses a prefix of what it holds without slicing first. /// - body: A closure that reads through the cursor. /// - Returns: Whatever `body` returns. func withParseInput(limit: Int, _ body: (inout ParseInput) throws -> R) rethrows -> R } /// A ``ParserInputBuffer`` the parser can also consume from, so that a caller does not have to trim /// it by hand: the stream parsers discard whatever they consumed, including when they throw. /// /// - Important: ``discardFirst(_:)`` must be O(1) amortised in the number of bytes discarded. A /// conformer that moves the remaining bytes down instead makes a caller that trims after every /// message quadratic over the life of a connection. That is why the array types are deliberately /// *not* consumable here, even though removing a prefix from them is one line: the one line is the slow /// one. internal protocol ConsumableParserInputBuffer: ParserInputBuffer { /// Discards the first `k` bytes. /// /// - Parameter k: How many bytes to discard. Must not exceed ``ParserInputBuffer/count``. mutating func discardFirst(_ k: Int) } /// A ``ParserInputBuffer`` whose region pointers stay valid for as long as the buffer is borrowed. /// /// **This is one of the two unsafe seams on the parser's read side, and conforming to it is an /// assertion the compiler cannot check.** ``ByteRegion`` promises only that a pointer is valid for the /// duration of a ``ByteRegion/withUnsafeBytes(_:)`` call. A conformer here promises more: that the /// pointers it pushes into a ``RegionSink`` stay valid for as long as a borrow of `self` lasts, so the /// gather can collect them in one pass instead of nesting one accessor scope per region. /// /// What that buys is not mainly speed. The generic gather recurses once per region, so it needs a /// depth cap and has to coalesce the tail beyond it; a single pass needs neither, at any region /// count. /// /// It is also where zero-copy extraction comes from. A conformer that owns its bytes can pass an /// ``IMAPByteStorage`` to ``RegionSink/append(_:owner:offset:)``, and a large payload extracted from /// that region is then handed to the caller as a reference to the input rather than as a copy — if it /// is at least a third of what the storage keeps alive (see ``StableAddressByteRegion/pinnedByteCount``). /// Passing `nil` is always correct and means such an extraction copies. internal protocol StableRegionParserInputBuffer: ParserInputBuffer { /// Pushes every region, in order, into `sink`. /// /// - Parameter sink: The table to fill. The only thing that can be done with it is appending. borrowing func withStableRegions(into sink: inout RegionSink) } /// The table a ``StableRegionParserInputBuffer`` fills, one region at a time. /// /// The library owns the storage and validates every push, so a conformer cannot get the bookkeeping /// wrong: it cannot overrun the table, and it cannot describe a range that lies outside the owner it /// names. It is neither copyable nor escapable, so it cannot outlive the gather that created it. internal struct RegionSink: ~Copyable, ~Escapable { /// Where the entries go. Sized from ``ParserInputBuffer/regionCount``. private let table: UnsafeMutableBufferPointer /// How many entries have been pushed. private(set) var filled: Int /// How many bytes have been pushed, for the conformance assertion in the gather. private(set) var byteCount: Int @_lifetime(immortal) internal init(table: UnsafeMutableBufferPointer) { self.table = table self.filled = 0 self.byteCount = 0 } /// Adds one region. /// /// - Parameters: /// - bytes: The region's bytes. Must stay valid for as long as `self` and the borrow of the /// buffer that produced it — this is the promise /// ``StableRegionParserInputBuffer`` makes. /// - owner: Who keeps `bytes` alive, if the buffer can vouch for them, so that a large /// extraction can refer to the input rather than copying it. `nil` means an extraction /// copies, which is always correct. The sink retains it until the gather ends, so a box /// created just for this call is safe, if wasteful. /// - offset: Where `bytes` begins inside `owner`. Zero when the region is all of the owner; /// the head offset when the region is the unconsumed tail of one. @_lifetime(self: copy self) internal mutating func append(_ bytes: UnsafeBufferPointer, owner: IMAPByteStorage?, offset: Int) { precondition( filled < table.count, "a ParserInputBuffer pushed more than the \(table.count) regions its regionCount promised" ) if let owner { precondition( offset >= 0 && offset + bytes.count <= owner.count, "a region of \(bytes.count) bytes at offset \(offset) does not fit its owner's \(owner.count)" ) } table[filled] = RegionEntry( bytes: bytes, owner: owner.map { Unmanaged.passRetained($0) }, ownerOffset: offset ) filled += 1 byteCount += bytes.count } /// Balances the retain `append` took on each owner. Called once, when the gather ends. internal func releaseOwners() { for entry in table[..(limit: Int, _ body: (inout ParseInput) throws -> R) rethrows -> R { let regions = byteRegions assertRegionsAddUp(regions) // One entry per region up to the cap, plus one for a coalesced tail. let capacity = Swift.min(regionCount, Self.maximumGatherDepth) + 1 return try withUnsafeTemporaryAllocation(of: RegionEntry.self, capacity: capacity) { table in try Self.descend(regions, regions.startIndex, 0, 0, table, limit, body) } } /// Reads through the buffer's bytes, with no limit on how many the parser may consume. /// /// - Parameter body: A closure that reads through the cursor. /// - Returns: Whatever `body` returns. /// /// - Note: This is deliberately a *different* signature from /// ``ParserInputBuffer/withParseInput(limit:_:)`` rather than a default argument on it. A /// default argument here would shadow a conformer's own witness, so a one-pass conformer would /// silently take the generic path whenever the caller left the limit out — which is the common /// case, and the failure is invisible. internal func withParseInput(_ body: (inout ParseInput) throws -> R) rethrows -> R { try withParseInput(limit: .max, body) } /// Fills the table one region deeper per frame. /// /// The nesting is forced: a cursor cannot be threaded *through* a chain of accessor scopes, so /// the descent writes pointers into a flat table on the way down and builds the cursor at the /// innermost frame, where every scope is still open. Do not restructure this into a loop that /// opens and closes one region at a time. private static func descend( _ regions: Regions, _ index: Regions.Index, _ depth: Int, _ filled: Int, _ table: UnsafeMutableBufferPointer, _ limit: Int, _ body: (inout ParseInput) throws -> R ) rethrows -> R { guard index != regions.endIndex else { var input = ParseInput(table: ChunkTable(rebasing: table[.. R in guard !raw.isEmpty else { // An empty region carries no bytes, so it needs no entry — but it does cost a frame. return try descend(regions, next, depth + 1, filled, table, limit, body) } // Not `bindMemory`, which would rebind the caller's memory for good; and not // `withMemoryRebound`, which costs a frame per region on a stack-bounded descent. table[filled] = RegionEntry(bytes: raw.assumingMemoryBound(to: UInt8.self)) return try descend(regions, next, depth + 1, filled + 1, table, limit, body) } } /// Copies every region from `index` onwards into one buffer, and parses that as a single region. /// /// This is the pathological path: a caller that hands over more regions than the recursion can /// safely descend gets a copy of the tail rather than a stack overflow. Nothing is dropped, and /// the copy is bounded by the bytes the caller passed. private static func coalescing( _ regions: Regions, _ index: Regions.Index, _ filled: Int, _ table: UnsafeMutableBufferPointer, _ limit: Int, _ body: (inout ParseInput) throws -> R ) rethrows -> R { var tail = [UInt8]() for region in regions[index...] { region.withUnsafeBytes { tail.append(contentsOf: $0) } } return try tail.withUnsafeBufferPointer { bytes -> R in var count = filled if !bytes.isEmpty { table[filled] = RegionEntry(bytes: bytes) count += 1 } var input = ParseInput(table: ChunkTable(rebasing: table[..(limit: Int, _ body: (inout ParseInput) throws -> R) rethrows -> R { /// try _stableWithParseInput(limit: limit, body) /// } /// ``` /// /// The forward has to be written out like that rather than left to a default implementation on /// this protocol. A default here satisfies the *base* protocol's requirement, and which of the /// two the compiler picks depends on where the call is made from — so `parse` would take the /// generic path while a direct call took this one, with nothing failing to announce it. /// /// - Parameters: /// - limit: The most bytes the parser may consume. /// - body: A closure that reads through the cursor. /// - Returns: Whatever `body` returns. internal func _stableWithParseInput( limit: Int, _ body: (inout ParseInput) throws -> R ) rethrows -> R { try withUnsafeTemporaryAllocation( of: RegionEntry.self, capacity: Swift.max(regionCount, 1) ) { table in var sink = RegionSink(table: table) defer { sink.releaseOwners() } withStableRegions(into: &sink) assert( sink.byteCount == count, "\(Self.self) pushed \(sink.byteCount) bytes but its count is \(count). Regions must " + "begin at the read position and add up to count." ) var input = ParseInput(table: sink.filledTable, owner: self, limit: limit) return try body(&input) } } } // MARK: - Consuming extension ConsumableParserInputBuffer { /// Reads through the buffer's bytes and discards whatever the parser consumed. /// /// This is the entry point a connection uses: the caller cannot forget to trim, and cannot trim /// by the wrong amount. /// /// Trimming happens on every exit path, a thrown error included, because the streaming literal /// modes legitimately make progress and *then* report that they need more bytes. A parser that /// wants its position back on failure gets that from the cursor's own backtracking instead, which /// leaves nothing consumed to trim. /// /// - Parameters: /// - limit: The most bytes the parser may consume. /// - body: A closure that reads through the cursor. /// - Returns: Whatever `body` returns. internal mutating func parse( limit: Int = .max, _ body: (inout ParseInput) throws -> R ) rethrows -> R { var consumed = 0 defer { if consumed > 0 { self.discardFirst(consumed) } } // `limit:` is passed explicitly so that this binds a conformer's own witness rather than the // no-limit convenience overload, which would send a one-pass conformer down the generic path. return try withParseInput(limit: limit) { input in defer { consumed = input.consumed } return try body(&input) } } } // MARK: - Stdlib conformances // For the package's tests, which parse out of arrays. Read-only on purpose: removing a prefix from an // array moves the rest of it. extension Array: ParserInputBuffer where Element == UInt8 { var byteRegions: CollectionOfOne<[UInt8]> { CollectionOfOne(self) } } extension ArraySlice: ParserInputBuffer where Element == UInt8 { var byteRegions: CollectionOfOne> { CollectionOfOne(self) } } extension ContiguousArray: ParserInputBuffer where Element == UInt8 { var byteRegions: CollectionOfOne> { CollectionOfOne(self) } }