Skip to content

Commit

Permalink
Revert fastRebase implementation (#3014)
Browse files Browse the repository at this point in the history
Motivation:

`UnsafeRawBufferPointer.init(fastRebase:)` and
`UnsafeMutableRawBufferPointer.init(fastRebase:)` were shimmed into NIO
in #1696. The shim is no longer
necessary.

Modifications:

- Revert the use of the `fastRebase` inits to the native
`.init(rebasing:)`.

Result:

Use of native APIs instead.
  • Loading branch information
clintonpi authored Dec 3, 2024
1 parent 33b8a48 commit 876fbf6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 100 deletions.
13 changes: 8 additions & 5 deletions Sources/NIOCore/ByteBuffer-aux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -205,7 +205,10 @@ 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
)
}
}

Expand Down Expand Up @@ -329,7 +332,7 @@ extension ByteBuffer {
self.withVeryUnsafeMutableBytes { destCompleteStorage in
assert(destCompleteStorage.count >= index + allBytesCount)
let dest = destCompleteStorage[index..<index + allBytesCount]
dispatchData.copyBytes(to: .init(fastRebase: dest), count: dest.count)
dispatchData.copyBytes(to: .init(rebasing: dest), count: dest.count)
}
return allBytesCount
}
Expand All @@ -348,7 +351,7 @@ extension ByteBuffer {
return nil
}
return self.withUnsafeReadableBytes { pointer in
DispatchData(bytes: UnsafeRawBufferPointer(fastRebase: pointer[range]))
DispatchData(bytes: UnsafeRawBufferPointer(rebasing: pointer[range]))
}
}

Expand Down Expand Up @@ -497,7 +500,7 @@ extension ByteBuffer {
precondition(count >= 0, "Can't write fewer than 0 bytes")
self.reserveCapacity(index + count)
self.withVeryUnsafeMutableBytes { pointer in
let dest = UnsafeMutableRawBufferPointer(fastRebase: pointer[index..<index + count])
let dest = UnsafeMutableRawBufferPointer(rebasing: pointer[index..<index + count])
_ = dest.initializeMemory(as: UInt8.self, repeating: byte)
}
return count
Expand Down
16 changes: 9 additions & 7 deletions Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,9 @@ public struct ByteBuffer {

@inlinable
mutating func _setBytesAssumingUniqueBufferAccess(_ bytes: UnsafeRawBufferPointer, at index: _Index) {
let targetPtr = UnsafeMutableRawBufferPointer(fastRebase: self._slicedStorageBuffer.dropFirst(Int(index)))
let targetPtr = UnsafeMutableRawBufferPointer(
rebasing: self._slicedStorageBuffer.dropFirst(Int(index))
)
targetPtr.copyMemory(from: bytes)
}

Expand All @@ -515,7 +517,7 @@ public struct ByteBuffer {
func ensureCapacityAndReturnStorageBase(capacity: Int) -> UnsafeMutablePointer<UInt8> {
self._ensureAvailableCapacity(_Capacity(capacity), at: index)
let newBytesPtr = UnsafeMutableRawBufferPointer(
fastRebase: self._slicedStorageBuffer[Int(index)..<Int(index) + Int(capacity)]
rebasing: self._slicedStorageBuffer[Int(index)..<Int(index) + Int(capacity)]
)
return newBytesPtr.bindMemory(to: UInt8.self).baseAddress!
}
Expand Down Expand Up @@ -659,7 +661,7 @@ public struct ByteBuffer {
self._copyStorageAndRebaseIfNeeded()
// this is safe because we always know that readerIndex >= writerIndex
let range = Range<Int>(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
Expand All @@ -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.
Expand Down Expand Up @@ -748,7 +750,7 @@ public struct ByteBuffer {
public func withUnsafeReadableBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
// This is safe, writerIndex >= readerIndex
let range = Range<Int>(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
Expand All @@ -769,7 +771,7 @@ public struct ByteBuffer {
let storageReference: Unmanaged<AnyObject> = Unmanaged.passUnretained(self._storage)
// This is safe, writerIndex >= readerIndex
let range = Range<Int>(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`.
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOCore/ByteBuffer-int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
43 changes: 0 additions & 43 deletions Sources/NIOCore/PointerHelpers.swift

This file was deleted.

10 changes: 7 additions & 3 deletions Sources/NIOPosix/ControlMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
)
}

Expand Down Expand Up @@ -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")
Expand All @@ -342,7 +346,7 @@ struct UnsafeOutboundControlBytes {
if writePosition == 0 {
return UnsafeMutableRawBufferPointer(start: nil, count: 0)
}
return UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[0..<self.writePosition])
return UnsafeMutableRawBufferPointer(rebasing: self.controlBytes[0..<self.writePosition])
}

}
Expand Down
41 changes: 0 additions & 41 deletions Sources/NIOPosix/PointerHelpers.swift

This file was deleted.

0 comments on commit 876fbf6

Please sign in to comment.