Proposal Details

Problem Currently, (*UnixConn) ReadMsgUnix() reads a message from a unix socket, and returns a *UnixAddress even when the calling code doesn't care about the source address. This allocation isn't optimized out by the compiler even when the calling code specifies _ for the return because it is referenced in some intermediate layers.

Proposed Solution Create (*UnixConn) ReadMsgUnixIgnoreSource() that returns (n, oobn, flags int, err error) and the required underlying functions to support this so that programs that do not care about the source address don't allocate unused data on each call.

Usecase I'm trying to optimize dbus calls which require multiple calls to ReadMsgUnix per dbus message and I am trying to support this on a limited memory (embedded) system. So any time I can save not allocating and GC'ing is an improvement.

Comment From: ianlancetaylor

For your use case, why do you need to use ReadMsgUnix rather than simply calling Read?

Comment From: pboyd04

For your use case, why do you need to use ReadMsgUnix rather than simply calling Read?

In order to handle Unix file descriptors being sent along with the messages. From the dbus spec: "The number of Unix file descriptors that accompany the message. If omitted, it is assumed that no Unix file descriptors accompany the message. The actual file descriptors need to be transferred via platform specific mechanism out-of-band. They must be sent at the same time as part of the message itself. They may not be sent before the first byte of the message itself is transferred or after the last byte of the message itself. This header field is controlled by the message sender."

The oob mechanism for the unix socket is only returened with ReadMsgUnix and not with the standard Read since it only passes the regular data and ignores the OOB.

Comment From: ianlancetaylor

Maybe we should start with a variant of x/sys/unix.Recvmsg that doesn't return a Sockaddr value. That might be enough.

Comment From: pboyd04

That seems reasonable to me.

Comment From: ianlancetaylor

x/sys/unix has at least the following functions that return a Sockaddr:

  • Recvfrom
  • Recvmsg
  • RecvmsgBuffers

The most general is RecvmsgBuffers.

In C it is possible to receive messages without getting an address by just passing NULL as the src_addr and addrlen arguments.

Perhaps we could add another Recvmsg variant.

// RecvmsgAnon is like RecvmsgBuffers but does not return the sender address.
func RecvmsgAnon(fd int, buffers [][]byte, oob []byte, flags int) (n, oobn int, recvflags int, err error)