Skip to content

Commit

Permalink
Removes the inner loop in the endtable check written tables (#5803)
Browse files Browse the repository at this point in the history
  • Loading branch information
mustiikhalil committed Mar 12, 2020
1 parent 0e3fdd0 commit 0dba639
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public final class ByteBuffer {
/// - size: Size of Value being written to the buffer
func push(struct value: UnsafeMutableRawPointer, size: Int) {
ensureSpace(size: UInt32(size))
_memory.advanced(by: writerIndex - size).copyMemory(from: value, byteCount: size)
memcpy(_memory.advanced(by: writerIndex - size), value, size)
defer { value.deallocate() }
_writerSize += size
}
Expand Down
22 changes: 10 additions & 12 deletions swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,17 @@ public final class FlatBufferBuilder {

var isAlreadyAdded: Int?

mainLoop: for table in _vtables {
let vt1 = _bb.capacity - Int(table)
let vt2 = _bb.writerIndex
let len = _bb.read(def: Int16.self, position: vt1)
guard len == _bb.read(def: Int16.self, position: vt2) else { break }
for i in stride(from: sizeofVoffset, to: Int(len), by: sizeofVoffset) {
let vt1ReadValue = _bb.read(def: Int16.self, position: vt1 + i)
let vt2ReadValue = _bb.read(def: Int16.self, position: vt2 + i)
if vt1ReadValue != vt2ReadValue {
break mainLoop
}
}
let vt2 = _bb.memory.advanced(by: _bb.writerIndex)
let len2 = vt2.load(fromByteOffset: 0, as: Int16.self)

for table in _vtables {
let position = _bb.capacity - Int(table)
let vt1 = _bb.memory.advanced(by: position)
let len1 = _bb.read(def: Int16.self, position: position)
if (len2 != len1 || 0 != memcmp(vt1, vt2, Int(len2))) { continue }

isAlreadyAdded = Int(table)
break
}

if let offset = isAlreadyAdded {
Expand Down

0 comments on commit 0dba639

Please sign in to comment.