mirror of
https://github.com/google/flatbuffers.git
synced 2026-11-12 23:16:21 +00:00
Bigfoot
This commit is contained in:
@@ -27,32 +27,15 @@ public struct ByteBuffer {
|
||||
@usableFromInline
|
||||
final class Storage {
|
||||
@usableFromInline
|
||||
@frozen enum Blob: ~Copyable {
|
||||
enum Blob {
|
||||
#if !os(WASI)
|
||||
case data(Data)
|
||||
case bytes(any ContiguousBytes)
|
||||
case bytes(ContiguousBytes)
|
||||
#endif
|
||||
|
||||
case byteBuffer(_InternalByteBuffer)
|
||||
case array([UInt8])
|
||||
case pointer(UnsafeMutableRawPointer)
|
||||
|
||||
init(_ other: borrowing Blob) {
|
||||
switch other {
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
self = .data(data)
|
||||
case .bytes(let contiguousBytes):
|
||||
self = .bytes(contiguousBytes)
|
||||
#endif
|
||||
case .byteBuffer(let internalByteBuffer):
|
||||
self = .byteBuffer(internalByteBuffer)
|
||||
case .array(let array):
|
||||
self = .array(array)
|
||||
case .pointer(let unsafeMutableRawPointer):
|
||||
self = .pointer(unsafeMutableRawPointer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
|
||||
@@ -61,7 +44,7 @@ public struct ByteBuffer {
|
||||
private let capacity: Int
|
||||
/// Retained blob of data that requires the storage to retain a pointer to.
|
||||
@usableFromInline
|
||||
let retainedBlob: Blob
|
||||
var retainedBlob: Blob
|
||||
|
||||
@usableFromInline
|
||||
init(count: Int) {
|
||||
@@ -74,9 +57,9 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
init(blob: borrowing Blob, capacity count: Int) {
|
||||
init(blob: Blob, capacity count: Int) {
|
||||
capacity = count
|
||||
retainedBlob = .init(blob)
|
||||
retainedBlob = blob
|
||||
isOwned = false
|
||||
}
|
||||
|
||||
@@ -293,7 +276,7 @@ public struct ByteBuffer {
|
||||
/// - removeBytes: Removes a number of bytes from the current size
|
||||
@inline(__always)
|
||||
init(
|
||||
blob: borrowing Storage.Blob,
|
||||
blob: Storage.Blob,
|
||||
count: Int,
|
||||
removing removeBytes: Int)
|
||||
{
|
||||
@@ -333,7 +316,7 @@ public struct ByteBuffer {
|
||||
/// - def: Type of the object
|
||||
/// - position: the index of the object in the buffer
|
||||
@inline(__always)
|
||||
public func read<T: BitwiseCopyable>(def: T.Type, position: Int) -> T {
|
||||
public func read<T>(def: T.Type, position: Int) -> T {
|
||||
_storage.readWithUnsafeRawPointer(position: position) {
|
||||
$0.bindMemory(to: T.self, capacity: 1)
|
||||
.pointee
|
||||
@@ -377,10 +360,10 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readSizedScalar<
|
||||
T: BinaryInteger,
|
||||
T1: BinaryInteger & BitwiseCopyable,
|
||||
T2: BinaryInteger & BitwiseCopyable,
|
||||
T3: BinaryInteger & BitwiseCopyable,
|
||||
T4: BinaryInteger & BitwiseCopyable
|
||||
T1: BinaryInteger,
|
||||
T2: BinaryInteger,
|
||||
T3: BinaryInteger,
|
||||
T4: BinaryInteger
|
||||
>(
|
||||
def: T.Type,
|
||||
t1: T1.Type,
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum FlexBufferType: UInt64, Sendable {
|
||||
public enum FlexBufferType: UInt64 {
|
||||
case null = 0
|
||||
/// Variable width signed integer: `Int8, Int16, Int32, Int64`
|
||||
case int = 1
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Common
|
||||
import Foundation
|
||||
|
||||
enum FlexBuffersErrors: Error {
|
||||
@@ -24,10 +23,6 @@ enum FlexBuffersErrors: Error {
|
||||
|
||||
@inline(__always)
|
||||
public func getRoot(buffer: ByteBuffer) throws -> Reference? {
|
||||
assert(
|
||||
isLitteEndian,
|
||||
"Swift FlexBuffers currently only supports little-endian systems")
|
||||
|
||||
let end = buffer.count
|
||||
if buffer.count < 3 {
|
||||
throw FlexBuffersErrors.sizeOfBufferIsTooSmall
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
@usableFromInline
|
||||
enum BitWidth: UInt64, CaseIterable, Sendable {
|
||||
enum BitWidth: UInt64, CaseIterable {
|
||||
case w8 = 0
|
||||
case w16 = 1
|
||||
case w32 = 2
|
||||
|
||||
@@ -20,10 +20,10 @@ import Foundation
|
||||
import Common
|
||||
#endif
|
||||
|
||||
public struct Value: Equatable, Sendable {
|
||||
public struct Value: Equatable {
|
||||
|
||||
@usableFromInline
|
||||
enum Union: Equatable, Sendable {
|
||||
enum Union: Equatable {
|
||||
case i(Int64)
|
||||
case u(UInt64)
|
||||
case f(Double)
|
||||
|
||||
@@ -37,15 +37,12 @@ public struct FlexBuffersWriter {
|
||||
private var hasDuplicatedKeys = false
|
||||
private var minBitWidth: BitWidth = .w8
|
||||
private var _bb: _InternalByteBuffer
|
||||
private var stack: Stack = Stack()
|
||||
private var stack: [Value] = []
|
||||
private var keyPool: [Int: UInt] = [:]
|
||||
private var stringPool: [Int: UInt] = [:]
|
||||
private var flags: BuilderFlag
|
||||
|
||||
public init(initialSize: Int = 1024, flags: BuilderFlag = .shareKeys) {
|
||||
assert(
|
||||
isLitteEndian,
|
||||
"Swift FlexBuffers currently only supports little-endian systems")
|
||||
_bb = _InternalByteBuffer(initialSize: initialSize)
|
||||
self.flags = flags
|
||||
}
|
||||
@@ -151,7 +148,7 @@ public struct FlexBuffersWriter {
|
||||
typed: typed,
|
||||
fixed: fixed,
|
||||
keys: nil)
|
||||
stack.popLast(start)
|
||||
stack = Array(stack[..<start])
|
||||
stack.append(vec)
|
||||
return vec.u
|
||||
}
|
||||
@@ -220,7 +217,7 @@ public struct FlexBuffersWriter {
|
||||
typed: false,
|
||||
fixed: false,
|
||||
keys: keys)
|
||||
stack.popLast(start)
|
||||
stack = Array(stack[..<start])
|
||||
stack.append(vec)
|
||||
return numericCast(vec.u)
|
||||
}
|
||||
@@ -724,7 +721,7 @@ public struct FlexBuffersWriter {
|
||||
assert(
|
||||
vectorType == stack[i].type,
|
||||
"""
|
||||
If you get this assert you are writing a typed vector
|
||||
If you get this assert you are writing a typed vector
|
||||
with elements that are not all the same type
|
||||
""")
|
||||
}
|
||||
@@ -760,7 +757,7 @@ public struct FlexBuffersWriter {
|
||||
}
|
||||
|
||||
if !fixed {
|
||||
write(value: UInt64(count), byteWidth: byteWidth)
|
||||
write(value: count, byteWidth: byteWidth)
|
||||
}
|
||||
|
||||
let vloc = _bb.writerIndex
|
||||
@@ -833,14 +830,13 @@ public struct FlexBuffersWriter {
|
||||
let key, value: Value
|
||||
}
|
||||
|
||||
stack.withUnsafeMutableBytes(start: start) { buffer in
|
||||
stack[start...].withUnsafeMutableBytes { buffer in
|
||||
var ptr = buffer.assumingMemoryBound(to: TwoValue.self)
|
||||
ptr.sort { a, b in
|
||||
let aMem = _bb.memory.advanced(by: numericCast(a.key.u))
|
||||
.assumingMemoryBound(to: CChar.self)
|
||||
let bMem = _bb.memory.advanced(by: numericCast(b.key.u))
|
||||
.assumingMemoryBound(to: CChar.self)
|
||||
|
||||
let comp = strcmp(aMem, bMem)
|
||||
if (comp == 0) && a != b { hasDuplicatedKeys = true }
|
||||
return comp < 0
|
||||
@@ -901,132 +897,3 @@ extension FlexBuffersWriter {
|
||||
return endMap(start: start)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate struct Stack: RandomAccessCollection {
|
||||
typealias Element = Value
|
||||
typealias Index = Int
|
||||
|
||||
private final class Storage {
|
||||
var memory: UnsafeMutableRawPointer
|
||||
|
||||
init(capacity: Int, alignment: Int) {
|
||||
memory = .allocate(byteCount: capacity, alignment: alignment)
|
||||
memset(memory, 0, capacity)
|
||||
}
|
||||
|
||||
deinit {
|
||||
memory.deallocate()
|
||||
}
|
||||
}
|
||||
|
||||
private static let initialCapacity = 10 &* MemoryLayout<Value>.stride
|
||||
private let storage: Storage
|
||||
private var capacity: Int
|
||||
private(set) var count: Int
|
||||
|
||||
var startIndex: Int {
|
||||
0
|
||||
}
|
||||
|
||||
var endIndex: Int {
|
||||
count
|
||||
}
|
||||
|
||||
init() {
|
||||
count = 0
|
||||
capacity = Self.initialCapacity
|
||||
|
||||
storage = Storage(
|
||||
capacity: capacity,
|
||||
alignment: MemoryLayout<Value>.alignment)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
subscript(position: Int) -> Value {
|
||||
get {
|
||||
storage.memory.advanced(by: position &* MemoryLayout<Value>.stride)
|
||||
.assumingMemoryBound(to: Value.self).pointee
|
||||
}
|
||||
set {
|
||||
storage.memory.advanced(by: position &* MemoryLayout<Value>.stride)
|
||||
.assumingMemoryBound(to: Value.self).pointee = newValue
|
||||
}
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
mutating func popLast(_ val: Int) {
|
||||
count = if val < 0 {
|
||||
0
|
||||
} else {
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
mutating func append(_ value: Value) {
|
||||
let writePosition = count &* MemoryLayout<Value>.stride
|
||||
if writePosition >= capacity {
|
||||
reallocate(writePosition: writePosition)
|
||||
}
|
||||
|
||||
storage.memory.advanced(by: writePosition).storeBytes(
|
||||
of: value,
|
||||
as: Value.self)
|
||||
count += 1
|
||||
}
|
||||
|
||||
mutating func removeAll(keepingCapacity keepCapacity: Bool = false) {
|
||||
count = 0
|
||||
if !keepCapacity {
|
||||
let ptr = storage.memory
|
||||
defer { ptr.deallocate() }
|
||||
|
||||
capacity = Self.initialCapacity
|
||||
storage.memory = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: capacity,
|
||||
alignment: MemoryLayout<Value>.alignment)
|
||||
}
|
||||
memset(storage.memory, 0, capacity)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
mutating func withUnsafeMutableBytes<R>(
|
||||
start: Int,
|
||||
_ body: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R
|
||||
{
|
||||
let startingPosition = start &* MemoryLayout<Value>.stride
|
||||
let pointer = storage.memory.advanced(by: startingPosition)
|
||||
return try body(UnsafeMutableRawBufferPointer(
|
||||
start: pointer,
|
||||
count: (count &* MemoryLayout<Value>.stride) &- startingPosition))
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
mutating func withUnsafeMutableBytes<R>(
|
||||
_ body: (UnsafeMutableRawBufferPointer) throws
|
||||
-> R) rethrows -> R
|
||||
{
|
||||
return try body(UnsafeMutableRawBufferPointer(
|
||||
start: storage.memory,
|
||||
count: count &* MemoryLayout<Value>.stride))
|
||||
}
|
||||
|
||||
mutating private func reallocate(writePosition: Int) {
|
||||
while capacity <= writePosition {
|
||||
capacity = capacity << 1
|
||||
}
|
||||
|
||||
/// solution take from Apple-NIO
|
||||
capacity = capacity.convertToPowerofTwo
|
||||
|
||||
let newData = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: capacity,
|
||||
alignment: MemoryLayout<Value>.alignment)
|
||||
memset(newData, 0, capacity)
|
||||
memcpy(
|
||||
newData,
|
||||
storage.memory,
|
||||
writePosition)
|
||||
storage.memory.deallocate()
|
||||
storage.memory = newData
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ struct _InternalByteBuffer {
|
||||
@usableFromInline
|
||||
final class Storage {
|
||||
/// pointer to the start of the buffer object in memory
|
||||
@exclusivity(unchecked) private(set) var memory: UnsafeMutableRawPointer
|
||||
var memory: UnsafeMutableRawPointer
|
||||
|
||||
@usableFromInline
|
||||
init(count: Int, alignment: Int) {
|
||||
@@ -60,7 +60,7 @@ struct _InternalByteBuffer {
|
||||
let newData = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: capacity,
|
||||
alignment: alignment)
|
||||
memset(newData, 0, capacity)
|
||||
memset(newData, 0, capacity &- writerSize)
|
||||
memcpy(
|
||||
newData,
|
||||
memory,
|
||||
|
||||
Reference in New Issue
Block a user