mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 22:56:27 +00:00
[Swift] Push contiguous bytes (#8157)
* Add version of push which takes ContiguousBytes * Ensure overloads aren't ambiguous * Add version of createVector * Add version of push which takes ContiguousBytes * Ensure overloads aren't ambiguous * Add version of createVector * Add similar conditional to other use of ContiguousBytes * Attempt CI fix * Use memcpy instead of copyMemory memcpy is faster in tests * Add testContiguousBytes * Add benchmarks * Add version of createVector * Add benchmarks * Update push to copy memory Since we don't care about endianness, we can simply memcpy the array of scalars * Remove function and benchmarks Since we don't care about endianness, a FixedWidthInteger version of createVector isn't needed * Improve naming * Add doc comment
This commit is contained in:
@@ -32,6 +32,20 @@ benchmark("100Strings") {
|
||||
}
|
||||
}
|
||||
|
||||
benchmark("100Bytes") {
|
||||
var fb = FlatBufferBuilder(initialSize: 1<<20)
|
||||
for _ in 0..<1_000_000 {
|
||||
_ = fb.createVector(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
benchmark("100Bytes-ContiguousBytes") {
|
||||
var fb = FlatBufferBuilder(initialSize: 1<<20)
|
||||
for _ in 0..<1_000_000 {
|
||||
_ = fb.createVector(bytes: bytes)
|
||||
}
|
||||
}
|
||||
|
||||
benchmark("FlatBufferBuilder.add") {
|
||||
var fb = FlatBufferBuilder(initialSize: 1024 * 1024 * 32)
|
||||
for _ in 0..<1_000_000 {
|
||||
@@ -73,6 +87,7 @@ benchmark("structs") {
|
||||
}
|
||||
|
||||
let str = (0...99).map { _ -> String in "x" }.joined()
|
||||
let bytes: [UInt8] = Array(repeating: 42, count: 100)
|
||||
|
||||
@usableFromInline
|
||||
struct AA: NativeStruct {
|
||||
|
||||
@@ -484,4 +484,21 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
#endif
|
||||
}
|
||||
|
||||
func testContiguousBytes() {
|
||||
let byteArray: [UInt8] = [3, 1, 4, 1, 5, 9]
|
||||
var fbb = FlatBufferBuilder(initialSize: 1)
|
||||
let name = fbb.create(string: "Frodo")
|
||||
let bytes = fbb.createVector(bytes: byteArray)
|
||||
let root = Monster.createMonster(
|
||||
&fbb,
|
||||
nameOffset: name,
|
||||
inventoryVectorOffset: bytes)
|
||||
fbb.finish(offset: root)
|
||||
var buffer = fbb.sizedBuffer
|
||||
let monster: Monster = getRoot(byteBuffer: &buffer)
|
||||
let values = monster.inventory
|
||||
|
||||
XCTAssertEqual(byteArray, values)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user