[Swift] Internal library improvements (#5965)

* Moves addition to overflow addition in swift by using &+

Moves code to use Int instead of UInt32 & fixes functions

Updates swift performance to great

Updated version to 0.5.2

Updated swift package version to 5.2

Updated docker to swift 5.2

Removed all none & arithmetic operations

* Small refactoring
This commit is contained in:
mustiikhalil
2020-06-18 20:14:38 +03:00
committed by GitHub
parent cfc7753a4c
commit 2e57d80b13
7 changed files with 71 additions and 80 deletions

View File

@@ -27,7 +27,7 @@ Reflection | Yes | No | No | No | No | No
Buffer verifier | Yes | No | No | No | No | No | No | Yes | No | No | No | No | No Buffer verifier | Yes | No | No | No | No | No | No | Yes | No | No | No | No | No
Testing: basic | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | Yes | Yes | Yes | Yes Testing: basic | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | Yes | Yes | Yes | Yes
Testing: fuzz | Yes | No | No | Yes | Yes | No | No | No | ? | No | No | Yes | No Testing: fuzz | Yes | No | No | Yes | Yes | No | No | No | ? | No | No | Yes | No
Performance: | Superb | Great | Great | Great | Ok | ? | ? | Superb | ? | ? | Great | Superb | ? Performance: | Superb | Great | Great | Great | Ok | ? | ? | Superb | ? | ? | Great | Superb | Great
Platform: Windows | VS2010 | Yes | Yes | ? | ? | ? | Yes | VS2010 | ? | Yes | Yes | Yes | No Platform: Windows | VS2010 | Yes | Yes | ? | ? | ? | Yes | VS2010 | ? | Yes | Yes | Yes | No
Platform: Linux | GCC282 | Yes | ? | Yes | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes | Yes Platform: Linux | GCC282 | Yes | ? | Yes | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes | Yes
Platform: OS X | Xcode4 | ? | ? | ? | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes | Yes Platform: OS X | Xcode4 | ? | ? | ? | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes | Yes

View File

@@ -1,6 +1,6 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'FlatBuffers' s.name = 'FlatBuffers'
s.version = '0.5.1' s.version = '0.5.2'
s.summary = 'FlatBuffers: Memory Efficient Serialization Library' s.summary = 'FlatBuffers: Memory Efficient Serialization Library'
s.description = "FlatBuffers is a cross platform serialization library architected for s.description = "FlatBuffers is a cross platform serialization library architected for

View File

@@ -1,4 +1,4 @@
// swift-tools-version:5.1 // swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package. // The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription

View File

@@ -25,14 +25,14 @@ public struct ByteBuffer {
} }
func initalize(for size: Int) { func initalize(for size: Int) {
memory.initializeMemory(as: UInt8.self, repeating: 0, count: size) memset(memory, 0, size)
} }
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer /// Reallocates the buffer incase the object to be written doesnt fit in the current buffer
/// - Parameter size: Size of the current object /// - Parameter size: Size of the current object
@usableFromInline internal func reallocate(_ size: UInt32, writerSize: Int, alignment: Int) { @usableFromInline internal func reallocate(_ size: Int, writerSize: Int, alignment: Int) {
let currentWritingIndex = capacity - writerSize let currentWritingIndex = capacity &- writerSize
while capacity <= writerSize + Int(size) { while capacity <= writerSize &+ size {
capacity = capacity << 1 capacity = capacity << 1
} }
@@ -40,8 +40,8 @@ public struct ByteBuffer {
capacity = capacity.convertToPowerofTwo capacity = capacity.convertToPowerofTwo
let newData = UnsafeMutableRawPointer.allocate(byteCount: capacity, alignment: alignment) let newData = UnsafeMutableRawPointer.allocate(byteCount: capacity, alignment: alignment)
memset(newData, 0, capacity - writerSize) memset(newData, 0, capacity &- writerSize)
memcpy(newData.advanced(by: capacity - writerSize), memory.advanced(by: currentWritingIndex), writerSize) memcpy(newData.advanced(by: capacity &- writerSize), memory.advanced(by: currentWritingIndex), writerSize)
memory.deallocate() memory.deallocate()
memory = newData memory = newData
} }
@@ -54,7 +54,7 @@ public struct ByteBuffer {
/// Aliginment of the current memory being written to the buffer /// Aliginment of the current memory being written to the buffer
internal var alignment = 1 internal var alignment = 1
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer /// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
internal var writerIndex: Int { return _storage.capacity - _writerSize } internal var writerIndex: Int { return _storage.capacity &- _writerSize }
/// Reader is the position of the current Writer Index (capacity - size) /// Reader is the position of the current Writer Index (capacity - size)
public var reader: Int { return writerIndex } public var reader: Int { return writerIndex }
@@ -135,16 +135,16 @@ public struct ByteBuffer {
/// Fills the buffer with padding by adding to the writersize /// Fills the buffer with padding by adding to the writersize
/// - Parameter padding: Amount of padding between two to be serialized objects /// - Parameter padding: Amount of padding between two to be serialized objects
@usableFromInline mutating func fill(padding: UInt32) { @usableFromInline mutating func fill(padding: Int) {
ensureSpace(size: padding) ensureSpace(size: padding)
_writerSize += (MemoryLayout<UInt8>.size * Int(padding)) _writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
} }
///Adds an array of type Scalar to the buffer memory ///Adds an array of type Scalar to the buffer memory
/// - Parameter elements: An array of Scalars /// - Parameter elements: An array of Scalars
@usableFromInline mutating func push<T: Scalar>(elements: [T]) { @usableFromInline mutating func push<T: Scalar>(elements: [T]) {
let size = elements.count * MemoryLayout<T>.size let size = elements.count &* MemoryLayout<T>.size
ensureSpace(size: UInt32(size)) ensureSpace(size: size)
elements.lazy.reversed().forEach { (s) in elements.lazy.reversed().forEach { (s) in
push(value: s, len: MemoryLayout.size(ofValue: s)) push(value: s, len: MemoryLayout.size(ofValue: s))
} }
@@ -153,8 +153,8 @@ public struct ByteBuffer {
///Adds an array of type Bool to the buffer memory ///Adds an array of type Bool to the buffer memory
/// - Parameter elements: An array of Bool /// - Parameter elements: An array of Bool
@usableFromInline mutating func push(elements: [Bool]) { @usableFromInline mutating func push(elements: [Bool]) {
let size = elements.count * MemoryLayout<Bool>.size let size = elements.count &* MemoryLayout<Bool>.size
ensureSpace(size: UInt32(size)) ensureSpace(size: size)
elements.lazy.reversed().forEach { (s) in elements.lazy.reversed().forEach { (s) in
push(value: s ? 1 : 0, len: MemoryLayout.size(ofValue: s)) push(value: s ? 1 : 0, len: MemoryLayout.size(ofValue: s))
} }
@@ -165,10 +165,10 @@ public struct ByteBuffer {
/// - value: Pointer to the object in memory /// - value: Pointer to the object in memory
/// - size: Size of Value being written to the buffer /// - size: Size of Value being written to the buffer
@usableFromInline mutating func push(struct value: UnsafeMutableRawPointer, size: Int) { @usableFromInline mutating func push(struct value: UnsafeMutableRawPointer, size: Int) {
ensureSpace(size: UInt32(size)) ensureSpace(size: size)
memcpy(_storage.memory.advanced(by: writerIndex - size), value, size) memcpy(_storage.memory.advanced(by: writerIndex &- size), value, size)
defer { value.deallocate() } defer { value.deallocate() }
_writerSize += size _writerSize = _writerSize &+ size
} }
/// Adds an object of type Scalar into the buffer /// Adds an object of type Scalar into the buffer
@@ -176,17 +176,17 @@ public struct ByteBuffer {
/// - value: Object that will be written to the buffer /// - value: Object that will be written to the buffer
/// - len: Offset to subtract from the WriterIndex /// - len: Offset to subtract from the WriterIndex
@usableFromInline mutating func push<T: Scalar>(value: T, len: Int) { @usableFromInline mutating func push<T: Scalar>(value: T, len: Int) {
ensureSpace(size: UInt32(len)) ensureSpace(size: len)
var v = value.convertedEndian var v = value
memcpy(_storage.memory.advanced(by: writerIndex - len), &v, len) memcpy(_storage.memory.advanced(by: writerIndex &- len), &v, len)
_writerSize += len _writerSize = _writerSize &+ len
} }
/// Adds a string to the buffer using swift.utf8 object /// Adds a string to the buffer using swift.utf8 object
/// - Parameter str: String that will be added to the buffer /// - Parameter str: String that will be added to the buffer
/// - Parameter len: length of the string /// - Parameter len: length of the string
@usableFromInline mutating func push(string str: String, len: Int) { @usableFromInline mutating func push(string str: String, len: Int) {
ensureSpace(size: UInt32(len)) ensureSpace(size: len)
if str.utf8.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil { if str.utf8.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil {
} else { } else {
let utf8View = str.utf8 let utf8View = str.utf8
@@ -201,8 +201,8 @@ public struct ByteBuffer {
/// - bytes: Pointer to the view /// - bytes: Pointer to the view
/// - len: Size of string /// - len: Size of string
@usableFromInline mutating internal func push(bytes: UnsafeBufferPointer<String.UTF8View.Element>, len: Int) -> Bool { @usableFromInline mutating internal func push(bytes: UnsafeBufferPointer<String.UTF8View.Element>, len: Int) -> Bool {
memcpy(_storage.memory.advanced(by: writerIndex - len), UnsafeRawPointer(bytes.baseAddress!), len) memcpy(_storage.memory.advanced(by: writerIndex &- len), UnsafeRawPointer(bytes.baseAddress!), len)
_writerSize += len _writerSize = _writerSize &+ len
return true return true
} }
@@ -217,7 +217,7 @@ public struct ByteBuffer {
func write<T>(value: T, index: Int, direct: Bool = false) { func write<T>(value: T, index: Int, direct: Bool = false) {
var index = index var index = index
if !direct { if !direct {
index = _storage.capacity - index index = _storage.capacity &- index
} }
_storage.memory.storeBytes(of: value, toByteOffset: index, as: T.self) _storage.memory.storeBytes(of: value, toByteOffset: index, as: T.self)
} }
@@ -225,8 +225,8 @@ public struct ByteBuffer {
/// Makes sure that buffer has enouch space for each of the objects that will be written into it /// Makes sure that buffer has enouch space for each of the objects that will be written into it
/// - Parameter size: size of object /// - Parameter size: size of object
@discardableResult @discardableResult
@usableFromInline mutating func ensureSpace(size: UInt32) -> UInt32 { @usableFromInline mutating func ensureSpace(size: Int) -> Int {
if Int(size) + _writerSize > _storage.capacity { if size &+ _writerSize > _storage.capacity {
_storage.reallocate(size, writerSize: _writerSize, alignment: alignment) _storage.reallocate(size, writerSize: _writerSize, alignment: alignment)
} }
assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes") assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes")
@@ -249,10 +249,10 @@ public struct ByteBuffer {
/// Resizes the buffer size /// Resizes the buffer size
/// - Parameter size: new size for the buffer /// - Parameter size: new size for the buffer
@usableFromInline mutating internal func resize(_ size: Int) { @usableFromInline mutating internal func resize(_ size: Int) {
assert((_writerSize - size) > 0) assert((_writerSize &- size) > 0)
var zero: UInt8 = 0 var zero: UInt8 = 0
for i in 0..<(_writerSize - size) { for i in 0..<(_writerSize &- size) {
memcpy(_storage.memory.advanced(by: writerIndex + i), &zero, MemoryLayout<UInt8>.size) memcpy(_storage.memory.advanced(by: writerIndex &+ i), &zero, MemoryLayout<UInt8>.size)
} }
_writerSize = size _writerSize = size
} }
@@ -292,7 +292,7 @@ public struct ByteBuffer {
/// Creates a new Flatbuffer object that's duplicated from the current one /// Creates a new Flatbuffer object that's duplicated from the current one
/// - Parameter removeBytes: the amount of bytes to remove from the current Size /// - Parameter removeBytes: the amount of bytes to remove from the current Size
public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer { public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer {
return ByteBuffer(memory: _storage.memory, count: _storage.capacity, removing: _writerSize - removeBytes) return ByteBuffer(memory: _storage.memory, count: _storage.capacity, removing: _writerSize &- removeBytes)
} }
} }

View File

@@ -29,8 +29,7 @@ extension Scalar where Self: FixedWidthInteger {
/// ///
/// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet. /// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet.
public var convertedEndian: NumericValue { public var convertedEndian: NumericValue {
if isLitteEndian { return self as! Self.NumericValue } return self as! Self.NumericValue
fatalError("This is not tested! please report an issue on the offical flatbuffers repo")
} }
} }
@@ -38,7 +37,6 @@ extension Double: Scalar {
public typealias NumericValue = UInt64 public typealias NumericValue = UInt64
public var convertedEndian: UInt64 { public var convertedEndian: UInt64 {
if isLitteEndian { return self.bitPattern }
return self.bitPattern.littleEndian return self.bitPattern.littleEndian
} }
} }
@@ -47,7 +45,6 @@ extension Float32: Scalar {
public typealias NumericValue = UInt32 public typealias NumericValue = UInt32
public var convertedEndian: UInt32 { public var convertedEndian: UInt32 {
if isLitteEndian { return self.bitPattern }
return self.bitPattern.littleEndian return self.bitPattern.littleEndian
} }
} }

View File

@@ -31,7 +31,7 @@ public struct FlatBufferBuilder {
public var data: Data { public var data: Data {
assert(finished, "Data shouldn't be called before finish()") assert(finished, "Data shouldn't be called before finish()")
return Data(bytes: _bb.memory.advanced(by: _bb.writerIndex), return Data(bytes: _bb.memory.advanced(by: _bb.writerIndex),
count: _bb.capacity - _bb.writerIndex) count: _bb.capacity &- _bb.writerIndex)
} }
/// Get's the fully sized buffer stored in memory /// Get's the fully sized buffer stored in memory
public var fullSizedByteArray: [UInt8] { public var fullSizedByteArray: [UInt8] {
@@ -41,7 +41,7 @@ public struct FlatBufferBuilder {
} }
/// Returns the written size of the buffer /// Returns the written size of the buffer
public var sizedByteArray: [UInt8] { public var sizedByteArray: [UInt8] {
let cp = _bb.capacity - _bb.writerIndex let cp = _bb.capacity &- _bb.writerIndex
let start = _bb.memory.advanced(by: _bb.writerIndex) let start = _bb.memory.advanced(by: _bb.writerIndex)
.bindMemory(to: UInt8.self, capacity: cp) .bindMemory(to: UInt8.self, capacity: cp)
@@ -90,9 +90,9 @@ public struct FlatBufferBuilder {
/// - fields: Array of all the important fields to be serialized /// - fields: Array of all the important fields to be serialized
mutating public func require(table: Offset<UOffset>, fields: [Int32]) { mutating public func require(table: Offset<UOffset>, fields: [Int32]) {
for field in fields { for field in fields {
let start = _bb.capacity - Int(table.o) let start = _bb.capacity &- Int(table.o)
let startTable = start - Int(_bb.read(def: Int32.self, position: start)) let startTable = start &- Int(_bb.read(def: Int32.self, position: start))
let isOkay = _bb.read(def: VOffset.self, position: startTable + Int(field)) != 0 let isOkay = _bb.read(def: VOffset.self, position: startTable &+ Int(field)) != 0
assert(isOkay, "Flatbuffers requires the following field") assert(isOkay, "Flatbuffers requires the following field")
} }
} }
@@ -104,7 +104,7 @@ public struct FlatBufferBuilder {
/// - prefix: if false it wont add the size of the buffer /// - prefix: if false it wont add the size of the buffer
mutating public func finish<T>(offset: Offset<T>, fileId: String, addPrefix prefix: Bool = false) { mutating public func finish<T>(offset: Offset<T>, fileId: String, addPrefix prefix: Bool = false) {
let size = MemoryLayout<UOffset>.size let size = MemoryLayout<UOffset>.size
preAlign(len: size + (prefix ? size : 0) + FileIdLength, alignment: _minAlignment) preAlign(len: size &+ (prefix ? size : 0) &+ FileIdLength, alignment: _minAlignment)
assert(fileId.count == FileIdLength, "Flatbuffers requires file id to be 4") assert(fileId.count == FileIdLength, "Flatbuffers requires file id to be 4")
_bb.push(string: fileId, len: 4) _bb.push(string: fileId, len: 4)
finish(offset: offset, addPrefix: prefix) finish(offset: offset, addPrefix: prefix)
@@ -117,7 +117,7 @@ public struct FlatBufferBuilder {
mutating public func finish<T>(offset: Offset<T>, addPrefix prefix: Bool = false) { mutating public func finish<T>(offset: Offset<T>, addPrefix prefix: Bool = false) {
notNested() notNested()
let size = MemoryLayout<UOffset>.size let size = MemoryLayout<UOffset>.size
preAlign(len: size + (prefix ? size : 0), alignment: _minAlignment) preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment)
push(element: refer(to: offset.o)) push(element: refer(to: offset.o))
if prefix { push(element: _bb.size) } if prefix { push(element: _bb.size) }
_vtableStorage.clear() _vtableStorage.clear()
@@ -147,21 +147,21 @@ public struct FlatBufferBuilder {
let sizeofVoffset = MemoryLayout<VOffset>.size let sizeofVoffset = MemoryLayout<VOffset>.size
let vTableOffset = push(element: SOffset(0)) let vTableOffset = push(element: SOffset(0))
let tableObjectSize = vTableOffset - startOffset let tableObjectSize = vTableOffset &- startOffset
assert(tableObjectSize < 0x10000, "Buffer can't grow beyond 2 Gigabytes") assert(tableObjectSize < 0x10000, "Buffer can't grow beyond 2 Gigabytes")
let _max = UInt32(_vtableStorage.maxOffset) + UInt32(sizeofVoffset) let _max = Int(_vtableStorage.maxOffset) &+ sizeofVoffset
_bb.fill(padding: _max) _bb.fill(padding: _max)
_bb.write(value: VOffset(tableObjectSize), index: _bb.writerIndex + sizeofVoffset, direct: true) _bb.write(value: VOffset(tableObjectSize), index: _bb.writerIndex &+ sizeofVoffset, direct: true)
_bb.write(value: VOffset(_max), index: _bb.writerIndex, direct: true) _bb.write(value: VOffset(_max), index: _bb.writerIndex, direct: true)
var itr = 0 var itr = 0
while itr < _vtableStorage.writtenIndex { while itr < _vtableStorage.writtenIndex {
let loaded = _vtableStorage.load(at: itr) let loaded = _vtableStorage.load(at: itr)
itr += _vtableStorage.size itr = itr &+ _vtableStorage.size
guard loaded.offset != 0 else { continue } guard loaded.offset != 0 else { continue }
let _index = (_bb.writerIndex + Int(loaded.position)) let _index = (_bb.writerIndex &+ Int(loaded.position))
_bb.write(value: VOffset(vTableOffset - loaded.offset), index: _index, direct: true) _bb.write(value: VOffset(vTableOffset &- loaded.offset), index: _index, direct: true)
} }
_vtableStorage.clear() _vtableStorage.clear()
@@ -173,7 +173,7 @@ public struct FlatBufferBuilder {
let len2 = vt2.load(fromByteOffset: 0, as: Int16.self) let len2 = vt2.load(fromByteOffset: 0, as: Int16.self)
for table in _vtables { for table in _vtables {
let position = _bb.capacity - Int(table) let position = _bb.capacity &- Int(table)
let vt1 = _bb.memory.advanced(by: position) let vt1 = _bb.memory.advanced(by: position)
let len1 = _bb.read(def: Int16.self, position: position) let len1 = _bb.read(def: Int16.self, position: position)
if (len2 != len1 || 0 != memcmp(vt1, vt2, Int(len2))) { continue } if (len2 != len1 || 0 != memcmp(vt1, vt2, Int(len2))) { continue }
@@ -184,11 +184,11 @@ public struct FlatBufferBuilder {
if let offset = isAlreadyAdded { if let offset = isAlreadyAdded {
let vTableOff = Int(vTableOffset) let vTableOff = Int(vTableOffset)
let space = _bb.capacity - vTableOff let space = _bb.capacity &- vTableOff
_bb.write(value: Int32(offset - vTableOff), index: space, direct: true) _bb.write(value: Int32(offset &- vTableOff), index: space, direct: true)
_bb.resize(_bb.capacity - space) _bb.resize(_bb.capacity &- space)
} else { } else {
_bb.write(value: Int32(vt_use) - Int32(vTableOffset), index: Int(vTableOffset)) _bb.write(value: Int32(vt_use &- vTableOffset), index: Int(vTableOffset))
_vtables.append(_bb.size) _vtables.append(_bb.size)
} }
isNested = false isNested = false
@@ -224,7 +224,7 @@ public struct FlatBufferBuilder {
/// - alignment: Alignment type /// - alignment: Alignment type
@usableFromInline mutating internal func preAlign(len: Int, alignment: Int) { @usableFromInline mutating internal func preAlign(len: Int, alignment: Int) {
minAlignment(size: alignment) minAlignment(size: alignment)
_bb.fill(padding: padding(bufSize: _bb.size + UOffset(len), elementSize: UOffset(alignment))) _bb.fill(padding: Int(padding(bufSize: _bb.size &+ UOffset(len), elementSize: UOffset(alignment))))
} }
/// Prealigns the buffer before writting a new object into the buffer /// Prealigns the buffer before writting a new object into the buffer
@@ -240,7 +240,7 @@ public struct FlatBufferBuilder {
@usableFromInline mutating internal func refer(to off: UOffset) -> UOffset { @usableFromInline mutating internal func refer(to off: UOffset) -> UOffset {
let size = MemoryLayout<UOffset>.size let size = MemoryLayout<UOffset>.size
preAlign(len: size, alignment: size) preAlign(len: size, alignment: size)
return _bb.size - off + UInt32(size) return _bb.size &- off &+ UInt32(size)
} }
/// Tracks the elements written into the buffer /// Tracks the elements written into the buffer
@@ -257,8 +257,8 @@ public struct FlatBufferBuilder {
mutating public func startVector(_ len: Int, elementSize: Int) { mutating public func startVector(_ len: Int, elementSize: Int) {
notNested() notNested()
isNested = true isNested = true
preAlign(len: len * elementSize, type: UOffset.self) preAlign(len: len &* elementSize, type: UOffset.self)
preAlign(len: len * elementSize, alignment: elementSize) preAlign(len: len &* elementSize, alignment: elementSize)
} }
/// Ends the vector of at length /// Ends the vector of at length
@@ -348,7 +348,7 @@ public struct FlatBufferBuilder {
/// - returns: Offset of the vector /// - returns: Offset of the vector
mutating public func createVector<T: Readable>(structs: [UnsafeMutableRawPointer], mutating public func createVector<T: Readable>(structs: [UnsafeMutableRawPointer],
type: T.Type) -> Offset<UOffset> { type: T.Type) -> Offset<UOffset> {
startVector(structs.count * T.size, elementSize: T.alignment) startVector(structs.count &* T.size, elementSize: T.alignment)
for i in structs.lazy.reversed() { for i in structs.lazy.reversed() {
create(struct: i, type: T.self) create(struct: i, type: T.self)
} }
@@ -387,7 +387,7 @@ public struct FlatBufferBuilder {
mutating public func create(string str: String) -> Offset<String> { mutating public func create(string str: String) -> Offset<String> {
let len = str.utf8.count let len = str.utf8.count
notNested() notNested()
preAlign(len: len + 1, type: UOffset.self) preAlign(len: len &+ 1, type: UOffset.self)
_bb.fill(padding: 1) _bb.fill(padding: 1)
_bb.push(string: str, len: len) _bb.push(string: str, len: len)
push(element: UOffset(len)) push(element: UOffset(len))
@@ -415,10 +415,7 @@ public struct FlatBufferBuilder {
/// - offset: Offset of another object to be written /// - offset: Offset of another object to be written
/// - position: The predefined position of the object /// - position: The predefined position of the object
mutating public func add<T>(offset: Offset<T>, at position: VOffset) { mutating public func add<T>(offset: Offset<T>, at position: VOffset) {
if offset.isEmpty { if offset.isEmpty { return }
track(offset: 0, at: position)
return
}
add(element: refer(to: offset.o), def: 0, at: position) add(element: refer(to: offset.o), def: 0, at: position)
} }
@@ -439,12 +436,8 @@ public struct FlatBufferBuilder {
/// - def: Default value for that element /// - def: Default value for that element
/// - position: The predefined position of the element /// - position: The predefined position of the element
mutating public func add<T: Scalar>(element: T, def: T, at position: VOffset) { mutating public func add<T: Scalar>(element: T, def: T, at position: VOffset) {
if (element == def && !serializeDefaults) { if (element == def && !serializeDefaults) { return }
track(offset: 0, at: position) track(offset: push(element: element), at: position)
return
}
let off = push(element: element)
track(offset: off, at: position)
} }
/// Adds Boolean values into the buffer /// Adds Boolean values into the buffer
@@ -467,9 +460,10 @@ public struct FlatBufferBuilder {
/// - returns: Postion of the Element /// - returns: Postion of the Element
@discardableResult @discardableResult
mutating public func push<T: Scalar>(element: T) -> UOffset { mutating public func push<T: Scalar>(element: T) -> UOffset {
preAlign(len: MemoryLayout<T>.size, let size = MemoryLayout<T>.size
alignment: MemoryLayout<T>.size) preAlign(len: size,
_bb.push(value: element, len: MemoryLayout<T>.size) alignment: size)
_bb.push(value: element, len: size)
return _bb.size return _bb.size
} }
@@ -505,7 +499,7 @@ extension FlatBufferBuilder: CustomDebugStringConvertible {
/// Last written Index /// Last written Index
var writtenIndex: Int = 0 var writtenIndex: Int = 0
/// the amount of added elements into the buffer /// the amount of added elements into the buffer
var addedElements: Int { return capacity - (numOfFields * size) } var addedElements: Int { return capacity - (numOfFields &* size) }
/// Creates the memory to store the buffer in /// Creates the memory to store the buffer in
init() { init() {
@@ -519,7 +513,7 @@ extension FlatBufferBuilder: CustomDebugStringConvertible {
/// Builds a buffer with byte count of fieldloc.size * count of field numbers /// Builds a buffer with byte count of fieldloc.size * count of field numbers
/// - Parameter count: number of fields to be written /// - Parameter count: number of fields to be written
func start(count: Int) { func start(count: Int) {
let capacity = count * size let capacity = count &* size
ensure(space: capacity) ensure(space: capacity)
} }
@@ -528,8 +522,8 @@ extension FlatBufferBuilder: CustomDebugStringConvertible {
/// - Parameter loc: Location of encoded element /// - Parameter loc: Location of encoded element
func add(loc: FieldLoc) { func add(loc: FieldLoc) {
memory.baseAddress?.advanced(by: writtenIndex).storeBytes(of: loc, as: FieldLoc.self) memory.baseAddress?.advanced(by: writtenIndex).storeBytes(of: loc, as: FieldLoc.self)
writtenIndex += size writtenIndex = writtenIndex &+ size
numOfFields += 1 numOfFields = numOfFields &+ 1
maxOffset = max(loc.position, maxOffset) maxOffset = max(loc.position, maxOffset)
} }
@@ -543,7 +537,7 @@ extension FlatBufferBuilder: CustomDebugStringConvertible {
/// Ensure that the buffer has enough space instead of recreating the buffer each time. /// Ensure that the buffer has enough space instead of recreating the buffer each time.
/// - Parameter space: space required for the new vtable /// - Parameter space: space required for the new vtable
func ensure(space: Int) { func ensure(space: Int) {
guard space + writtenIndex > capacity else { return } guard space &+ writtenIndex > capacity else { return }
memory.deallocate() memory.deallocate()
memory = UnsafeMutableRawBufferPointer.allocate(byteCount: space, alignment: size) memory = UnsafeMutableRawBufferPointer.allocate(byteCount: space, alignment: size)
capacity = space capacity = space

View File

@@ -1,4 +1,4 @@
FROM swift:5.1 FROM swift:5.2
WORKDIR /code WORKDIR /code
ADD . . ADD . .
RUN cp flatc_debian_stretch flatc RUN cp flatc_debian_stretch flatc