mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 09:21:37 +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:
@@ -26,192 +26,240 @@ public struct ByteBuffer {
|
||||
/// deallocating the memory that was held by (memory: UnsafeMutableRawPointer)
|
||||
@usableFromInline
|
||||
final class Storage {
|
||||
// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
|
||||
private let unowned: Bool
|
||||
/// pointer to the start of the buffer object in memory
|
||||
var memory: UnsafeMutableRawPointer
|
||||
@usableFromInline
|
||||
enum Blob {
|
||||
#if !os(WASI)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
#endif
|
||||
|
||||
case byteBuffer(_InternalByteBuffer)
|
||||
case array([UInt8])
|
||||
case pointer(UnsafeMutableRawPointer)
|
||||
}
|
||||
|
||||
/// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
|
||||
private let isOwned: Bool
|
||||
/// Retained blob of data that requires the storage to retain a pointer to.
|
||||
@usableFromInline
|
||||
var retainedBlob: Blob
|
||||
/// Capacity of UInt8 the buffer can hold
|
||||
var capacity: Int
|
||||
|
||||
@usableFromInline
|
||||
init(count: Int, alignment: Int) {
|
||||
memory = UnsafeMutableRawPointer.allocate(
|
||||
init(count: Int) {
|
||||
let memory = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: count,
|
||||
alignment: alignment)
|
||||
alignment: MemoryLayout<UInt8>.alignment)
|
||||
capacity = count
|
||||
unowned = false
|
||||
retainedBlob = .pointer(memory)
|
||||
isOwned = true
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
init(memory: UnsafeMutableRawPointer, capacity: Int, unowned: Bool) {
|
||||
self.memory = memory
|
||||
self.capacity = capacity
|
||||
self.unowned = unowned
|
||||
init(blob: Blob, capacity count: Int) {
|
||||
capacity = count
|
||||
retainedBlob = blob
|
||||
isOwned = false
|
||||
}
|
||||
|
||||
deinit {
|
||||
if !unowned {
|
||||
memory.deallocate()
|
||||
guard isOwned else { return }
|
||||
switch retainedBlob {
|
||||
case .pointer(let unsafeMutableRawPointer):
|
||||
unsafeMutableRawPointer.deallocate()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func copy(from ptr: UnsafeRawPointer, count: Int) {
|
||||
assert(
|
||||
!unowned,
|
||||
isOwned,
|
||||
"copy should NOT be called on a buffer that is built by assumingMemoryBound")
|
||||
memory.copyMemory(from: ptr, byteCount: count)
|
||||
withUnsafeRawPointer {
|
||||
$0.copyMemory(from: ptr, byteCount: count)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func initialize(for size: Int) {
|
||||
assert(
|
||||
!unowned,
|
||||
isOwned,
|
||||
"initalize should NOT be called on a buffer that is built by assumingMemoryBound")
|
||||
memset(memory, 0, size)
|
||||
withUnsafeRawPointer {
|
||||
memset($0, 0, size)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer
|
||||
/// - Parameter size: Size of the current object
|
||||
@usableFromInline
|
||||
func reallocate(_ size: Int, writerSize: Int, alignment: Int) {
|
||||
let currentWritingIndex = capacity &- writerSize
|
||||
while capacity <= writerSize &+ size {
|
||||
capacity = capacity << 1
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeBytes(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes(body)
|
||||
case .pointer(let ptr):
|
||||
return try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
|
||||
}
|
||||
}
|
||||
|
||||
/// solution take from Apple-NIO
|
||||
capacity = capacity.convertToPowerofTwo
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeRawPointer(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .pointer(let ptr):
|
||||
return try body(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
let newData = UnsafeMutableRawPointer.allocate(
|
||||
byteCount: capacity,
|
||||
alignment: alignment)
|
||||
memset(newData, 0, capacity &- writerSize)
|
||||
memcpy(
|
||||
newData.advanced(by: capacity &- writerSize),
|
||||
memory.advanced(by: currentWritingIndex),
|
||||
writerSize)
|
||||
memory.deallocate()
|
||||
memory = newData
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .pointer(let ptr):
|
||||
return try body(ptr.advanced(by: position))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline var _storage: Storage
|
||||
|
||||
/// The size of the elements written to the buffer + their paddings
|
||||
private var _writerSize: Int = 0
|
||||
/// Alignment of the current memory being written to the buffer
|
||||
var alignment = 1
|
||||
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
|
||||
var writerIndex: Int { _storage.capacity &- _writerSize }
|
||||
|
||||
private var _readerIndex: Int = 0
|
||||
/// Reader is the position of the current Writer Index (capacity - size)
|
||||
public var reader: Int { writerIndex }
|
||||
public var reader: Int { _storage.capacity &- _readerIndex }
|
||||
/// Current size of the buffer
|
||||
public var size: UOffset { UOffset(_writerSize) }
|
||||
/// Public Pointer to the buffer object in memory. This should NOT be modified for any reason
|
||||
public var memory: UnsafeMutableRawPointer { _storage.memory }
|
||||
public var size: UOffset { UOffset(_readerIndex) }
|
||||
/// Current capacity for the buffer
|
||||
public var capacity: Int { _storage.capacity }
|
||||
/// Crash if the trying to read an unaligned buffer instead of allowing users to read them.
|
||||
public let allowReadingUnalignedBuffers: Bool
|
||||
|
||||
/// Constructor that creates a Flatbuffer object from an InternalByteBuffer
|
||||
/// - Parameter
|
||||
/// - bytes: Array of UInt8
|
||||
@inline(__always)
|
||||
init(byteBuffer: _InternalByteBuffer) {
|
||||
_storage = Storage(
|
||||
blob: .byteBuffer(byteBuffer),
|
||||
capacity: byteBuffer.capacity)
|
||||
_readerIndex = Int(byteBuffer.size)
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region by copying
|
||||
/// the underlying data to a new pointer
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - copyingMemoryBound: The unsafe memory region
|
||||
/// - capacity: The size of the given memory region
|
||||
@inline(__always)
|
||||
public init(
|
||||
copyingMemoryBound memory: UnsafeRawPointer,
|
||||
capacity: Int)
|
||||
{
|
||||
_storage = Storage(count: capacity)
|
||||
_storage.copy(from: memory, count: capacity)
|
||||
_readerIndex = _storage.capacity
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer object from a UInt8
|
||||
/// - Parameter
|
||||
/// - bytes: Array of UInt8
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
public init(
|
||||
bytes: [UInt8],
|
||||
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
||||
{
|
||||
var b = bytes
|
||||
_storage = Storage(count: bytes.count, alignment: alignment)
|
||||
_writerSize = _storage.capacity
|
||||
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
||||
b.withUnsafeMutableBytes { bufferPointer in
|
||||
_storage.copy(from: bufferPointer.baseAddress!, count: bytes.count)
|
||||
}
|
||||
@inline(__always)
|
||||
public init(bytes: [UInt8]) {
|
||||
_storage = Storage(blob: .array(bytes), capacity: bytes.count)
|
||||
_readerIndex = _storage.capacity
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
public init(
|
||||
data: Data,
|
||||
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
||||
{
|
||||
var b = data
|
||||
_storage = Storage(count: data.count, alignment: alignment)
|
||||
_writerSize = _storage.capacity
|
||||
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
||||
b.withUnsafeMutableBytes { bufferPointer in
|
||||
_storage.copy(from: bufferPointer.baseAddress!, count: data.count)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer instance with a size
|
||||
/// - Parameter:
|
||||
/// - size: Length of the buffer
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
init(initialSize size: Int) {
|
||||
let size = size.convertToPowerofTwo
|
||||
_storage = Storage(count: size, alignment: alignment)
|
||||
_storage.initialize(for: size)
|
||||
allowReadingUnalignedBuffers = false
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = _storage.capacity
|
||||
}
|
||||
|
||||
#if swift(>=5.0) && !os(WASI)
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int,
|
||||
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
||||
count: Int)
|
||||
{
|
||||
_storage = Storage(count: count, alignment: alignment)
|
||||
_writerSize = _storage.capacity
|
||||
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
||||
contiguousBytes.withUnsafeBytes { buf in
|
||||
_storage.copy(from: buf.baseAddress!, count: buf.count)
|
||||
}
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = _storage.capacity
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
/// - Parameter:
|
||||
/// **NOTE** Needs a call to `memory.deallocate()` later on to free the memory
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - assumingMemoryBound: The unsafe memory region
|
||||
/// - capacity: The size of the given memory region
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
@inline(__always)
|
||||
public init(
|
||||
assumingMemoryBound memory: UnsafeMutableRawPointer,
|
||||
capacity: Int,
|
||||
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
||||
capacity: Int)
|
||||
{
|
||||
_storage = Storage(memory: memory, capacity: capacity, unowned: true)
|
||||
_writerSize = capacity
|
||||
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
||||
}
|
||||
|
||||
/// Creates a copy of the buffer that's being built by calling sizedBuffer
|
||||
/// - Parameters:
|
||||
/// - memory: Current memory of the buffer
|
||||
/// - count: count of bytes
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
init(
|
||||
memory: UnsafeMutableRawPointer,
|
||||
count: Int,
|
||||
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
||||
{
|
||||
_storage = Storage(count: count, alignment: alignment)
|
||||
_storage.copy(from: memory, count: count)
|
||||
_writerSize = _storage.capacity
|
||||
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
||||
_storage = Storage(
|
||||
blob: .pointer(memory),
|
||||
capacity: capacity)
|
||||
_readerIndex = _storage.capacity
|
||||
}
|
||||
|
||||
/// Creates a copy of the existing flatbuffer, by copying it to a different memory.
|
||||
@@ -219,145 +267,14 @@ public struct ByteBuffer {
|
||||
/// - memory: Current memory of the buffer
|
||||
/// - count: count of bytes
|
||||
/// - removeBytes: Removes a number of bytes from the current size
|
||||
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
||||
@inline(__always)
|
||||
init(
|
||||
memory: UnsafeMutableRawPointer,
|
||||
blob: Storage.Blob,
|
||||
count: Int,
|
||||
removing removeBytes: Int,
|
||||
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
||||
removing removeBytes: Int)
|
||||
{
|
||||
_storage = Storage(count: count, alignment: alignment)
|
||||
_storage.copy(from: memory, count: count)
|
||||
_writerSize = removeBytes
|
||||
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
||||
}
|
||||
|
||||
/// Fills the buffer with padding by adding to the writersize
|
||||
/// - Parameter padding: Amount of padding between two to be serialized objects
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func fill(padding: Int) {
|
||||
assert(padding >= 0, "Fill should be larger than or equal to zero")
|
||||
ensureSpace(size: padding)
|
||||
_writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
|
||||
}
|
||||
|
||||
/// Adds an array of type Scalar to the buffer memory
|
||||
/// - Parameter elements: An array of Scalars
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push<T: Scalar>(elements: [T]) {
|
||||
elements.withUnsafeBytes { ptr in
|
||||
ensureSpace(size: ptr.count)
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
||||
ptr.baseAddress!,
|
||||
ptr.count)
|
||||
_writerSize = _writerSize &+ ptr.count
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds an array of type Scalar to the buffer memory
|
||||
/// - Parameter elements: An array of Scalars
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push<T: NativeStruct>(elements: [T]) {
|
||||
elements.withUnsafeBytes { ptr in
|
||||
ensureSpace(size: ptr.count)
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
||||
ptr.baseAddress!,
|
||||
ptr.count)
|
||||
_writerSize = _writerSize &+ ptr.count
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a `ContiguousBytes` to buffer memory
|
||||
/// - Parameter value: bytes to copy
|
||||
#if swift(>=5.0) && !os(WASI)
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push(bytes: ContiguousBytes) {
|
||||
bytes.withUnsafeBytes { ptr in
|
||||
ensureSpace(size: ptr.count)
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
||||
ptr.baseAddress!,
|
||||
ptr.count)
|
||||
_writerSize = _writerSize &+ ptr.count
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Adds an object of type NativeStruct into the buffer
|
||||
/// - Parameters:
|
||||
/// - value: Object that will be written to the buffer
|
||||
/// - size: size to subtract from the WriterIndex
|
||||
@usableFromInline
|
||||
@inline(__always)
|
||||
mutating func push<T: NativeStruct>(struct value: T, size: Int) {
|
||||
ensureSpace(size: size)
|
||||
withUnsafePointer(to: value) {
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- size),
|
||||
$0,
|
||||
size)
|
||||
_writerSize = _writerSize &+ size
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds an object of type Scalar into the buffer
|
||||
/// - Parameters:
|
||||
/// - value: Object that will be written to the buffer
|
||||
/// - len: Offset to subtract from the WriterIndex
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push<T: Scalar>(value: T, len: Int) {
|
||||
ensureSpace(size: len)
|
||||
withUnsafePointer(to: value) {
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: writerIndex &- len),
|
||||
$0,
|
||||
len)
|
||||
_writerSize = _writerSize &+ len
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a string to the buffer using swift.utf8 object
|
||||
/// - Parameter str: String that will be added to the buffer
|
||||
/// - Parameter len: length of the string
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
mutating func push(string str: String, len: Int) {
|
||||
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(
|
||||
_storage.memory.advanced(by: writerIndex &- len),
|
||||
bytes.baseAddress!,
|
||||
len)
|
||||
_writerSize = _writerSize &+ len
|
||||
return true
|
||||
_storage = Storage(blob: blob, capacity: count)
|
||||
_readerIndex = removeBytes
|
||||
}
|
||||
|
||||
/// Write stores an object into the buffer directly or indirectly.
|
||||
@@ -376,63 +293,26 @@ public struct ByteBuffer {
|
||||
}
|
||||
assert(index < _storage.capacity, "Write index is out of writing bound")
|
||||
assert(index >= 0, "Writer index should be above zero")
|
||||
withUnsafePointer(to: value) {
|
||||
memcpy(
|
||||
_storage.memory.advanced(by: index),
|
||||
$0,
|
||||
MemoryLayout<T>.size)
|
||||
_ = withUnsafePointer(to: value) { ptr in
|
||||
_storage.withUnsafeRawPointer {
|
||||
memcpy(
|
||||
$0.advanced(by: index),
|
||||
ptr,
|
||||
MemoryLayout<T>.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes sure that buffer has enouch space for each of the objects that will be written into it
|
||||
/// - Parameter size: size of object
|
||||
@discardableResult
|
||||
@usableFromInline
|
||||
@inline(__always)
|
||||
mutating func ensureSpace(size: Int) -> Int {
|
||||
if size &+ _writerSize > _storage.capacity {
|
||||
_storage.reallocate(size, writerSize: _writerSize, alignment: alignment)
|
||||
}
|
||||
assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes")
|
||||
return size
|
||||
}
|
||||
|
||||
/// pops the written VTable if it's already written into the buffer
|
||||
/// - Parameter size: size of the `VTable`
|
||||
@usableFromInline
|
||||
@inline(__always)
|
||||
mutating func pop(_ size: Int) {
|
||||
assert(
|
||||
(_writerSize &- size) > 0,
|
||||
"New size should NOT be a negative number")
|
||||
memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size)
|
||||
_writerSize = size
|
||||
}
|
||||
|
||||
/// Clears the current size of the buffer
|
||||
@inline(__always)
|
||||
mutating public func clearSize() {
|
||||
_writerSize = 0
|
||||
}
|
||||
|
||||
/// Clears the current instance of the buffer, replacing it with new memory
|
||||
@inline(__always)
|
||||
mutating public func clear() {
|
||||
_writerSize = 0
|
||||
alignment = 1
|
||||
_storage.initialize(for: _storage.capacity)
|
||||
}
|
||||
|
||||
/// Reads an object from the buffer
|
||||
/// - Parameters:
|
||||
/// - def: Type of the object
|
||||
/// - position: the index of the object in the buffer
|
||||
@inline(__always)
|
||||
public func read<T>(def: T.Type, position: Int) -> T {
|
||||
if allowReadingUnalignedBuffers {
|
||||
return _storage.memory.advanced(by: position).loadUnaligned(as: T.self)
|
||||
_storage.readWithUnsafeRawPointer(position: position) {
|
||||
$0.bindMemory(to: T.self, capacity: 1)
|
||||
.pointee
|
||||
}
|
||||
return _storage.memory.advanced(by: position).load(as: T.self)
|
||||
}
|
||||
|
||||
/// Reads a slice from the memory assuming a type of T
|
||||
@@ -447,10 +327,32 @@ public struct ByteBuffer {
|
||||
assert(
|
||||
index + count <= _storage.capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
let start = _storage.memory.advanced(by: index)
|
||||
.assumingMemoryBound(to: T.self)
|
||||
let array = UnsafeBufferPointer(start: start, count: count)
|
||||
return Array(array)
|
||||
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
let buf = UnsafeBufferPointer(
|
||||
start: $0.bindMemory(to: T.self, capacity: count),
|
||||
count: count)
|
||||
return Array(buf)
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides a pointer towards the underlying primitive types
|
||||
/// - Parameters:
|
||||
/// - index: index of the object to be read from the buffer
|
||||
/// - count: count of bytes in memory
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
public func withUnsafePointerToSlice<T>(
|
||||
index: Int,
|
||||
count: Int,
|
||||
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
assert(
|
||||
index + count <= _storage.capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return try _storage.readWithUnsafeRawPointer(position: index) {
|
||||
try body(UnsafeRawBufferPointer(start: $0, count: count))
|
||||
}
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
@@ -468,10 +370,14 @@ public struct ByteBuffer {
|
||||
assert(
|
||||
index + count <= _storage.capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
let start = _storage.memory.advanced(by: index)
|
||||
.assumingMemoryBound(to: UInt8.self)
|
||||
let bufprt = UnsafeBufferPointer(start: start, count: count)
|
||||
return String(bytes: Array(bufprt), encoding: type)
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
let buf = UnsafeBufferPointer(
|
||||
start: $0.bindMemory(to: UInt8.self, capacity: count),
|
||||
count: count)
|
||||
return String(
|
||||
bytes: buf,
|
||||
encoding: type)
|
||||
}
|
||||
}
|
||||
#else
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
@@ -486,10 +392,9 @@ public struct ByteBuffer {
|
||||
assert(
|
||||
index + count <= _storage.capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
let start = _storage.memory.advanced(by: index)
|
||||
.assumingMemoryBound(to: UInt8.self)
|
||||
let bufprt = UnsafeBufferPointer(start: start, count: count)
|
||||
return String(cString: bufprt.baseAddress!)
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -502,19 +407,9 @@ public struct ByteBuffer {
|
||||
removeBytes < _storage.capacity,
|
||||
"Can NOT remove more bytes than the ones allocated")
|
||||
return ByteBuffer(
|
||||
memory: _storage.memory,
|
||||
blob: _storage.retainedBlob,
|
||||
count: _storage.capacity,
|
||||
removing: _writerSize &- removeBytes)
|
||||
}
|
||||
|
||||
/// Returns the written bytes into the ``ByteBuffer``
|
||||
public var underlyingBytes: [UInt8] {
|
||||
let cp = capacity &- writerIndex
|
||||
let start = memory.advanced(by: writerIndex)
|
||||
.bindMemory(to: UInt8.self, capacity: cp)
|
||||
|
||||
let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp)
|
||||
return Array(ptr)
|
||||
removing: _readerIndex &- removeBytes)
|
||||
}
|
||||
|
||||
/// SkipPrefix Skips the first 4 bytes in case one of the following
|
||||
@@ -524,19 +419,46 @@ public struct ByteBuffer {
|
||||
@usableFromInline
|
||||
@inline(__always)
|
||||
mutating func skipPrefix() -> Int32 {
|
||||
_writerSize = _writerSize &- MemoryLayout<Int32>.size
|
||||
_readerIndex = _readerIndex &- MemoryLayout<Int32>.size
|
||||
return read(def: Int32.self, position: 0)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
public func withUnsafeBytes<T>(
|
||||
body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try _storage.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func withUnsafeMutableRawPointer<T>(
|
||||
body: (UnsafeMutableRawPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try _storage.withUnsafeRawPointer(body)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
try _storage.readWithUnsafeRawPointer(position: position, body)
|
||||
}
|
||||
}
|
||||
|
||||
extension ByteBuffer: CustomDebugStringConvertible {
|
||||
|
||||
public var debugDescription: String {
|
||||
"""
|
||||
buffer located at: \(_storage.memory), with capacity of \(_storage.capacity)
|
||||
{ writerSize: \(_writerSize), readerSize: \(reader), writerIndex: \(
|
||||
writerIndex) }
|
||||
buffer located at: \(_storage.retainedBlob),
|
||||
with capacity of \(_storage.capacity),
|
||||
{ writtenSize: \(_readerIndex), readerSize: \(reader),
|
||||
size: \(size) }
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user