C++ · Batches

Batches

#include <ucall/server.hpp> 1.0.0-rc.1 · b7577426

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/ucall/cpp/batches.md' | claude -p "How would this fit my code?"

A pooled batch endpoint gathers matching requests from every event loop into one queue and hands them to its handler in batches, which suits work that is cheaper per request in bulk, like GPU inference.

void infer(uc::batch_t batch) noexcept {
    for (uc::request_t request : batch) {
        auto [body, body_status] = request.body();
        std::array<char, 24> score {};
        int const length = std::snprintf(score.data(), score.size(), "%zu", body.size());
        [[maybe_unused]] uc::status_t const replied =
            std::move(request).reply(ucall_http_status_ok_k, {}, {score.data(), std::size_t(length)});
    }
}

uc::interface_t const interfaces[] {{.address = "127.0.0.1:0", .max_pipelined_requests = 8}};
auto [server, server_status] = uc::server_t::make(interfaces);
uc::batching_t const batching {.min_batch_size = 16, .max_batch_size = 64, .max_latency = std::chrono::milliseconds(2)};
auto [endpoint, endpoint_status] = server.pooled_batch<infer>("/infer", ucall_verb_post_k, batching);
std::thread polling([&server] { [[maybe_unused]] uc::status_t const polled = server.take_calls(0); });
std::thread batching_thread([&endpoint] { [[maybe_unused]] uc::status_t const taken = endpoint.take_batches(); });

batching_t dispatches as soon as min_batch_size requests wait, never more than max_batch_size at once, and a smaller batch once its oldest request waited max_latency; zero fields mean 1, 32 and 10 milliseconds. The handler runs on the thread polling the endpoint through take_batch or take_batches, never on an event loop, so a slow batch never stalls the network. A batch_t holds its requests in arrival order: take(index) hands one over, and iterating it hands over each in turn, once, answering 500 for one passed without being taken. A request never taken stays unanswered until the server is destroyed, so take every one; a taken request answers 500 if it is destroyed unreplied. The queue holds as many requests as the interfaces admit at once, connections times pipelined requests, and refuses more with 503. pooled_batch_endpoint_t::statistics counts batches and requests, and whether each was dispatched on reaching the maximum size, the minimum size, or the latency. local_batch takes the same handlers and batching_t, but each event loop fires its own batches inside take_call or take_calls, so no thread drains them and the handler may reply there; its local_batch_endpoint_t offers only statistics.

API

Types

batch_tThe requests one batch callback received, each taken once by take or by iterating, which hands them over in arrival order.
server.hpp:407 ↗
class batch_t

A request never taken stays unanswered until the server itself is freed, so take every one.

batching_tWhen a batch endpoint dispatches; zero fields take 1, 32 and 10 milliseconds.
server.hpp:483 ↗
struct batching_t

Fields

min_batch_sizestd::size_tRequests that dispatch a batch at once.
max_batch_sizestd::size_tRequests one batch holds at most.
max_latencystd::chrono::microsecondsHow long the oldest request of a smaller batch waits before it dispatches anyway.