xref: /aosp_15_r20/external/grpc-grpc/doc/cpp/perf_notes.md (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard Worker# C++ Performance Notes
2*cc02d7e2SAndroid Build Coastguard Worker
3*cc02d7e2SAndroid Build Coastguard Worker## Streaming write buffering
4*cc02d7e2SAndroid Build Coastguard Worker
5*cc02d7e2SAndroid Build Coastguard WorkerGenerally, each write operation (Write(), WritesDone()) implies a syscall.
6*cc02d7e2SAndroid Build Coastguard WorkergRPC will try to batch together separate write operations from different
7*cc02d7e2SAndroid Build Coastguard Workerthreads, but currently cannot automatically infer batching in a single stream.
8*cc02d7e2SAndroid Build Coastguard Worker
9*cc02d7e2SAndroid Build Coastguard WorkerIf message k+1 in a stream does not rely on responses from message k, it's
10*cc02d7e2SAndroid Build Coastguard Workerpossible to enable write batching by passing a WriteOptions argument to Write
11*cc02d7e2SAndroid Build Coastguard Workerwith the buffer_hint set:
12*cc02d7e2SAndroid Build Coastguard Worker
13*cc02d7e2SAndroid Build Coastguard Worker~~~{.cpp}
14*cc02d7e2SAndroid Build Coastguard Workerstream_writer->Write(message, WriteOptions().set_buffer_hint());
15*cc02d7e2SAndroid Build Coastguard Worker~~~
16*cc02d7e2SAndroid Build Coastguard Worker
17*cc02d7e2SAndroid Build Coastguard WorkerThe write will be buffered until one of the following is true:
18*cc02d7e2SAndroid Build Coastguard Worker- the per-stream buffer is filled (controllable with the channel argument
19*cc02d7e2SAndroid Build Coastguard Worker  GRPC_ARG_HTTP2_WRITE_BUFFER_SIZE) - this prevents infinite buffering leading
20*cc02d7e2SAndroid Build Coastguard Worker  to OOM
21*cc02d7e2SAndroid Build Coastguard Worker- a subsequent Write without buffer_hint set is posted
22*cc02d7e2SAndroid Build Coastguard Worker- the call is finished for writing (WritesDone() called on the client,
23*cc02d7e2SAndroid Build Coastguard Worker  or Finish() called on an async server stream, or the service handler returns
24*cc02d7e2SAndroid Build Coastguard Worker  for a sync server stream)
25*cc02d7e2SAndroid Build Coastguard Worker
26*cc02d7e2SAndroid Build Coastguard Worker## Completion Queues and Threading in the Async API
27*cc02d7e2SAndroid Build Coastguard Worker
28*cc02d7e2SAndroid Build Coastguard WorkerRight now, the best performance trade-off is having numcpu's threads and one
29*cc02d7e2SAndroid Build Coastguard Workercompletion queue per thread.
30