Repository: apple/swift-nio
File: Sources/NIOCore/NIOPooledRecvBufferAllocator.swift (Swift)

Code before:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//

/// A receive buffer allocator which cycles through a pool of buffers.
///
/// Channels can read multiple times per cycle (based on `ChannelOptions.maxMessagesPerRead`), and they reuse
/// the inbound buffer for each read. If a `ChannelHandler` holds onto this buffer, then CoWing will be needed.
/// A `NIOPooledRecvBufferAllocator` cycles through preallocated buffers to avoid CoWs during the same read cycle.
public struct NIOPooledRecvBufferAllocator {
    // The pool will either use a single buffer (i.e. `buffer`) OR store multiple buffers
    // in `buffers`. If `buffers` is non-empty then `buffer` MUST be `nil`. If `buffer`
    // is non-nil then `buffers` MUST be empty.
    //
    // The backing storage is changed from `buffer` to `buffers` when a second buffer is
    // needed (and if capacity allows).
    @usableFromInline
    internal var _buffer: Optional<ByteBuffer>
    @usableFromInline
    internal var _buffers: [ByteBuffer]
    /// The index into `buffers` of the index which was last used.
    @usableFromInline
    internal var _lastUsedIndex: Int

    /// Maximum number of buffers to store in the pool.
    public private(set) var capacity: Int
    /// The receive allocator providing hints for the next buffer size to use.
    public var recvAllocator: RecvByteBufferAllocator

    /// The return value from the last call to `recvAllocator.record(actualReadBytes:)`.
    @usableFromInline
    internal var _mayGrow: Bool

    /// Builds a new instance of `NIOPooledRecvBufferAllocator`
    ///

Review comment (suggestion):
As a public type we should annotate whether this is `Sendable` or not. It _looks_ like it is.

Code after:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//

/// A receive buffer allocator which cycles through a pool of buffers.
///
/// Channels can read multiple times per cycle (based on `ChannelOptions.maxMessagesPerRead`), and they reuse
/// the inbound buffer for each read. If a `ChannelHandler` holds onto this buffer, then CoWing will be needed.
/// A `NIOPooledRecvBufferAllocator` cycles through preallocated buffers to avoid CoWs during the same read cycle.
public struct NIOPooledRecvBufferAllocator: Sendable {
    // The pool will either use a single buffer (i.e. `buffer`) OR store multiple buffers
    // in `buffers`. If `buffers` is non-empty then `buffer` MUST be `nil`. If `buffer`
    // is non-nil then `buffers` MUST be empty.
    //
    // The backing storage is changed from `buffer` to `buffers` when a second buffer is
    // needed (and if capacity allows).
    @usableFromInline
    internal var _buffer: Optional<ByteBuffer>
    @usableFromInline
    internal var _buffers: [ByteBuffer]
    /// The index into `buffers` of the index which was last used.
    @usableFromInline
    internal var _lastUsedIndex: Int

    /// Maximum number of buffers to store in the pool.
    public private(set) var capacity: Int
    /// The receive allocator providing hints for the next buffer size to use.
    public var recvAllocator: RecvByteBufferAllocator

    /// The return value from the last call to `recvAllocator.record(actualReadBytes:)`.
    @usableFromInline
    internal var _mayGrow: Bool

    /// Builds a new instance of `NIOPooledRecvBufferAllocator`
    ///