Skip to content

Commit 876fbf6

Browse files
authored
Revert fastRebase implementation (#3014)
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.
1 parent 33b8a48 commit 876fbf6

File tree

6 files changed

+25
-100
lines changed

6 files changed

+25
-100
lines changed

Sources/NIOCore/ByteBuffer-aux.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension ByteBuffer {
3939
// this is not technically correct because we shouldn't just bind
4040
// the memory to `UInt8` but it's not a real issue either and we
4141
// need to work around https://bugs.swift.org/browse/SR-9604
42-
[UInt8](UnsafeRawBufferPointer(fastRebase: ptr[range]).bindMemory(to: UInt8.self))
42+
[UInt8](UnsafeRawBufferPointer(rebasing: ptr[range]).bindMemory(to: UInt8.self))
4343
}
4444
}
4545

@@ -205,7 +205,10 @@ extension ByteBuffer {
205205
}
206206
return self.withUnsafeReadableBytes { pointer in
207207
assert(range.lowerBound >= 0 && (range.upperBound - range.lowerBound) <= pointer.count)
208-
return String(decoding: UnsafeRawBufferPointer(fastRebase: pointer[range]), as: Unicode.UTF8.self)
208+
return String(
209+
decoding: UnsafeRawBufferPointer(rebasing: pointer[range]),
210+
as: Unicode.UTF8.self
211+
)
209212
}
210213
}
211214

@@ -329,7 +332,7 @@ extension ByteBuffer {
329332
self.withVeryUnsafeMutableBytes { destCompleteStorage in
330333
assert(destCompleteStorage.count >= index + allBytesCount)
331334
let dest = destCompleteStorage[index..<index + allBytesCount]
332-
dispatchData.copyBytes(to: .init(fastRebase: dest), count: dest.count)
335+
dispatchData.copyBytes(to: .init(rebasing: dest), count: dest.count)
333336
}
334337
return allBytesCount
335338
}
@@ -348,7 +351,7 @@ extension ByteBuffer {
348351
return nil
349352
}
350353
return self.withUnsafeReadableBytes { pointer in
351-
DispatchData(bytes: UnsafeRawBufferPointer(fastRebase: pointer[range]))
354+
DispatchData(bytes: UnsafeRawBufferPointer(rebasing: pointer[range]))
352355
}
353356
}
354357

@@ -497,7 +500,7 @@ extension ByteBuffer {
497500
precondition(count >= 0, "Can't write fewer than 0 bytes")
498501
self.reserveCapacity(index + count)
499502
self.withVeryUnsafeMutableBytes { pointer in
500-
let dest = UnsafeMutableRawBufferPointer(fastRebase: pointer[index..<index + count])
503+
let dest = UnsafeMutableRawBufferPointer(rebasing: pointer[index..<index + count])
501504
_ = dest.initializeMemory(as: UInt8.self, repeating: byte)
502505
}
503506
return count

Sources/NIOCore/ByteBuffer-core.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,9 @@ public struct ByteBuffer {
503503

504504
@inlinable
505505
mutating func _setBytesAssumingUniqueBufferAccess(_ bytes: UnsafeRawBufferPointer, at index: _Index) {
506-
let targetPtr = UnsafeMutableRawBufferPointer(fastRebase: self._slicedStorageBuffer.dropFirst(Int(index)))
506+
let targetPtr = UnsafeMutableRawBufferPointer(
507+
rebasing: self._slicedStorageBuffer.dropFirst(Int(index))
508+
)
507509
targetPtr.copyMemory(from: bytes)
508510
}
509511

@@ -515,7 +517,7 @@ public struct ByteBuffer {
515517
func ensureCapacityAndReturnStorageBase(capacity: Int) -> UnsafeMutablePointer<UInt8> {
516518
self._ensureAvailableCapacity(_Capacity(capacity), at: index)
517519
let newBytesPtr = UnsafeMutableRawBufferPointer(
518-
fastRebase: self._slicedStorageBuffer[Int(index)..<Int(index) + Int(capacity)]
520+
rebasing: self._slicedStorageBuffer[Int(index)..<Int(index) + Int(capacity)]
519521
)
520522
return newBytesPtr.bindMemory(to: UInt8.self).baseAddress!
521523
}
@@ -659,7 +661,7 @@ public struct ByteBuffer {
659661
self._copyStorageAndRebaseIfNeeded()
660662
// this is safe because we always know that readerIndex >= writerIndex
661663
let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
662-
return try body(.init(fastRebase: self._slicedStorageBuffer[range]))
664+
return try body(.init(rebasing: self._slicedStorageBuffer[range]))
663665
}
664666

665667
/// Yields the bytes currently writable (`bytesWritable` = `capacity` - `writerIndex`). Before reading those bytes you must first
@@ -677,7 +679,7 @@ public struct ByteBuffer {
677679
_ body: (UnsafeMutableRawBufferPointer) throws -> T
678680
) rethrows -> T {
679681
self._copyStorageAndRebaseIfNeeded()
680-
return try body(.init(fastRebase: self._slicedStorageBuffer.dropFirst(self.writerIndex)))
682+
return try body(.init(rebasing: self._slicedStorageBuffer.dropFirst(self.writerIndex)))
681683
}
682684

683685
/// 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 {
748750
public func withUnsafeReadableBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
749751
// This is safe, writerIndex >= readerIndex
750752
let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
751-
return try body(.init(fastRebase: self._slicedStorageBuffer[range]))
753+
return try body(.init(rebasing: self._slicedStorageBuffer[range]))
752754
}
753755

754756
/// 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 {
769771
let storageReference: Unmanaged<AnyObject> = Unmanaged.passUnretained(self._storage)
770772
// This is safe, writerIndex >= readerIndex
771773
let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
772-
return try body(.init(fastRebase: self._slicedStorageBuffer[range]), storageReference)
774+
return try body(.init(rebasing: self._slicedStorageBuffer[range]), storageReference)
773775
}
774776

775777
/// See `withUnsafeReadableBytesWithStorageManagement` and `withVeryUnsafeBytes`.
@@ -1120,7 +1122,7 @@ extension ByteBuffer {
11201122
self._ensureAvailableCapacity(_Capacity(length), at: _toIndex(toIndex))
11211123
self.withVeryUnsafeMutableBytes { ptr in
11221124
let srcPtr = UnsafeRawBufferPointer(start: ptr.baseAddress!.advanced(by: fromIndex), count: length)
1123-
let targetPtr = UnsafeMutableRawBufferPointer(fastRebase: ptr.dropFirst(toIndex))
1125+
let targetPtr = UnsafeMutableRawBufferPointer(rebasing: ptr.dropFirst(toIndex))
11241126
targetPtr.copyMemory(from: srcPtr)
11251127
}
11261128

Sources/NIOCore/ByteBuffer-int.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension ByteBuffer {
6767
return self.withUnsafeReadableBytes { ptr in
6868
var value: T = 0
6969
withUnsafeMutableBytes(of: &value) { valuePtr in
70-
valuePtr.copyMemory(from: UnsafeRawBufferPointer(fastRebase: ptr[range]))
70+
valuePtr.copyMemory(from: UnsafeRawBufferPointer(rebasing: ptr[range]))
7171
}
7272
return _toEndianness(value: value, endianness: endianness)
7373
}

Sources/NIOCore/PointerHelpers.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

Sources/NIOPosix/ControlMessage.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ struct UnsafeControlMessageStorage: Collection {
7979
/// Get the part of the buffer for use with a message.
8080
public subscript(position: Int) -> UnsafeMutableRawBufferPointer {
8181
UnsafeMutableRawBufferPointer(
82-
fastRebase: self.buffer[(position * self.bytesPerMessage)..<((position + 1) * self.bytesPerMessage)]
82+
rebasing: self.buffer[
83+
(position * self.bytesPerMessage)..<((position + 1) * self.bytesPerMessage)
84+
]
8385
)
8486
}
8587

@@ -316,7 +318,9 @@ struct UnsafeOutboundControlBytes {
316318
type: CInt,
317319
payload: PayloadType
318320
) {
319-
let writableBuffer = UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[writePosition...])
321+
let writableBuffer = UnsafeMutableRawBufferPointer(
322+
rebasing: self.controlBytes[writePosition...]
323+
)
320324

321325
let requiredSize = NIOBSDSocketControlMessage.space(payloadSize: MemoryLayout.stride(ofValue: payload))
322326
precondition(writableBuffer.count >= requiredSize, "Insufficient size for cmsghdr and data")
@@ -342,7 +346,7 @@ struct UnsafeOutboundControlBytes {
342346
if writePosition == 0 {
343347
return UnsafeMutableRawBufferPointer(start: nil, count: 0)
344348
}
345-
return UnsafeMutableRawBufferPointer(fastRebase: self.controlBytes[0..<self.writePosition])
349+
return UnsafeMutableRawBufferPointer(rebasing: self.controlBytes[0..<self.writePosition])
346350
}
347351

348352
}

Sources/NIOPosix/PointerHelpers.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)