mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 19:02:03 +00:00
Improves the performance of the string imp (#8772)
Improves the performance of the implementation in Swift by using withCString instead of the contigiousString
This commit is contained in:
@@ -207,7 +207,9 @@ public struct FlatBufferBuilder {
|
|||||||
len: size &+ (prefix ? size : 0) &+ FileIdLength,
|
len: size &+ (prefix ? size : 0) &+ FileIdLength,
|
||||||
alignment: _minAlignment)
|
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)
|
fileId.withCString { ptr in
|
||||||
|
_bb.writeBytes(ptr, len: 4)
|
||||||
|
}
|
||||||
finish(offset: offset, addPrefix: prefix)
|
finish(offset: offset, addPrefix: prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -706,8 +708,9 @@ public struct FlatBufferBuilder {
|
|||||||
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)
|
str.withCString { ptr in
|
||||||
_bb.push(string: str, len: len)
|
_bb.writeBytes(ptr, len: len &+ 1)
|
||||||
|
}
|
||||||
push(element: UOffset(len))
|
push(element: UOffset(len))
|
||||||
return Offset(offset: _bb.size)
|
return Offset(offset: _bb.size)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -192,41 +192,17 @@ struct _InternalByteBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a string to the buffer using swift.utf8 object
|
/// Adds a RawPointer into the buffer
|
||||||
/// - Parameter str: String that will be added to the buffer
|
/// - Parameter pointer: pointer to be copied into the buffer
|
||||||
/// - Parameter len: length of the string
|
/// - Parameter len: length of the data
|
||||||
@inline(__always)
|
@inline(__always)
|
||||||
@usableFromInline
|
mutating func writeBytes(_ ptr: UnsafeRawPointer, len: Int) {
|
||||||
mutating func push(string str: String, len: Int) {
|
|
||||||
ensureSpace(size: len)
|
ensureSpace(size: len)
|
||||||
if str.utf8
|
|
||||||
.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) !=
|
|
||||||
nil
|
|
||||||
{
|
|
||||||
} else {
|
|
||||||
let utf8View = str.utf8
|
|
||||||
for c in utf8View.reversed() {
|
|
||||||
push(value: c, len: 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Writes a string to Bytebuffer using UTF8View
|
|
||||||
/// - Parameters:
|
|
||||||
/// - bytes: Pointer to the view
|
|
||||||
/// - len: Size of string
|
|
||||||
@usableFromInline
|
|
||||||
@inline(__always)
|
|
||||||
mutating func push(
|
|
||||||
bytes: UnsafeBufferPointer<String.UTF8View.Element>,
|
|
||||||
len: Int) -> Bool
|
|
||||||
{
|
|
||||||
memcpy(
|
memcpy(
|
||||||
_storage.memory.advanced(by: writerIndex &- len),
|
_storage.memory.advanced(by: writerIndex &- len),
|
||||||
bytes.baseAddress!,
|
ptr,
|
||||||
len)
|
len)
|
||||||
_writerSize = _writerSize &+ len
|
_writerSize = _writerSize &+ len
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write stores an object into the buffer directly or indirectly.
|
/// Write stores an object into the buffer directly or indirectly.
|
||||||
|
|||||||
Reference in New Issue
Block a user