From d488e6165af3a618eabbc8cd39be09c3609518f5 Mon Sep 17 00:00:00 2001 From: cnkwocha Date: Tue, 3 Dec 2024 14:33:13 +0000 Subject: [PATCH 1/2] Revert `fastRebase` implementation Motivation: UnsafeRawBufferPointer.init(fastRebase:) and UnsafeMutableRawBufferPointer.init(fastRebase:) were shimmed into NIO in https://github.com/apple/swift-nio/pull/1696. The shim is no longer necessay. Modifications: - Revert the use of the `fastRebase` inits to the native `.init(rebasing:)`. Result: Use of native APIs instead. --- Sources/NIOCore/ByteBuffer-aux.swift | 12 ++++---- Sources/NIOCore/ByteBuffer-core.swift | 16 +++++----- Sources/NIOCore/ByteBuffer-int.swift | 2 +- Sources/NIOCore/PointerHelpers.swift | 43 --------------------------- Sources/NIOPosix/ControlMessage.swift | 10 +++++-- Sources/NIOPosix/PointerHelpers.swift | 41 ------------------------- 6 files changed, 24 insertions(+), 100 deletions(-) delete mode 100644 Sources/NIOCore/PointerHelpers.swift delete mode 100644 Sources/NIOPosix/PointerHelpers.swift diff --git a/Sources/NIOCore/ByteBuffer-aux.swift b/Sources/NIOCore/ByteBuffer-aux.swift index 5e07f4af2b..07f0aa77ad 100644 --- a/Sources/NIOCore/ByteBuffer-aux.swift +++ b/Sources/NIOCore/ByteBuffer-aux.swift @@ -39,7 +39,7 @@ extension ByteBuffer { // this is not technically correct because we shouldn't just bind // the memory to `UInt8` but it's not a real issue either and we // need to work around https://bugs.swift.org/browse/SR-9604 - [UInt8](UnsafeRawBufferPointer(fastRebase: ptr[range]).bindMemory(to: UInt8.self)) + [UInt8](UnsafeRawBufferPointer(rebasing: ptr[range]).bindMemory(to: UInt8.self)) } } @@ -205,7 +205,9 @@ extension ByteBuffer { } return self.withUnsafeReadableBytes { pointer in assert(range.lowerBound >= 0 && (range.upperBound - range.lowerBound) <= pointer.count) - return String(decoding: UnsafeRawBufferPointer(fastRebase: pointer[range]), as: Unicode.UTF8.self) + return String( + decoding: UnsafeRawBufferPointer(rebasing: pointer[range]), as: Unicode.UTF8.self + ) } } @@ -329,7 +331,7 @@ extension ByteBuffer { self.withVeryUnsafeMutableBytes { destCompleteStorage in assert(destCompleteStorage.count >= index + allBytesCount) let dest = destCompleteStorage[index..= 0, "Can't write fewer than 0 bytes") self.reserveCapacity(index + count) self.withVeryUnsafeMutableBytes { pointer in - let dest = UnsafeMutableRawBufferPointer(fastRebase: pointer[index.. UnsafeMutablePointer { self._ensureAvailableCapacity(_Capacity(capacity), at: index) let newBytesPtr = UnsafeMutableRawBufferPointer( - fastRebase: self._slicedStorageBuffer[Int(index)..= writerIndex let range = Range(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex)) - return try body(.init(fastRebase: self._slicedStorageBuffer[range])) + return try body(.init(rebasing: self._slicedStorageBuffer[range])) } /// Yields the bytes currently writable (`bytesWritable` = `capacity` - `writerIndex`). Before reading those bytes you must first @@ -677,7 +679,7 @@ public struct ByteBuffer { _ body: (UnsafeMutableRawBufferPointer) throws -> T ) rethrows -> T { self._copyStorageAndRebaseIfNeeded() - return try body(.init(fastRebase: self._slicedStorageBuffer.dropFirst(self.writerIndex))) + return try body(.init(rebasing: self._slicedStorageBuffer.dropFirst(self.writerIndex))) } /// This vends a pointer of the `ByteBuffer` at the `writerIndex` after ensuring that the buffer has at least `minimumWritableBytes` of writable bytes available. @@ -748,7 +750,7 @@ public struct ByteBuffer { public func withUnsafeReadableBytes(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T { // This is safe, writerIndex >= readerIndex let range = Range(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex)) - return try body(.init(fastRebase: self._slicedStorageBuffer[range])) + return try body(.init(rebasing: self._slicedStorageBuffer[range])) } /// Yields a buffer pointer containing this `ByteBuffer`'s readable bytes. You may hold a pointer to those bytes @@ -769,7 +771,7 @@ public struct ByteBuffer { let storageReference: Unmanaged = Unmanaged.passUnretained(self._storage) // This is safe, writerIndex >= readerIndex let range = Range(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex)) - return try body(.init(fastRebase: self._slicedStorageBuffer[range]), storageReference) + return try body(.init(rebasing: self._slicedStorageBuffer[range]), storageReference) } /// See `withUnsafeReadableBytesWithStorageManagement` and `withVeryUnsafeBytes`. @@ -1120,7 +1122,7 @@ extension ByteBuffer { self._ensureAvailableCapacity(_Capacity(length), at: _toIndex(toIndex)) self.withVeryUnsafeMutableBytes { ptr in let srcPtr = UnsafeRawBufferPointer(start: ptr.baseAddress!.advanced(by: fromIndex), count: length) - let targetPtr = UnsafeMutableRawBufferPointer(fastRebase: ptr.dropFirst(toIndex)) + let targetPtr = UnsafeMutableRawBufferPointer(rebasing: ptr.dropFirst(toIndex)) targetPtr.copyMemory(from: srcPtr) } diff --git a/Sources/NIOCore/ByteBuffer-int.swift b/Sources/NIOCore/ByteBuffer-int.swift index d0fb026901..87e0c9abb5 100644 --- a/Sources/NIOCore/ByteBuffer-int.swift +++ b/Sources/NIOCore/ByteBuffer-int.swift @@ -67,7 +67,7 @@ extension ByteBuffer { return self.withUnsafeReadableBytes { ptr in var value: T = 0 withUnsafeMutableBytes(of: &value) { valuePtr in - valuePtr.copyMemory(from: UnsafeRawBufferPointer(fastRebase: ptr[range])) + valuePtr.copyMemory(from: UnsafeRawBufferPointer(rebasing: ptr[range])) } return _toEndianness(value: value, endianness: endianness) } diff --git a/Sources/NIOCore/PointerHelpers.swift b/Sources/NIOCore/PointerHelpers.swift deleted file mode 100644 index 0acc2f2c2d..0000000000 --- a/Sources/NIOCore/PointerHelpers.swift +++ /dev/null @@ -1,43 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftNIO open source project -// -// Copyright (c) 2017-2018 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 -// -//===----------------------------------------------------------------------===// - -// MARK: Rebasing shims - -// FIXME: Duplicated in NIO. - -// These methods are shimmed in to NIO until https://github.com/apple/swift/pull/34879 is resolved. -// They address the fact that the current rebasing initializers are surprisingly expensive and do excessive -// checked arithmetic. This expense forces them to often be outlined, reducing the ability to optimise out -// further preconditions and branches. -extension UnsafeRawBufferPointer { - @inlinable - init(fastRebase slice: Slice) { - let base = slice.base.baseAddress?.advanced(by: slice.startIndex) - self.init(start: base, count: slice.endIndex &- slice.startIndex) - } - - @inlinable - init(fastRebase slice: Slice) { - let base = slice.base.baseAddress?.advanced(by: slice.startIndex) - self.init(start: base, count: slice.endIndex &- slice.startIndex) - } -} - -extension UnsafeMutableRawBufferPointer { - @inlinable - init(fastRebase slice: Slice) { - let base = slice.base.baseAddress?.advanced(by: slice.startIndex) - self.init(start: base, count: slice.endIndex &- slice.startIndex) - } -} diff --git a/Sources/NIOPosix/ControlMessage.swift b/Sources/NIOPosix/ControlMessage.swift index 79cf861e58..6e6338e6b6 100644 --- a/Sources/NIOPosix/ControlMessage.swift +++ b/Sources/NIOPosix/ControlMessage.swift @@ -79,7 +79,9 @@ struct UnsafeControlMessageStorage: Collection { /// Get the part of the buffer for use with a message. public subscript(position: Int) -> UnsafeMutableRawBufferPointer { UnsafeMutableRawBufferPointer( - fastRebase: self.buffer[(position * self.bytesPerMessage)..<((position + 1) * self.bytesPerMessage)] + rebasing: self.buffer[ + (position * self.bytesPerMessage)..<((position + 1) * self.bytesPerMessage) + ] ) } @@ -316,7 +318,9 @@ struct UnsafeOutboundControlBytes { type: CInt, payload: PayloadType ) { - let writableBuffer = UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[writePosition...]) + let writableBuffer = UnsafeMutableRawBufferPointer( + rebasing: self.controlBytes[writePosition...] + ) let requiredSize = NIOBSDSocketControlMessage.space(payloadSize: MemoryLayout.stride(ofValue: payload)) precondition(writableBuffer.count >= requiredSize, "Insufficient size for cmsghdr and data") @@ -342,7 +346,7 @@ struct UnsafeOutboundControlBytes { if writePosition == 0 { return UnsafeMutableRawBufferPointer(start: nil, count: 0) } - return UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[0..) { - let base = slice.base.baseAddress?.advanced(by: slice.startIndex) - self.init(start: base, count: slice.endIndex &- slice.startIndex) - } - - @inlinable - init(fastRebase slice: Slice) { - let base = slice.base.baseAddress?.advanced(by: slice.startIndex) - self.init(start: base, count: slice.endIndex &- slice.startIndex) - } -} - -extension UnsafeMutableRawBufferPointer { - @inlinable - init(fastRebase slice: Slice) { - let base = slice.base.baseAddress?.advanced(by: slice.startIndex) - self.init(start: base, count: slice.endIndex &- slice.startIndex) - } -} From a3de32cb5d95c926b2331733706333554f79a2f3 Mon Sep 17 00:00:00 2001 From: cnkwocha Date: Tue, 3 Dec 2024 15:08:49 +0000 Subject: [PATCH 2/2] Fix formatting --- Sources/NIOCore/ByteBuffer-aux.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/NIOCore/ByteBuffer-aux.swift b/Sources/NIOCore/ByteBuffer-aux.swift index 07f0aa77ad..232dffd874 100644 --- a/Sources/NIOCore/ByteBuffer-aux.swift +++ b/Sources/NIOCore/ByteBuffer-aux.swift @@ -206,7 +206,8 @@ extension ByteBuffer { return self.withUnsafeReadableBytes { pointer in assert(range.lowerBound >= 0 && (range.upperBound - range.lowerBound) <= pointer.count) return String( - decoding: UnsafeRawBufferPointer(rebasing: pointer[range]), as: Unicode.UTF8.self + decoding: UnsafeRawBufferPointer(rebasing: pointer[range]), + as: Unicode.UTF8.self ) } }