System I/O Integration

Platform Buffer Structures

struct iovec {
    void*  iov_base;  // Pointer to data
    size_t iov_len;   // Length of data
};

POSIX uses iovec with readv(), writev(), recvmsg(), and sendmsg(). Capy’s buffer types place the pointer first and the size second, matching iovec in both order and width. Filling an iovec is therefore a field-for-field copy, with no conversion.

typedef struct _WSABUF {
    ULONG  len;  // Length (note: first!)
    CHAR*  buf;  // Pointer
} WSABUF;

Windows uses WSABUF with WSARecv() and WSASend(). The field order is reversed and the length is 32-bit, so an I/O backend copies descriptors into a WSABUF array rather than casting.

Translation Process

Capy makes no I/O system calls itself. An I/O backend, such as Corosio, implements ReadStream and WriteStream on top of the operating system. When you pass a buffer sequence to its write_some, the I/O backend translates the descriptors into the platform structures above and makes the OS call.

The type-erased streams add one step in front of the I/O backend. any_write_stream copies at most the first 16 non-empty buffers into a fixed array inside its awaitable. It passes that array to the wrapped stream’s write_some in one call, with no allocation. any_read_stream does the same for read_some. Buffers past the sixteenth are not part of that call. The limit of 16 is implementation-defined.

A write_some call may transfer fewer bytes than offered. The composed algorithms write and read loop over write_some and read_some. Each iteration passes the part of the sequence not yet transferred, so a long sequence goes out over several calls:

Pseudocode - how write drives write_some
template<capy::WriteStream Stream, capy::ConstBufferSequence Buffers>
capy::task<capy::results<std::size_t>>
write(Stream& stream, Buffers buffers)
{
    std::size_t const total = capy::buffer_size(buffers);
    std::size_t written = 0;
    while (written < total)
    {
        // remaining(buffers, written) stands for the bytes not yet sent
        auto [ec, n] = co_await stream.write_some(remaining(buffers, written));
        written += n;
        if (ec && written < total)
            co_return {ec, written};
    }
    co_return {std::error_code(), written};
}

Why Vectored I/O

Consider sending an HTTP message whose headers and body sit in separate buffers. With a single-buffer API you have two options, and each costs something:

  • Copy. Allocate a buffer large enough for both, copy the headers in, copy the body after them, then send once. That is an allocation plus two copies of data you already have.

  • Call twice. Send the headers, then send the body. No copy, but two system calls rather than one. On a datagram socket it also changes the result: two datagrams instead of one.

Vectored I/O avoids both. One call transfers several non-contiguous buffers as a single logical operation:

write(fd, header, header_len);  // syscall 1
write(fd, body, body_len);      // syscall 2
iovec iov[2] = {{header, header_len}, {body, body_len}};
writev(fd, iov, 2);  // single syscall

The data is never copied into a contiguous staging buffer. The OS reads directly from each region.

Registered Buffers

Some platforms allow buffers to be pre-registered with the kernel, removing per-operation address translation. On Linux 5.1+, io_uring supports this:

// Registration (done once)
io_uring_register_buffers(ring, buffers, count);

// Use (fast path - no translation)
io_uring_prep_write_fixed(sqe, fd, buf, len, offset, buf_index);

Windows offers a comparable optimization through Registered I/O (RIO).

Corosio does not currently expose either. Every operation goes through the per-call translation described above.

Writing Efficient Code

Fewer buffers means less translation overhead:

// Prefer: single buffer when possible
auto buf = assemble_message();  // Build in one buffer
co_await capy::write(stream, buf);

// Avoid: many tiny buffers
std::array<capy::const_buffer, 100> tiny_bufs;
co_await capy::write(stream, tiny_bufs);  // 100-element translation

No Capy API accepts platform structures. If your own code calls writev directly with the same structure each time, consider caching the platform array:

// Build once, use many times
struct message_buffers
{
    std::array<iovec, 3> iovecs;

    void set_header(void const* p, std::size_t n);
    void set_body(void const* p, std::size_t n);
    void set_footer(void const* p, std::size_t n);
};

Buffer translation is rarely the bottleneck. Profile network latency, disk time, and your own processing before optimizing descriptor copying.