Skip to content

Commit fbe0c8a

Browse files
Lukasaweissi
authored andcommitted
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.
1 parent a0ab329 commit fbe0c8a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Sources/NIO/ByteBuffer-core.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension _ByteBufferSlice: Equatable {}
2525
struct _ByteBufferSlice {
2626
@usableFromInline var upperBound: ByteBuffer._Index
2727
@usableFromInline var _begin: _UInt24
28-
@usableFromInline var lowerBound: ByteBuffer._Index {
28+
@inlinable var lowerBound: ByteBuffer._Index {
2929
return UInt32(self._begin)
3030
}
3131
@inlinable var count: Int {
@@ -412,10 +412,10 @@ public struct ByteBuffer {
412412

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

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

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

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

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

0 commit comments

Comments
 (0)