mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 19:58:17 +00:00
[Swift] Adds new API to reduce memory copying within swift (#8484)
* Adds new API to reduce memory copying within swift Adds new storage container _InternalByteBuffer which will be holding the data that will be created within the swift lib, however reading data will be redirected to ByteBuffer, which should be able to handle all types of data that swift provide without the need to copy the data itself. This is due to holding a reference to the data. Replaces assumingMemoryBinding with bindMemory which is safer Adds function that provides access to a UnsafeBufferPointer for scalars and NativeStructs within swift Updates docs Suppress compilation warnings by replacing var with let Using overflow operators within swift to improve performance Adds tests for GRPC message creation from a retained _InternalByteBuffer
This commit is contained in:
@@ -83,15 +83,18 @@ public struct Verifier {
|
||||
if !_checkAlignment { return }
|
||||
|
||||
/// advance pointer to position X
|
||||
let ptr = _buffer._storage.memory.advanced(by: position)
|
||||
/// Check if the pointer is aligned
|
||||
if Int(bitPattern: ptr) & (MemoryLayout<T>.alignment &- 1) == 0 {
|
||||
return
|
||||
}
|
||||
try _buffer.withUnsafeBytes { pointer in
|
||||
let ptr = pointer.baseAddress!.advanced(by: position)
|
||||
|
||||
throw FlatbuffersErrors.missAlignedPointer(
|
||||
position: position,
|
||||
type: String(describing: T.self))
|
||||
/// Check if the pointer is aligned
|
||||
if Int(bitPattern: ptr) & (MemoryLayout<T>.alignment &- 1) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
throw FlatbuffersErrors.missAlignedPointer(
|
||||
position: position,
|
||||
type: String(describing: T.self))
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the value of Size "X" is within the range of the buffer
|
||||
@@ -134,17 +137,17 @@ public struct Verifier {
|
||||
|
||||
let length = Int(vtableLength)
|
||||
try isAligned(
|
||||
position: Int(clamping: (vtablePosition + length).magnitude),
|
||||
position: Int(clamping: (vtablePosition &+ length).magnitude),
|
||||
type: VOffset.self)
|
||||
try rangeInBuffer(position: vtablePosition, size: length)
|
||||
|
||||
storage.tableCount += 1
|
||||
storage.tableCount &+= 1
|
||||
|
||||
if storage.tableCount > _options._maxTableCount {
|
||||
throw FlatbuffersErrors.maximumTables
|
||||
}
|
||||
|
||||
storage.depth += 1
|
||||
storage.depth &+= 1
|
||||
|
||||
if storage.depth > _options._maxDepth {
|
||||
throw FlatbuffersErrors.maximumDepth
|
||||
@@ -211,7 +214,7 @@ public struct Verifier {
|
||||
@inline(__always)
|
||||
func verify(id: String) throws {
|
||||
let size = MemoryLayout<Int32>.size
|
||||
guard storage.capacity >= (size * 2) else {
|
||||
guard storage.capacity >= (size &* 2) else {
|
||||
throw FlatbuffersErrors.bufferDoesntContainID
|
||||
}
|
||||
let str = _buffer.readString(at: size, count: size)
|
||||
|
||||
Reference in New Issue
Block a user