Efficiently sending data from an X… | Apple Developer Forums
Efficiently sending data from an XPC process to the host application. App & System Services Processes & Concurrency XPC You’re now watching this thread. If you’ve opted in to email or web notifications, you’ll be notified when there’s activity. Click again to stop watching or visit your profile to manage watched threads and notifications. You’ve stopped watching this thread and will no longer receive emails or web notifications when there’s activity.
Click again to start watching. kennyc OP Created Dec ’19 Replies 6 Boosts 0 Views 6.0k Participants 4 Given an XPC process, what is the most efficient way to get data back to the host application? I have an XPC process, primarily written in C++ and a host application written in Swift. It generates a bunch of data that it serializes into an std::vector<std::byte>. When the process is finished, I want to efficiently transfer that buffer of bytes back to the host application.
At the moment, I copy the std::vector data() into an NSData, then encode that NSData into an object conforming to NSSecureCoding that is then sent back to the app. At a minimum this is creating two copies of the data (one in the vector and the other in the NSData) but then I suspect that the XPC transporter might be creating another? * When using NSData, can I use the bytesNoCopy version if I guarantee that the underlying vector is still alive when I initiate the XPC connection response?
When that call returns, am I then free to deallocate the vector even if the NSData is still in-flight back to the main app? * In one of the WWDC videos, it is recommended to use DispatchData as DispatchData might avoid making a copy when being transported across XPC. Does this apply when using NSXPCConnection or only when using the lower-level C APIs? * Is there a downside to using DispatchData that might increase the overhead? * Finally, where does Swift's Data type fit into this?
On the application side, I have Swift code that is reading the buffer as a stream of bytes, so I ideally want the buffer to be contiguous and in a format that doesn't require another copy in order for Swift to be able to read it. (On average, the buffers tend to be small. Maybe only 1-2 megabytes, if that. But occasionally a buffer might ballon to 100-200 megabytes.) Replies 6 Boosts 0 Views 6.0k Participants 4 john daniel OP Dec ’19 It is 2019.
Don't worry about "MBs". Add comment kennyc OP Dec ’19 For the most part I would agree. However, I'm still curious if there's an efficient way of using NSData, DispatchData or Data to reduce the number of unnecessary allocations. Consider how prevalent the use of an IOSurface pool or MetalTexture pool is to efficiently transfer graphics data between processes. Add comment DTS Engineer OP Apple Dec ’19 This response is kinda long.
Normally I’m not able to dedicate this much time on a DevForums post, but in this case I did some research over the weekend because I was curious how this stuff worked. I hope to be able to answer basic follow-up questions here, but if we go too far down the rabbit hole I may ask you to open a DTS tech support incident so that I can allocate more time to look at this. OK, with that out of the way, let’s tackle your immediate questions: When using NSData , can I use the bytesNoCopy version if I guarantee that the underlying vector is still alive when I initiate the XPC connection response?
When that call returns, am I then free to deallocate the vector even if the NSData is still in-flight back to the main app? No. There are three different ‘no copy’ methods, none of which support those semantics: -initWithBytesNoCopy:length: will call free on the buffer when NSData is deallocated (that is, its reference count hits zero). -initWithBytesNoCopy:length:deallocator: acts like the above, but calls the custom deallocator you provide. -initWithBytesNoCopy:length:freeWhenDone: acts like the above, but it only calls free if the freeWhenDone parameter is true.
In all cases, the buffer will (or must) stick around until the reference count hits zero. In one of the WWDC videos, it is recommended to use DispatchData as DispatchData might avoid making a copy when being transported across XPC. Does this apply when using NSXPCConnection … ? Yes, but the full story is more complex than that. I’ll going into this more below. Is there a downside to using DispatchData that might increase the overhead?
What sort of overhead are you concerned about? Finally, where does Swift's Data type fit into this? The key thing to remember about Data is that, as of Swift 5, it has a different underlying model than NSData . The C dispatch_data_t type is bridged to NSData [1], and thus DispatchData and NSData share the same underlying model, that is, the data is composed of multiple runs of contiguous bytes. That’s why you have dispatch_data_apply and -[NSData enumerateByteRangesUsingBlock:] , both of which let you get at these underlying runs.
Swift’s Data is simpler: It supports a single contiguous run of bytes [2]. Thus, you have to be very careful when bridging between NSData and Swift . In my experience, doing a round trip through Data (that is, NSData to Data and back to NSData ) without actually touching the data is fine. But as soon as you try to touch the bytes you run the risk of Data ‘flattening’ the bridged NSData . Before we get further into the data case, a quick note about NSXPCConnection and the low-level <xpc/xpc.h> API.
These are closely related and, for the most part, the high-level API supports everything supported by the low-level API. There is, however, one critical caveat: The low-level API lets you transport more types over the connection. For example, imagine you want to create an XPC Service where one part of your code uses NSXPCConnection and another part uses the low-level API. Historically this was tricky because a) an XPC Service can only register a single service, and b) there’s no way to transport an xpc_endpoint_t over NSXPCConnection and there’s no way to transport an NSXPCListenerEndpoint over xpc_connection_t .
This has been resolved in 10.15. There we added -[NSXPCInterface setXPCType:forSelector:argumentIndex:ofReply:] , which allows you to transport arbitrary XPC objects over NSXPCConnection . Yay! Note The availability macros on that method indicate that it’s available since 10.14. That’s not my experience, and I’ve filed a bug to get that corrected (r. 57736296). Historically this use to crop up when folks were trying to transport an IOSurface .
That API has an IOSurfaceCreateXPCObject routine that returns a low-level XPC object that represents the surface, but you couldn’t transport that over an NSXPCConnection . However, that specific problem got resolved on 10.12 where we introduced a new Objective-C IOSurface object, and that object is transportable directly over NSXPCConnection . So double yay! Coming back to the problem of transporting data efficiently, there are two paths to consider: You can share data implicitly by sending a data object.
You can share data explicitly using a shared memory object. I’ll discuss each in turn below, albeit in reverse (-: Both NSXPCConnection and the low-level XPC API let you explicitly transport shared memory objects over the connection. The low-level API supports this explicitly via a shared memory object ( XPC_TYPE_SHMEM ), create using xpc_shmem_create . In contrast, for NSXPCConnection you must created a POSIX shared memory object (using shm_open man page ), wrap the resulting file descriptor into an NSFileHandle , and then pass that over the XPC connection.
Note You can actually use the latter technique with the low-level API as well, using an XPC_TYPE_FD object created using xpc_fd_create . I can’t see any advantage of doing that, but there’s probably some subtlety I’ve missed. Overall, I can’t help but think that this might be the best option for you. That is, set up a pool of shared memory regions
The rest of this page is on the live site.