Skip to content
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

Zero-copy support #126

Open
siriux opened this issue Nov 28, 2016 · 5 comments
Open

Zero-copy support #126

siriux opened this issue Nov 28, 2016 · 5 comments

Comments

@siriux
Copy link

siriux commented Nov 28, 2016

Is it possible to do zero-copy with ipc-channel today? I've seen OsIpcSharedMemory, but I'm not sure it can be used directly for this purpose.

If it's not possible right now, do you plan to support it? I'm testing some ideas for interprocess video transfer, and zero-copy would be really helpful.

Thanks

@emilio
Copy link
Member

emilio commented Nov 28, 2016

I think this was discussed, and the conclusion was something like: Even though it's hard to expose a safe API on top of shmem to avoid races, letting the user write to a shared memory buffer with an unsafe API could be desirable, but it's just not done.

It should be a straight-forward addition I think, a patch would probably be welcomed :)

cc @antrik, did I get that right?

@siriux
Copy link
Author

siriux commented Nov 28, 2016

I'm quite new to Rust, but I might try to write a patch when I get more confidence. Thanks

@antrik
Copy link
Contributor

antrik commented Nov 29, 2016

We need to clarify the use case here.

What IpcSharedMemory currently allows, is creating a shared memory object initialised with some pre-existing data, and sending that to other processes, where it is mapped, so the data can be read there. This is not zero-copy, since creation of the object allocates new memory that is initialised by copying some existing data.

This could be fixed by extending the API to allow decoupling the allocation of the buffer from initialisation. This way we could create a new buffer for each frame, then fill it with data, and finally send it to the consumer process, where it is read and finally freed.

One possible approach for that would be introducing some kind of proto-SHM-object, which is allocated but not initialised, and can be borrowed mutably to fill it with data, before it's converted into a regular immutable SHM object for sending. Another approach would be allowing some kind of callback for supplying the initialisation data -- this would be less flexible in how the data can be supplied (probably precluding DMA?), but keep the API entirely safe, by not exposing uninitialised memory to users.

An entirely different case would arise if we want permanent shared memory, that can be overwritten by the producer process with new data repeatedly even after sending it off to a consumer process. This would be much more involved, since it requires some kind of inter-process locking to make it safe. Wrapping this in some kind of safe abstraction might be somewhat tricky I suspect, though I haven't spent too much thought on it. (And I'm not really familiar with inter-process locking mechanisms.)

@siriux
Copy link
Author

siriux commented Nov 29, 2016 via email

@antrik
Copy link
Contributor

antrik commented Nov 30, 2016

Let's not over-engineer this. The more flexible variant is not necessarily better, if it requires an unsafe interface. Do you actually need this flexibility for your use case? Please be more specific about what you are trying to do.

Same regarding buffer reuse. Of course we could synchronise the use of mutable buffers using only IPC channel messages, rather than adding some other cross-process synchronisation mechanism -- but that's likely less efficient. At any rate, if we try to reuse buffers, we definitely need some kind of cross-process synchronisation, which is always fairly expensive -- so it has to be very carefully considered, to make sure it's actually an optimisation... Again, we need specifics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants