Skip to content

Commit a2a2023

Browse files
authored
alts: Fix buffer alignment with 16KB records (grpc#8791)
gRPC Go receives ALTS records of max 16KB under high load (see grpc#8512 (comment) 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 <img width="1532" height="480" alt="image" src="https://github.com/user-attachments/assets/c2051b7f-e828-44d6-a82a-0d444cace2df" /> ### After <img width="1550" height="690" alt="image" src="https://github.com/user-attachments/assets/5715c5b8-b163-474c-81a6-5bcc6808d2ff" /> RELEASE NOTES: * credentials/alts: optimize read buffer alignment to reduce copies.
1 parent 4046676 commit a2a2023

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

credentials/alts/internal/conn/record.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ const (
6868
// altsRecordDefaultLength.
6969
altsWriteBufferMaxSize = 512 * 1024 // 512KiB
7070
// The initial buffer used to read from the network.
71-
altsReadBufferInitialSize = 32 * 1024 // 32KiB
71+
// It includes an additional 512 Bytes to hold two 16KiB records plus
72+
// small framing overheads.
73+
altsReadBufferInitialSize = 32*1024 + 512 // 32.5KiB
7274
)
7375

7476
var (

0 commit comments

Comments
 (0)