]> git.feebdaed.xyz Git - 0xmirror/grpc-go.git/commit
alts: Fix buffer alignment with 16KB records (#8791) master
authorArjan Singh Bal <46515553+arjan-bal@users.noreply.github.com>
Fri, 26 Dec 2025 06:24:09 +0000 (11:54 +0530)
committerGitHub <noreply@github.com>
Fri, 26 Dec 2025 06:24:09 +0000 (11:54 +0530)
commita2a2023d2a01e28360be9a8a4817b7e83cfcece5
treeebb0001013059717b14ae10a84b5bdb7c7cf42e8
parent40466769682557e7179b8c74ba3820cc78d49b4b
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
<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.
credentials/alts/internal/conn/record.go