[Swift] Migrates struct write APIS to write directly to the buffer (#6093)

* Migrates struct write APIS to in place APIS

* Fixes indentation in grpc swift
This commit is contained in:
mustiikhalil
2020-09-17 17:10:59 +03:00
committed by GitHub
parent c75ae24293
commit 89435303b7
13 changed files with 317 additions and 475 deletions

View File

@@ -166,7 +166,7 @@ public struct ByteBuffer {
@usableFromInline mutating func push<T: Scalar>(elements: [T]) {
let size = elements.count &* MemoryLayout<T>.size
ensureSpace(size: size)
elements.lazy.reversed().forEach { (s) in
elements.reversed().forEach { (s) in
push(value: s, len: MemoryLayout.size(ofValue: s))
}
}
@@ -175,12 +175,32 @@ public struct ByteBuffer {
/// - Parameters:
/// - value: Pointer to the object in memory
/// - size: Size of Value being written to the buffer
@available(*, deprecated, message: "0.9.0 will be removing the following method. Regenerate the code")
@usableFromInline mutating func push(struct value: UnsafeMutableRawPointer, size: Int) {
ensureSpace(size: size)
memcpy(_storage.memory.advanced(by: writerIndex &- size), value, size)
defer { value.deallocate() }
_writerSize = _writerSize &+ size
}
/// Prepares the buffer to receive a struct of certian size.
/// The alignment of the memory is already handled since we already called preAlign
/// - Parameter size: size of the struct
@usableFromInline mutating func prepareBufferToReceiveStruct(of size: Int) {
ensureSpace(size: size)
_writerSize = _writerSize &+ size
}
/// Reverse the input direction to the buffer, since `FlatBuffers` uses a back to front, following method will take current `writerIndex`
/// and writes front to back into the buffer, respecting the padding & the alignment
/// - Parameters:
/// - value: value of type Scalar
/// - position: position relative to the `writerIndex`
/// - len: length of the value in terms of bytes
@usableFromInline mutating func reversePush<T: Scalar>(value: T, position: Int, len: Int) {
var v = value
memcpy(_storage.memory.advanced(by: writerIndex &+ position), &v, len)
}
/// Adds an object of type Scalar into the buffer
/// - Parameters:
@@ -201,7 +221,7 @@ public struct ByteBuffer {
if str.utf8.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil {
} else {
let utf8View = str.utf8
for c in utf8View.lazy.reversed() {
for c in utf8View.reversed() {
push(value: c, len: 1)
}
}
@@ -246,14 +266,11 @@ public struct ByteBuffer {
return size
}
/// Resizes the buffer size
/// - Parameter size: new size for the buffer
@usableFromInline mutating internal func resize(_ size: Int) {
/// pops the written VTable if it's already written into the buffer
/// - Parameter size: size of the `VTable`
@usableFromInline mutating internal func pop(_ size: Int) {
assert((_writerSize &- size) > 0, "New size should NOT be a negative number")
var zero: UInt8 = 0
for i in 0..<(_writerSize &- size) {
memcpy(_storage.memory.advanced(by: writerIndex &+ i), &zero, MemoryLayout<UInt8>.size)
}
memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size)
_writerSize = size
}

View File

@@ -187,7 +187,7 @@ public struct FlatBufferBuilder {
let vTableOff = Int(vTableOffset)
let space = _bb.capacity &- vTableOff
_bb.write(value: Int32(offset &- vTableOff), index: space, direct: true)
_bb.resize(_bb.capacity &- space)
_bb.pop(_bb.capacity &- space)
} else {
_bb.write(value: Int32(vt_use &- vTableOffset), index: Int(vTableOffset))
_vtables.append(_bb.size)
@@ -304,7 +304,7 @@ public struct FlatBufferBuilder {
mutating public func createVector<T: Enum>(_ elements: [T], size: Int) -> Offset<UOffset> {
let size = size
startVector(size, elementSize: T.byteSize)
for e in elements.lazy.reversed() {
for e in elements.reversed() {
_bb.push(value: e.value, len: T.byteSize)
}
return Offset(offset: endVector(len: size))
@@ -323,7 +323,7 @@ public struct FlatBufferBuilder {
/// - returns: Offset of the vector
mutating public func createVector<T>(ofOffsets offsets: [Offset<T>], len: Int) -> Offset<UOffset> {
startVector(len, elementSize: MemoryLayout<Offset<T>>.size)
for o in offsets.lazy.reversed() {
for o in offsets.reversed() {
push(element: o)
}
return Offset(offset: endVector(len: len))
@@ -347,14 +347,31 @@ public struct FlatBufferBuilder {
/// - structs: An array of UnsafeMutableRawPointer
/// - type: Type of the struct being written
/// - returns: Offset of the vector
@available(*, deprecated, message: "0.9.0 will be removing the following method. Regenerate the code")
mutating public func createVector<T: Readable>(structs: [UnsafeMutableRawPointer],
type: T.Type) -> Offset<UOffset> {
startVector(structs.count &* T.size, elementSize: T.alignment)
for i in structs.lazy.reversed() {
for i in structs.reversed() {
create(struct: i, type: T.self)
}
return Offset(offset: endVector(len: structs.count))
}
/// Starts a vector of struct that considers the size and alignment of the struct
/// - Parameters:
/// - count: number of elements to be written
/// - size: size of struct
/// - alignment: alignment of the struct
mutating public func startVectorOfStructs(count: Int, size: Int, alignment: Int) {
startVector(count &* size, elementSize: alignment)
}
/// Ends the vector of structs and writtens the current offset
/// - Parameter count: number of written elements
/// - Returns: Offset of type UOffset
mutating public func endVectorOfStructs(count: Int) -> Offset<UOffset> {
return Offset<UOffset>(offset: endVector(len: count))
}
// MARK: - Inserting Structs
@@ -363,6 +380,7 @@ public struct FlatBufferBuilder {
/// - s: Flatbuffer struct
/// - type: Type of the element to be serialized
/// - returns: Offset of the Object
@available(*, deprecated, message: "0.9.0 will be removing the following method. Regenerate the code")
@discardableResult
mutating public func create<T: Readable>(struct s: UnsafeMutableRawPointer,
type: T.Type) -> Offset<UOffset> {
@@ -372,6 +390,32 @@ public struct FlatBufferBuilder {
return Offset(offset: _bb.size)
}
/// prepares the ByteBuffer to receive a struct of size and alignment
/// - Parameters:
/// - size: size of written struct
/// - alignment: alignment of written struct
mutating public func createStructOf(size: Int, alignment: Int) {
preAlign(len: size, alignment: alignment)
_bb.prepareBufferToReceiveStruct(of: size)
}
/// Adds scalars front to back instead of the default behavior of the normal add
/// - Parameters:
/// - v: element of type Scalar
/// - postion: position relative to the `writerIndex`
mutating public func reverseAdd<T: Scalar>(v: T, postion: Int) {
_bb.reversePush(value: v,
position: postion,
len: MemoryLayout<T>.size)
}
/// Ends the struct and returns the current buffer size
/// - Returns: Offset of type UOffset
@discardableResult
public func endStruct() -> Offset<UOffset> {
return Offset(offset: _bb.size)
}
/// Adds the offset of a struct into the vTable
///
/// The function fatalErrors if we pass an offset that is out of range