Skip to content

Commit

Permalink
remove unnecessary UInt8 pointer binds (#524)
Browse files Browse the repository at this point in the history
Motivation:

The bytes that a Unsafe(Mutable)RawBufferPointers points to can be
accessed directly as `UInt8`s. We didn't know that so we bind
`ByteBuffer`s memory to `UInt8` so that we can access it as
`UnsafePointer<UInt8>` using `assumingMemoryBound(to: UInt8.self)` in
many places. Most of the time that is unnecessarily awkward and
unnecessary.

Modifications:

- remove some easy instances where we access an
  `UnsafeMutableBufferPointer` as `UnsafeBufferPointer<UInt8>` just to
  read some `UInt8`s.

Result:

less awkward looking code
  • Loading branch information
weissi authored and Lukasa committed Jul 23, 2018
1 parent e56d1cc commit 8c5eee8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
6 changes: 2 additions & 4 deletions Sources/NIO/ByteBuffer-aux.swift
Expand Up @@ -30,8 +30,7 @@ extension ByteBuffer {
}

return self.withVeryUnsafeBytes { ptr in
Array.init(UnsafeBufferPointer<UInt8>(start: ptr.baseAddress?.advanced(by: index).assumingMemoryBound(to: UInt8.self),
count: length))
Array<UInt8>(ptr[index..<(index+length)])
}
}

Expand Down Expand Up @@ -118,8 +117,7 @@ extension ByteBuffer {
guard index <= pointer.count - length else {
return nil
}
return String(decoding: UnsafeBufferPointer(start: pointer.baseAddress?.assumingMemoryBound(to: UInt8.self).advanced(by: index), count: length),
as: UTF8.self)
return String(decoding: pointer[index..<(index+length)], as: UTF8.self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NIO/NonBlockingFileIO.swift
Expand Up @@ -192,7 +192,7 @@ public struct NonBlockingFileIO {
let n = try buf.writeWithUnsafeMutableBytes { ptr in
let res = try fileHandle.withUnsafeFileDescriptor { descriptor in
try Posix.read(descriptor: descriptor,
pointer: ptr.baseAddress!.assumingMemoryBound(to: UInt8.self),
pointer: ptr.baseAddress!,
size: byteCount - bytesRead)
}
switch res {
Expand Down
8 changes: 4 additions & 4 deletions Sources/NIO/System.swift
Expand Up @@ -314,7 +314,7 @@ internal enum Posix {
}

@inline(never)
public static func write(descriptor: CInt, pointer: UnsafePointer<UInt8>, size: Int) throws -> IOResult<Int> {
public static func write(descriptor: CInt, pointer: UnsafeRawPointer, size: Int) throws -> IOResult<Int> {
return try wrapSyscallMayBlock {
sysWrite(descriptor, pointer, size)
}
Expand All @@ -328,22 +328,22 @@ internal enum Posix {
}

@inline(never)
public static func sendto(descriptor: CInt, pointer: UnsafePointer<UInt8>, size: size_t,
public static func sendto(descriptor: CInt, pointer: UnsafeRawPointer, size: size_t,
destinationPtr: UnsafePointer<sockaddr>, destinationSize: socklen_t) throws -> IOResult<Int> {
return try wrapSyscallMayBlock {
sysSendTo(descriptor, pointer, size, 0, destinationPtr, destinationSize)
}
}

@inline(never)
public static func read(descriptor: CInt, pointer: UnsafeMutablePointer<UInt8>, size: size_t) throws -> IOResult<Int> {
public static func read(descriptor: CInt, pointer: UnsafeMutableRawPointer, size: size_t) throws -> IOResult<Int> {
return try wrapSyscallMayBlock {
sysRead(descriptor, pointer, size)
}
}

@inline(never)
public static func recvfrom(descriptor: CInt, pointer: UnsafeMutablePointer<UInt8>, len: size_t, addr: UnsafeMutablePointer<sockaddr>, addrlen: UnsafeMutablePointer<socklen_t>) throws -> IOResult<ssize_t> {
public static func recvfrom(descriptor: CInt, pointer: UnsafeMutableRawPointer, len: size_t, addr: UnsafeMutablePointer<sockaddr>, addrlen: UnsafeMutablePointer<socklen_t>) throws -> IOResult<ssize_t> {
return try wrapSyscallMayBlock {
sysRecvFrom(descriptor, pointer, len, 0, addr, addrlen)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/NIOTests/ByteBufferTest.swift
Expand Up @@ -472,7 +472,7 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual(string.utf8.count, ptr.count)

for (idx, expected) in zip(0..<string.utf8.count, string.utf8) {
let actual = ptr.baseAddress!.advanced(by: idx).assumingMemoryBound(to: UInt8.self).pointee
let actual = ptr[idx]
XCTAssertEqual(expected, actual, "character at index \(idx) is \(actual) but should be \(expected)")
}
}
Expand Down Expand Up @@ -636,7 +636,7 @@ class ByteBufferTest: XCTestCase {
otherBuf?.withUnsafeReadableBytes { ptr in
XCTAssertEqual(cap, ptr.count)
for i in 0..<cap {
XCTAssertEqual(ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)[i], UInt8(truncatingIfNeeded: i))
XCTAssertEqual(ptr[i], UInt8(truncatingIfNeeded: i))
}
}
}
Expand Down Expand Up @@ -675,13 +675,13 @@ class ByteBufferTest: XCTestCase {
buf!.withUnsafeReadableBytes { ptr in
XCTAssertEqual(cap, ptr.count)
for i in 0..<cap {
XCTAssertEqual(ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)[i], 0)
XCTAssertEqual(ptr[i], 0)
}
}
otherBuf!.withUnsafeReadableBytes { ptr in
XCTAssertEqual(cap, ptr.count)
for i in 0..<cap {
XCTAssertEqual(ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)[i], UInt8(truncatingIfNeeded: i))
XCTAssertEqual(ptr[i], UInt8(truncatingIfNeeded: i))
}
}
}
Expand Down

0 comments on commit 8c5eee8

Please sign in to comment.