Search code examples
javaperformancesocketsniolow-latency

zero allocation copy from socket to socket


Given we have a socket connection (lets call it c1) where we accept messages, and we have N other socket connections we are about write the exact same message, to determine which connection we want it to write to we only need to read the first few bytes from c1, but the remaining of the bytes on the socket needs not to be loaded to java heap, just to be written to c2...

So in nutshell what we want to do is. we got event that we have bytes to read on c1 we read the first few bytes and determine that we know that we want to redirect this to c2. We take those first few bytes we already took from c1 write it to c2 and now we want to tell the system to write next N bytes from c1 to c2 directly (instead of c1 -> java heap -> c2).

Is there any means to do this in java?


Update:

Although the answer is to the point, if you are building something like this ( even not only for multiplexing but some little business logic ), I highly recommend you to go with ZeroMQ.


Solution

  • Say we are doing C++ is there a way to tell the OS to pass data from buffer to buffer without loading into application memory?

    So basically the question is does Linux allow to pass N bytes from socket buffer A to socket buffer B (we can safely assume that we are on linux, and that no one else is reading or writing to A and B)

    It may be possible to do this with the Linux "sendfile" syscall.

    Reference:

    However, this is not supported by the Java I/O class libraries.

    UPDATE - Apparently it is supported; see the FileChannel::transferTo method. See the answer to FileChannel zero-copy transferTo fails to copy bytes to SocketChannel for an example that shows this being used with a socket. However, it is not clear if it can be used for socket to socket copies.

    UPDATE 2 - Based on the answers to Using Java to perform Zero Copy data transfers between two or more sockets, I think the answer may be: "No it can't". Yet.

    UPDATE 3 - Here's the RFE - https://bugs.openjdk.java.net/browse/JDK-6653061