-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Swift] Adds new API to reduce memory copying within swift #8484
base: master
Are you sure you want to change the base?
Conversation
b6473e1
to
7bfcafb
Compare
@wtholliday would be nice to have a secondary pair of eyes on the swift implementation, the benchmarks are good when we are creating a buffer. however as mentioned before, reading data from the buffer is a bit slower |
7bfcafb
to
3a29d6e
Compare
9f3f299
to
f171d78
Compare
Hello, @abandy, @mr-swifter. We are currently reworking the way Flatbuffers swift reads data from a data object, what we used to do is make a copy of the data that is stored somewhere and then read from our internal storage, but that seemed to be inefficient. So we decided to refactor that and basically store that data within the ByteBuffer, and provide ways for our already written methods to read from that Questions
ClarificationSo in the following test, we are creating an array of bytes that we unalign using a byte at the start and then advancing the pointer, however that advance would always advance into let testUnaligned: () -> Bool = {
var bytes: [UInt8] = [0x00]
bytes.append(contentsOf: fbb.sizedByteArray)
return bytes.withUnsafeMutableBytes { ptr in
guard var baseAddress = ptr.baseAddress else {
XCTFail("Base pointer is not defined")
return false
}
baseAddress = baseAddress.advanced(by: 1)
let unlignedPtr = UnsafeMutableRawPointer(baseAddress)
var bytes = ByteBuffer(
assumingMemoryBound: unlignedPtr,
capacity: ptr.count - 1)
var monster: Monster = getRoot(byteBuffer: &bytes)
self.readFlatbufferMonster(monster: &monster)
return true
}
} So as long as the bytes within a Data, Uint8 array or even a pointer is pointing to an offset, which in theory is the Where do we go from here?
|
Hi @mustiikhalil, I commented on the Swift Arrow issue you opened and am pasting the reply here as well. Thanks again! Nice, good to remove unnecessary copying! I will try to pull your changes down and run the tests by end of next week. Even if these changes cause an issue, Swift Arrow should be fine as it is bound to a version of the flatbuffers, so checking in your changes shouldn't break Swift Arrow right away. Thanks for checking! |
c17d423
to
d636dfc
Compare
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. Removed unowned from the initializer 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
d636dfc
to
bd409d4
Compare
Using overflow operators within swift to improve performance Adds tests for GRPC message creation from a retained _InternalByteBuffer
bd409d4
to
b74f2af
Compare
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.
_InternalByteBuffer
which is responsible for creating FlatBuffersByteBuffer
to be the external face of the library and allows different ways of initialization