brpc-fast (brpc_fast) is a high-performance C++20 serialization codec, Proto3 code generator, and RPC runtime built on top of Apache bRPC (baidu_std protocol). It compiles standard .proto (Proto3) schemas into plain C++20 structs and typed bRPC services/stubs—bypassing protoc --cpp_out and libprotobuf's runtime object model and wire codec—while preserving bRPC's transport, scheduling, routing, and observability.
Control-plane and metadata services often run demanding mixed workloads: multi-megabyte metadata batches containing tens of thousands of nested descriptors alongside latency-sensitive small RPCs (such as heartbeats and single-key lookups).
-
Why Apache bRPC as the RPC layer? Apache bRPC provides a battle-tested M:N
bthreadscheduler, connection pooling, load balancing, timeouts/retries, cooperative cancellation, built-in monitoring/status pages, and scatter/gatherbutil::IOBufnetworking. -
Why not standard bRPC +
libprotobuf(protoc --cpp_out)?- Allocation & object-model overhead on large batches: Standard Protobuf C++ messages heap-allocate nested elements individually (
RepeatedPtrFieldpointer chasing, destructor walks, or manual arena plumbing) and pay per-field varint encode/decode branching on every integer. At 10k–30k nested items per RPC, serialization and object lifecycle dominate CPU and tail latency. - Allocator & kernel contention on multi-MB payloads: Repeatedly allocating and freeing multi-megabyte serialization buffers across concurrent bthreads triggers
mmap, minor page faults, andmunmapTLB shootdowns, hurting both large-batch throughput and co-located small-RPC p99 latency. - Verbose C++ API & lifecycle pitfalls: Raw
google::protobuf::Service,brpc::Controller, andgoogle::protobuf::Closureboilerplate makes async lifecycles, cancellation, and server/service teardown ordering easy to get wrong and does not integrate naturally with C++20 value types or coroutines.
- Allocation & object-model overhead on large batches: Standard Protobuf C++ messages heap-allocate nested elements individually (
-
Why not a schema-less or raw C++ reflection codec?
- Production distributed clusters need IDL-first contracts (
.protoas a single source of truth) and rolling-upgrade schema evolution—adding/removing fields, skipping unknown fields in O(1), widening scalars, evolving singular ↔ repeated fields, and keepingmap<K, V>wire-compatible with repeated map entries.
- Production distributed clusters need IDL-first contracts (
brpc-fast bridges this gap: Proto3 IDL + schema evolution + Apache bRPC transport, paired with a zero-copy, pool-backed, fixed-width binary codec and plain C++20 structs.
Measured on an AMD Ryzen 7 9700X (8C/16T, Linux, GCC 16.2.1 -O3 -DNDEBUG, TCP loopback, bench_schema.proto with key_len=128, replicas_per_key=2, concurrency=16). See benchmarks/README.md for the complete codec and E2E tables, small-RPC baselines, versions, and methodology.
| Workload | brpc-fast |
brpc + pb |
grpc++ (Callback) |
coro_rpc (struct_pack) |
capnp_rpc (0-copy)* |
|---|---|---|---|---|---|
Codec BatchGet(10,000) (reuse RT) |
607 us (1.71 MiB) | 4.81 ms (1.30 MiB) / 3.45 ms Arena | — | 1.36 ms (1.63 MiB) | 1.36 ms (2.90 MiB) |
Codec BatchPutStart(10,000) (reuse RT) |
53.7 us (24.5 GB/s) | 275 us (4.6 GB/s) | — | 171 us (7.7 GB/s) | 96.5 us (15.8 GB/s) |
RPC Ping(c=16) QPS / P50 / P99 |
552k / 28.6 us / 38.6 us | 553k / 28.4 us / 38.5 us | 480k / 28.4 us / 106 us | 1,692k / 9.2 us / 10.2 us | 1,348k / 11.0 us / 29.9 us |
RPC BatchGet(1,000) QPS / P50 / P99 |
31.0k / 403 us / 2.07 ms | 3.88k / 3.70 ms / 9.96 ms | 3.73k / 3.30 ms / 17.9 ms | 12.2k / 1.29 ms / 2.97 ms | 36.2k / 421 us / 880 us |
RPC BatchGet(10,000) QPS / P50 / P99 |
938 / 15.5 ms / 32.1 ms | 288 / 51.3 ms / 128 ms | 256 / 44.7 ms / 301 ms | 784 / 20.2 ms / 45.6 ms | 1,361 / 11.5 ms / 16.3 ms |
RPC Mixed(80%sm+10k) small P99 / large QPS |
1.61 ms / 932 | 9.07 ms / 284 | 61.2 ms / 232 | 1.68 ms / 794 | 5.89 ms / 1,622 |
- vs.
brpc + protobuf& nativegrpc++: 7.9×–11× faster large-batch codec (5.7× vs. Protobuf Arena), 3.3×–8.3× higher batch RPC throughput, and 5.6× / 38× lower small-RPC P99 tail latency under mixed large-batch pressure, with zero overhead on tinyPingRPCs (~31% larger wire size on nested payloads:1.71 MiBvs.1.30 MiB). - vs.
coro_rpc(struct_pack):coro_rpcleads on tiny RPCs (Ping/GetReplica, ~3.1× QPS) thanks to C++20 stackless coroutines and minimal framing;brpc-fastsurpassescoro_rpcon medium/large batches (2.54× QPS onBatchGet(1,000), +20% QPS and 30% lower P99 onBatchGet(10,000)) and mixed traffic while providing Proto3 schema evolution and bRPC's industrial transport stack. - *vs.
capnp_rpc: Cap'n Proto writes/reads wire segments in-place without materializing C++ structs (and ran across 8 manually sharded single-threaded reactors), yielding higher raw large-batch loopback QPS at the cost of 1.70× larger wire size (2.90 MiB), 3.7× worse small-RPC P99 in mixed traffic (5.89 msvs.1.61 ms), and non-valueBuilder/ReaderAPIs.
- Uses a compact 3-bit wire type tag (
kFixed1,kFixed4,kFixed8,kBytes,kPacked1,kPacked4,kPacked8,kList) supporting Protobuf field numbers up to2^29 - 1(1-byte tags for fields 1–15, 2-byte tags for 16–2047). - Encodes integers, floats, doubles, and enums as fixed-width little-endian values—eliminating per-scalar varint branching and allowing bulk
memcpyforrepeated4-byte/8-byte scalars and enums. - Encodes
repeatedstrings/messages andmapfields with akListenvelope (body_len,count, elements...) so decodersreserve(count)upfront in one allocation and can skip any unknown field in O(1). - Supports Proto3 schema evolution (
detail/schema_compat.h): forward/backward unknown-field skipping, opt-in unknown-field pass-through (PRESERVE_UNKNOWN_FIELDS), cross-width scalar conversion, singular ↔ repeated merging, andmap<K, V>↔ repeatedMapEntrycompatibility.
- Pass 1 (
ComputeSize) computes exact byte lengths into a pooled pre-orderSizeTape(butil::ObjectPool). - Pass 2 (
EncodeUnchecked) writes straight into a contiguous buffer with zero bounds checks. - Payloads
<= 4 KiBencode on the stack; larger payloads acquire a pre-warmed buffer fromWriteSlabPool(size classes from 16 KiB to 8 MiB) and attach it zero-copy tobutil::IOBufviaappend_user_data, eliminating per-RPCmmap/page-fault/munmapoverhead.
FastReaderdecodes directly across multi-blockbutil::IOBufchains without linearizing multi-MB messages into a flat temporary buffer.- Contiguous spans run a speculative in-order tag-matching fast path (
MatchConstTag), falling back cleanly to a full tag-switch loop for out-of-order, unknown, or schema-evolved fields. DecodeFromIOBufoverwrites destination structs in place to reuse existingstd::vectorandstd::stringcapacities. On the server side,DispatchServerMethodpoolsServerCallSlot<Req, Resp>objects viabutil::ObjectPool(with outlier capacity caps to keep RSS bounded).
brpc_fast_codegenreads theFileDescriptorSetproduced byprotoc --descriptor_set_outand only linkslibprotobuf(nolibprotoc-devrequired).- Emits plain C++20 structs under
::brpc_fast_pb::<package>usingstd::string,std::vector,std::optional(IndirectOptionalfor cyclic message graphs),std::variant+ typed accessors foroneof, andstd::unordered_mapformap. Referencedgoogle/protobuf/*well-known types are generated automatically without colliding withlibprotobuf's::google::protobuf::*types. - Registers lightweight synthetic
MethodDescriptors so bRPC'sbaidu_stdprotocol carries realservice_name/method_namemetadata and routes requests throughFastMasterService(brpc::BaiduMasterService) withoutprotoc --cpp_out.
FastServerenforcesAddService -> Start -> Stop/Join -> Service destructionordering via RAII.RpcStatus,RpcResult<T>,CallOptions, andRpcContextprovide a minimal, type-safe wrapper overbrpc::Controller.- Generated stubs expose both synchronous (
stub.Method(req, resp, opts)) and asynchronous (stub.MethodAsync(req, opts)) calls.RpcFuture<Resp>is move-only, supports both bthread-friendly.Get()and C++20co_await, cancels safely if dropped unawaited, and supports cooperative cancellation (single-threadedCancel(), or cross-threadbrpc::StartCancel(saved_call_id)using acall_id()saved beforeco_await).
| Path | Description |
|---|---|
codegen/main.cpp |
Standalone Proto3-to-C++20 code generator (brpc_fast_codegen). |
cmake/BrpcFastGenerate.cmake |
brpc_fast_generate(...) CMake helper function. |
cmake/FindBRPC.cmake |
Fetches and configures Apache bRPC 1.18.0 (without leveldb) if brpc::brpc is not already provided. |
patches/brpc-without-leveldb.patch |
Minimal patch for bRPC 1.18.0 (optional leveldb, C++20/glibc compatibility, GCC TLS workaround). |
include/brpc_fast/codec.h |
Public EncodeToIOBuf / DecodeFromIOBuf entry points and FastMessage concept. |
include/brpc_fast/service.h |
FastServer, FastMasterService, RpcStatus, RpcResult, RpcContext, RpcFuture. |
include/brpc_fast/indirect.h |
IndirectOptional and IndirectBox for cyclic message definitions. |
include/brpc_fast/detail/ |
Wire reader, field codec, schema-evolution converters, buffer/slab pools, and RPC runtime. |
src/service.cpp |
FastMasterService, FastServer, and synthetic descriptor registration. |
tests/ |
Unit tests covering codec round-trips, schema evolution, unknown fields, and sync/async/co_await RPCs. |
benchmarks/ |
Core codec and end-to-end loopback RPC benchmarks (see benchmarks/README.md). |
- C++20 compiler (GCC 11+ or Clang 14+)
- CMake 3.16+ and Ninja / Make
protoc+libprotobuf-dev(Protobuf 3.12+ supported;libprotoc-devis not needed)libgflags-dev,libglog-dev,libssl-dev,zlib1g-dev(required by Apache bRPC;libleveldb-devis not required)
include(FetchContent)
FetchContent_Declare(
brpc_fast
GIT_REPOSITORY https://github.com/Aionw/brpc-fast.git
GIT_TAG main)
FetchContent_MakeAvailable(brpc_fast)
brpc_fast_generate(
OUT_HDRS MY_FAST_HDRS
OUT_SRCS MY_FAST_SRCS
OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated"
PROTO_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/proto"
PROTO_FILES my_service.proto
# Optional: preserve unknown fields across decode -> encode round-trips
# PRESERVE_UNKNOWN_FIELDS
)
add_library(my_service_proto STATIC ${MY_FAST_SRCS} ${MY_FAST_HDRS})
target_include_directories(my_service_proto PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_link_libraries(my_service_proto PUBLIC brpc_fast::brpc_fast)This generates my_service.fast.h and my_service.fast.cc containing structs, service base classes, and <Service>_Stub classes under ::brpc_fast_pb::<proto_package>.
#include <brpc/channel.h>
#include "my_service.fast.h"
using namespace brpc_fast;
using namespace ::brpc_fast_pb::my_pkg;
class MyServiceImpl final : public MyService {
public:
RpcStatus Ping(const RpcContext& ctx, const PingRequest& req,
PingResponse& resp) override {
resp.ok = true;
return RpcStatus::Ok();
}
};
// Server
FastServer server;
server.AddService(std::make_unique<MyServiceImpl>());
server.Start("127.0.0.1:8000");
// Client
brpc::Channel channel;
brpc::ChannelOptions ch_opts;
ch_opts.protocol = brpc::PROTOCOL_BAIDU_STD;
channel.Init("127.0.0.1:8000", &ch_opts);
MyService_Stub stub(&channel);
// Synchronous call (reuses `resp` buffer capacities across calls)
PingResponse resp;
RpcStatus st = stub.Ping({.client_hi = 1, .client_lo = 2}, resp);
// Asynchronous call with blocking `.Get()` or C++20 `co_await`
RpcFuture<PingResponse> fut = stub.PingAsync({.client_hi = 1, .client_lo = 2});
RpcResult<PingResponse> res = fut.Get();
// Or inside a coroutine:
// RpcResult<PingResponse> res = co_await stub.PingAsync({.client_hi = 1, .client_lo = 2});# Configure with unit tests and benchmark enabled
cmake -B build -G Ninja -DBRPC_FAST_BUILD_TESTS=ON -DBRPC_FAST_BUILD_BENCHMARK=ON -DCMAKE_BUILD_TYPE=Release
# Build and run unit tests
cmake --build build --target brpc_fast_codec_test brpc_fast_rpc_test -j$(nproc)
ctest --test-dir build --output-on-failure
# Build and run performance benchmark
cmake --build build --target brpc_fast_codec_benchmark -j$(nproc)
./build/brpc_fast_codec_benchmarkSee benchmarks/README.md for benchmark flags and CSV export options.
- Wire compatibility:
brpc-fastuses its own tagged binary wire format over bRPC'sbaidu_stdprotocol. It is not wire-compatible with standard Protobuf wire encoding; both client and server must usebrpc-fast. - Proto3 unary RPCs only:
brpc_fast_codegenenforcessyntax = "proto3"(proto2 files are rejected) and unary RPC methods (streammethods are rejected at codegen time). - Server options:
FastServerowns thebaidu_master_servicedispatch hook and rejects caller-suppliedoptions.baidu_master_serviceandoptions.interceptor(bRPC'sbaidu_master_servicepath bypassesServer::AcceptRequest()). - Limits: Matches Protobuf's 2 GiB − 1 (
INT32_MAX) maximum encoded message size and enforces a default max nesting depth of 64. - Schema-evolution edge cases: Because
kFixed4/kFixed8wire tags do not carry signedness or float-vs-int distinctions, cross-width integer widening/narrowing follows the destination type's signedness, and changing a field betweenfloat/doubleand integer types reinterprets raw IEEE-754 bits rather than skipping the field. - Coroutine lifetime & cancellation contract: Once a coroutine suspends on
co_await RpcFuture, destroying the suspended coroutine frame before resumption is undefined behavior (guarded by a debug assert).RpcFuture::Cancel()is not thread-safe; to cancel an in-flightco_awaitfrom another thread, savecall_id()beforeco_await, callbrpc::StartCancel(saved_call_id), and let the coroutine resume with an error result.
Apache License 2.0. See LICENSE.