Add inlinability to ByteBuffer getters. (#1220)

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.
This commit is contained in:
Cory Benfield authored and Johannes Weiss committed 2019-11-04 19:31:26 +01:00
1 parent a0ab329d9d
commit fbe0c8a6b8
1 file changed
+5 -3
+5 -3
View File
@@ -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 {
@@ -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`.
@@ -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)
}