Skip to content

Commit

Permalink
Disables redendent return because of buildkite throwing errors for now
Browse files Browse the repository at this point in the history
Using overflow operators within swift to improve performance

Adds tests for GRPC message creation from a retained _InternalByteBuffer
  • Loading branch information
mustiikhalil committed Jan 17, 2025
1 parent 18e6a6e commit bd409d4
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 138 deletions.
2 changes: 1 addition & 1 deletion swift.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
--typeattributes prev-line # wrapAttributes

# rules
--rules wrap,todos,anyObjectProtocol,redundantParens,redundantReturn,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader
--rules wrap,todos,anyObjectProtocol,redundantParens,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader
--disable trailingclosures

--exclude **/*_generated.swift
Expand Down
69 changes: 42 additions & 27 deletions swift/Sources/FlatBuffers/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public struct ByteBuffer {

var isOwned: Bool {
switch self {
case .writePointer: true
default: false
case .writePointer: return true
default: return false
}
}
}
Expand Down Expand Up @@ -102,17 +102,19 @@ public struct ByteBuffer {
{
switch retainedBlob {
case .byteBuffer(let byteBuffer):
try byteBuffer.withUnsafeBytes(body)
return try byteBuffer.withUnsafeBytes(body)
#if !os(WASI)
case .data(let data):
try data.withUnsafeBytes(body)
return try data.withUnsafeBytes(body)
case .bytes(let contiguousBytes):
try contiguousBytes.withUnsafeBytes(body)
return try contiguousBytes.withUnsafeBytes(body)
#endif
case .array(let array):
try array.withUnsafeBytes(body)
return try array.withUnsafeBytes(body)
case .pointer(let ptr):
try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
return try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
case .writePointer(let ptr):
try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
return try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
}
}

Expand All @@ -123,26 +125,28 @@ public struct ByteBuffer {
{
switch retainedBlob {
case .byteBuffer(let byteBuffer):
try byteBuffer.withUnsafeRawPointer(body)
return try byteBuffer.withUnsafeRawPointer(body)
#if !os(WASI)
case .data(let data):
try data
return try data
.withUnsafeBytes {
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
}
case .bytes(let contiguousBytes):
try contiguousBytes
return try contiguousBytes
.withUnsafeBytes {
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
}
#endif
case .array(let array):
try array
return try array
.withUnsafeBytes {
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
}
case .pointer(let ptr):
try body(ptr)
return try body(ptr)
case .writePointer(let ptr):
try body(ptr)
return try body(ptr)
}
}

Expand All @@ -153,23 +157,25 @@ public struct ByteBuffer {
{
switch retainedBlob {
case .byteBuffer(let byteBuffer):
try byteBuffer.readWithUnsafeRawPointer(position: position, body)
return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
#if !os(WASI)
case .data(let data):
try data.withUnsafeBytes {
return try data.withUnsafeBytes {
try body($0.baseAddress!.advanced(by: position))
}
case .bytes(let contiguousBytes):
try contiguousBytes.withUnsafeBytes {
return try contiguousBytes.withUnsafeBytes {
try body($0.baseAddress!.advanced(by: position))
}
#endif
case .array(let array):
try array.withUnsafeBytes {
return try array.withUnsafeBytes {
try body($0.baseAddress!.advanced(by: position))
}
case .pointer(let ptr):
try body(ptr.advanced(by: position))
return try body(ptr.advanced(by: position))
case .writePointer(let ptr):
try body(ptr.advanced(by: position))
return try body(ptr.advanced(by: position))
}
}
}
Expand All @@ -178,10 +184,8 @@ public struct ByteBuffer {

/// The size of the elements written to the buffer + their paddings
private var _readerIndex: Int = 0
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
var writerIndex: Int { _storage.capacity &- _readerIndex }
/// Reader is the position of the current Writer Index (capacity - size)
public var reader: Int { writerIndex }
public var reader: Int { _storage.capacity &- _readerIndex }
/// Current size of the buffer
public var size: UOffset { UOffset(_readerIndex) }
/// Current capacity for the buffer
Expand All @@ -192,7 +196,9 @@ public struct ByteBuffer {
/// - bytes: Array of UInt8
@inline(__always)
init(byteBuffer: _InternalByteBuffer) {
_storage = Storage(blob: .byteBuffer(byteBuffer), capacity: byteBuffer.capacity)
_storage = Storage(
blob: .byteBuffer(byteBuffer),
capacity: byteBuffer.capacity)
_readerIndex = Int(byteBuffer.size)
}

Expand Down Expand Up @@ -392,7 +398,7 @@ public struct ByteBuffer {
assert(
index + count <= _storage.capacity,
"Reading out of bounds is illegal")
return _storage.retainedBlob.readWithUnsafeRawPointer(position: index) {
return _storage.readWithUnsafeRawPointer(position: index) {
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
}
}
Expand Down Expand Up @@ -433,14 +439,22 @@ public struct ByteBuffer {
}

@discardableResult
@usableFromInline
@inline(__always)
func withUnsafeMutableRawPointer<T>(
body: (UnsafeMutableRawPointer) throws
-> T) rethrows -> T
{
try _storage.withUnsafeRawPointer(body)
}

@discardableResult
@inline(__always)
func readWithUnsafeRawPointer<T>(
position: Int,
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
{
try _storage.readWithUnsafeRawPointer(position: position, body)
}
}

extension ByteBuffer: CustomDebugStringConvertible {
Expand All @@ -449,7 +463,8 @@ extension ByteBuffer: CustomDebugStringConvertible {
"""
buffer located at: \(_storage.retainedBlob),
with capacity of \(_storage.capacity),
{ writerSize: \(_readerIndex), readerSize: \(reader), writerIndex: \(writerIndex) }
{ writtenSize: \(_readerIndex), readerSize: \(reader),
size: \(size) }
"""
}
}
96 changes: 48 additions & 48 deletions swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ public struct FlatBufferBuilder {
public var capacity: Int { _bb.capacity }

#if !os(WASI)
/// Data representation of the buffer
///
/// Should only be used after ``finish(offset:addPrefix:)`` is called
public var data: Data {
assert(finished, "Data shouldn't be called before finish()")
return _bb.withUnsafeSlicedBytes { ptr in
var data = Data()
data.append(
ptr.baseAddress!.bindMemory(
to: UInt8.self,
capacity: _bb.capacity),
count: _bb.capacity)
return data
}
/// Data representation of the buffer
///
/// Should only be used after ``finish(offset:addPrefix:)`` is called
public var data: Data {
assert(finished, "Data shouldn't be called before finish()")
return _bb.withUnsafeSlicedBytes { ptr in
var data = Data()
data.append(
ptr.baseAddress!.bindMemory(
to: UInt8.self,
capacity: _bb.capacity),
count: _bb.capacity)
return data
}
}
#endif

/// Returns the underlying bytes in the ``ByteBuffer``
Expand Down Expand Up @@ -110,7 +110,7 @@ public struct FlatBufferBuilder {
public var sizedBuffer: ByteBuffer {
assert(finished, "Data shouldn't be called before finish()")
return _bb.withUnsafeSlicedBytes { ptr in
return ByteBuffer(
ByteBuffer(
copyingMemoryBound: ptr.baseAddress!,
capacity: ptr.count)
}
Expand All @@ -128,8 +128,8 @@ public struct FlatBufferBuilder {
/// however the builder can be force by passing true for `serializeDefaults`
public init(
initialSize: Int32 = 1024,
serializeDefaults force: Bool = false
) {
serializeDefaults force: Bool = false)
{
assert(initialSize > 0, "Size should be greater than zero!")
guard isLitteEndian else {
fatalError(
Expand Down Expand Up @@ -196,8 +196,8 @@ public struct FlatBufferBuilder {
mutating public func finish(
offset: Offset,
fileId: String,
addPrefix prefix: Bool = false
) {
addPrefix prefix: Bool = false)
{
let size = MemoryLayout<UOffset>.size
preAlign(
len: size &+ (prefix ? size : 0) &+ FileIdLength,
Expand Down Expand Up @@ -226,8 +226,8 @@ public struct FlatBufferBuilder {
/// include the size of the current buffer.
mutating public func finish(
offset: Offset,
addPrefix prefix: Bool = false
) {
addPrefix prefix: Bool = false)
{
notNested()
let size = MemoryLayout<UOffset>.size
preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment)
Expand Down Expand Up @@ -351,8 +351,8 @@ public struct FlatBufferBuilder {
@usableFromInline
mutating internal func padding(
bufSize: UInt32,
elementSize: UInt32
) -> UInt32 {
elementSize: UInt32) -> UInt32
{
((~bufSize) &+ 1) & (elementSize &- 1)
}

Expand Down Expand Up @@ -479,29 +479,29 @@ public struct FlatBufferBuilder {
@inline(__always)
mutating public func createVector<T: Scalar>(
_ elements: [T],
size: Int
) -> Offset {
size: Int) -> Offset
{
let size = size
startVector(size, elementSize: MemoryLayout<T>.size)
_bb.push(elements: elements)
return endVector(len: size)
}

#if swift(>=5.0) && !os(WASI)
@inline(__always)
/// Creates a vector of bytes in the buffer.
///
/// Allows creating a vector from `Data` without copying to a `[UInt8]`
///
/// - Parameter bytes: bytes to be written into the buffer
/// - Returns: ``Offset`` of the vector
mutating public func createVector(bytes: ContiguousBytes) -> Offset {
bytes.withUnsafeBytes {
startVector($0.count, elementSize: MemoryLayout<UInt8>.size)
_bb.push(bytes: $0)
return endVector(len: $0.count)
}
@inline(__always)
/// Creates a vector of bytes in the buffer.
///
/// Allows creating a vector from `Data` without copying to a `[UInt8]`
///
/// - Parameter bytes: bytes to be written into the buffer
/// - Returns: ``Offset`` of the vector
mutating public func createVector(bytes: ContiguousBytes) -> Offset {
bytes.withUnsafeBytes {
startVector($0.count, elementSize: MemoryLayout<UInt8>.size)
_bb.push(bytes: $0)
return endVector(len: $0.count)
}
}
#endif

/// Creates a vector of type ``Enum`` into the ``ByteBuffer``
Expand Down Expand Up @@ -539,8 +539,8 @@ public struct FlatBufferBuilder {
@inline(__always)
mutating public func createVector<T: Enum>(
_ elements: [T],
size: Int
) -> Offset {
size: Int) -> Offset
{
let size = size
startVector(size, elementSize: T.byteSize)
for index in stride(from: elements.count, to: 0, by: -1) {
Expand Down Expand Up @@ -585,8 +585,8 @@ public struct FlatBufferBuilder {
@inline(__always)
mutating public func createVector(
ofOffsets offsets: [Offset],
len: Int
) -> Offset {
len: Int) -> Offset
{
startVector(len, elementSize: MemoryLayout<Offset>.size)
for index in stride(from: offsets.count, to: 0, by: -1) {
push(element: offsets[index &- 1])
Expand Down Expand Up @@ -662,8 +662,8 @@ public struct FlatBufferBuilder {
@inline(__always)
@discardableResult
mutating public func create<T: NativeStruct>(
struct s: T, position: VOffset
) -> Offset {
struct s: T, position: VOffset) -> Offset
{
let offset = create(struct: s)
_vtableStorage.add(
loc: (offset: _bb.size, position: VOffset(position)))
Expand All @@ -687,8 +687,8 @@ public struct FlatBufferBuilder {
@inline(__always)
@discardableResult
mutating public func create<T: NativeStruct>(
struct s: T
) -> Offset {
struct s: T) -> Offset
{
let size = MemoryLayout<T>.size
preAlign(len: size, alignment: MemoryLayout<T>.alignment)
_bb.push(struct: s, size: size)
Expand Down Expand Up @@ -803,8 +803,8 @@ public struct FlatBufferBuilder {
mutating public func add<T: Scalar>(
element: T,
def: T,
at position: VOffset
) {
at position: VOffset)
{
if element == def && !serializeDefaults { return }
track(offset: push(element: element), at: position)
}
Expand Down
6 changes: 4 additions & 2 deletions swift/Sources/FlatBuffers/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
public var object: T {
T.init(
buffer,
o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) +
o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) &+
Int32(buffer.reader))
}

Expand Down Expand Up @@ -68,6 +68,8 @@ public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
_ body: (UnsafeRawBufferPointer) throws
-> T) rethrows -> T
{
try buffer.withUnsafeBytes { try body($0) }
return try buffer.readWithUnsafeRawPointer(position: buffer.reader) {
try body(UnsafeRawBufferPointer(start: $0, count: size))
}
}
}
Loading

0 comments on commit bd409d4

Please sign in to comment.