[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:
mustiikhalil
2025-03-18 07:48:39 +01:00
committed by GitHub
parent 1c514626e8
commit bd1b2d0baf
29 changed files with 1470 additions and 463 deletions

View File

@@ -17,8 +17,7 @@ public extension GRPCFlatBufPayload {
self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes))
}
func serialize(into buffer: inout NIO.ByteBuffer) throws {
let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: Int(self.size))
buffer.writeBytes(buf)
withUnsafeReadableBytes { buffer.writeBytes($0) }
}
}
extension Message: GRPCFlatBufPayload {}

View File

@@ -428,10 +428,7 @@ grpc::string GenerateHeader() {
code += " }\n";
code += " func serialize(into buffer: inout NIO.ByteBuffer) throws {\n";
code +=
" let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: "
"Int(self.size))\n";
code += " buffer.writeBytes(buf)\n";
code += " withUnsafeReadableBytes { buffer.writeBytes($0) }\n";
code += " }\n";
code += "}\n";
code += "extension Message: GRPCFlatBufPayload {}\n";