From 0dba63909fb2959994fec11c704c5d5ea45e8d83 Mon Sep 17 00:00:00 2001 From: mustiikhalil Date: Thu, 12 Mar 2020 22:13:03 +0300 Subject: [PATCH] Removes the inner loop in the endtable check written tables (#5803) --- swift/Sources/FlatBuffers/ByteBuffer.swift | 2 +- .../FlatBuffers/FlatBufferBuilder.swift | 22 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift index 0c52317883d..e468227c390 100644 --- a/swift/Sources/FlatBuffers/ByteBuffer.swift +++ b/swift/Sources/FlatBuffers/ByteBuffer.swift @@ -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 } diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index cf479fd6b51..8afa7596b9e 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -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 {