Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ByteBuffer: small & medium sized string copies over 10% faster #640

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions Sources/NIO/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ public struct ByteBuffer {
"called the slower set<S: Sequence> function even though \(S.self) is a ContiguousCollection")
func ensureCapacityAndReturnStorageBase(capacity: Int) -> UnsafeMutablePointer<UInt8> {
self._ensureAvailableCapacity(_Capacity(capacity), at: index)
let newBytesPtr = UnsafeMutableRawBufferPointer(rebasing: self._slicedStorageBuffer
.dropFirst(Int(index))
.prefix(capacity))
let newBytesPtr = UnsafeMutableRawBufferPointer(rebasing: self._slicedStorageBuffer[Int(index) ..< Int(index) + Int(capacity)])
return newBytesPtr.bindMemory(to: UInt8.self).baseAddress!
}
let underestimatedByteCount = bytes.underestimatedCount
Expand Down Expand Up @@ -538,9 +536,8 @@ public struct ByteBuffer {
@_inlineable
public mutating func withUnsafeMutableReadableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
self._copyStorageAndRebaseIfNeeded()
return try body(.init(rebasing: self._slicedStorageBuffer
.dropFirst(self.readerIndex)
.prefix(self.readableBytes)))
let readerIndex = self.readerIndex
return try body(.init(rebasing: self._slicedStorageBuffer[readerIndex ..< readerIndex + self.readableBytes]))
}

/// Yields the bytes currently writable (`bytesWritable` = `capacity` - `writerIndex`). Before reading those bytes you must first
Expand Down Expand Up @@ -585,9 +582,8 @@ public struct ByteBuffer {
/// - returns: The value returned by `fn`.
@_inlineable
public func withUnsafeReadableBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
return try body(.init(rebasing: self._slicedStorageBuffer
.dropFirst(self.readerIndex)
.prefix(self.readableBytes)))
let readerIndex = self.readerIndex
return try body(.init(rebasing: self._slicedStorageBuffer[readerIndex ..< readerIndex + self.readableBytes]))
}

/// Yields a buffer pointer containing this `ByteBuffer`'s readable bytes. You may hold a pointer to those bytes
Expand All @@ -604,9 +600,9 @@ public struct ByteBuffer {
@_inlineable
public func withUnsafeReadableBytesWithStorageManagement<T>(_ body: (UnsafeRawBufferPointer, Unmanaged<AnyObject>) throws -> T) rethrows -> T {
let storageReference: Unmanaged<AnyObject> = Unmanaged.passUnretained(self._storage)
return try body(.init(rebasing: self._slicedStorageBuffer
.dropFirst(self.readerIndex)
.prefix(self.readableBytes)), storageReference)
let readerIndex = self.readerIndex
return try body(.init(rebasing: self._slicedStorageBuffer[readerIndex ..< readerIndex + self.readableBytes]),
storageReference)
}

/// See `withUnsafeReadableBytesWithStorageManagement` and `withVeryUnsafeBytes`.
Expand Down