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:
mustiikhalil
2025-11-15 00:06:52 +01:00
committed by GitHub
parent 599847236c
commit a62f45fed8
2 changed files with 11 additions and 32 deletions

View File

@@ -207,7 +207,9 @@ public struct FlatBufferBuilder {
len: size &+ (prefix ? size : 0) &+ FileIdLength,
alignment: _minAlignment)
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)
}
@@ -706,8 +708,9 @@ public struct FlatBufferBuilder {
let len = str.utf8.count
notNested()
preAlign(len: len &+ 1, type: UOffset.self)
_bb.fill(padding: 1)
_bb.push(string: str, len: len)
str.withCString { ptr in
_bb.writeBytes(ptr, len: len &+ 1)
}
push(element: UOffset(len))
return Offset(offset: _bb.size)
}