From a2a2023d2a01e28360be9a8a4817b7e83cfcece5 Mon Sep 17 00:00:00 2001 From: Arjan Singh Bal <46515553+arjan-bal@users.noreply.github.com> Date: Fri, 26 Dec 2025 11:54:09 +0530 Subject: [PATCH] alts: Fix buffer alignment with 16KB records (#8791) gRPC Go receives ALTS records of max 16KB under high load (see https://github.com/grpc/grpc-go/pull/8512#issuecomment-3193280949 for details). When the ALTS conn has a partial encrypted frame in its buffer, it attempts to copy the frame to the beginning of the buffer to read the remainder. https://github.com/grpc/grpc-go/blob/40466769682557e7179b8c74ba3820cc78d49b4b/credentials/alts/internal/conn/record.go#L151-L159 When using a buffer of exactly 32KiB, almost the entire second frame of 16KiB is stored, but not the full frame. As a result, a large copy of ~16KiB is performed. ## Solution This PR increases the read buffer length by 512 bytes to ensure two entire 16KiB frames can be stored. This ensures that usually, only ~512 bytes needs to be moved to the front. ## Benchmark In a GCS directpath benchmark downloading files in a loop, the time spent on memory copies in the ALTS code is eliminated, saving ~3.7% of CPU time. ### Before image ### After image RELEASE NOTES: * credentials/alts: optimize read buffer alignment to reduce copies. --- credentials/alts/internal/conn/record.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/credentials/alts/internal/conn/record.go b/credentials/alts/internal/conn/record.go index f9d2646d..329e3691 100644 --- a/credentials/alts/internal/conn/record.go +++ b/credentials/alts/internal/conn/record.go @@ -68,7 +68,9 @@ const ( // altsRecordDefaultLength. altsWriteBufferMaxSize = 512 * 1024 // 512KiB // The initial buffer used to read from the network. - altsReadBufferInitialSize = 32 * 1024 // 32KiB + // It includes an additional 512 Bytes to hold two 16KiB records plus + // small framing overheads. + altsReadBufferInitialSize = 32*1024 + 512 // 32.5KiB ) var ( -- 2.43.0