Skip to content

Commit

Permalink
Add inlinability to ByteBuffer getters. (#1220)
Browse files Browse the repository at this point in the history
Motivation:

In complex ByteBuffer parsing code a surprising amount of cost comes
from retain/release operations that are emitted by the compiler around
calls to readerIndex/writerIndex, readableBytes/writableBytes, and
ByteBufferSlice._lowerBound. These are all straightforward computed
properties based on usableFromInline operations, and so they can be made
inlinable.

Modifications:

Joannis' sample benchmark runtime is halved by eliminating
retains/releases.

Result:

Better, faster parsing code.
  • Loading branch information
Lukasa authored and weissi committed Nov 4, 2019
1 parent a0ab329 commit fbe0c8a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Sources/NIO/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension _ByteBufferSlice: Equatable {}
struct _ByteBufferSlice {
@usableFromInline var upperBound: ByteBuffer._Index
@usableFromInline var _begin: _UInt24
@usableFromInline var lowerBound: ByteBuffer._Index {
@inlinable var lowerBound: ByteBuffer._Index {
return UInt32(self._begin)
}
@inlinable var count: Int {
Expand Down Expand Up @@ -412,10 +412,10 @@ public struct ByteBuffer {

/// The number of bytes writable until `ByteBuffer` will need to grow its underlying storage which will likely
/// trigger a copy of the bytes.
public var writableBytes: Int { return Int(_toCapacity(self._slice.count) - self._writerIndex) }
@inlinable public var writableBytes: Int { return Int(_toCapacity(self._slice.count) - self._writerIndex) }

/// The number of bytes readable (`readableBytes` = `writerIndex` - `readerIndex`).
public var readableBytes: Int { return Int(self._writerIndex - self._readerIndex) }
@inlinable public var readableBytes: Int { return Int(self._writerIndex - self._readerIndex) }

/// The current capacity of the storage of this `ByteBuffer`, this is not constant and does _not_ signify the number
/// of bytes that have been written to this `ByteBuffer`.
Expand Down Expand Up @@ -643,12 +643,14 @@ public struct ByteBuffer {

/// The reader index or the number of bytes previously read from this `ByteBuffer`. `readerIndex` is `0` for a
/// newly allocated `ByteBuffer`.
@inlinable
public var readerIndex: Int {
return Int(self._readerIndex)
}

/// The write index or the number of bytes previously written to this `ByteBuffer`. `writerIndex` is `0` for a
/// newly allocated `ByteBuffer`.
@inlinable
public var writerIndex: Int {
return Int(self._writerIndex)
}
Expand Down

0 comments on commit fbe0c8a

Please sign in to comment.