Skip to content

Modify Message After Calling serverStream.Send() #8186

@lihao-figma

Description

@lihao-figma

Hello gRPC-Go maintainers,

Our application requires a substantial number of byte slices to compose outgoing messages, and we are exploring ways to reduce these allocations. We would appreciate any guidance you could provide on this matter.

Currently, we have attempted using a bytes.Buffer to assemble the message content and then reclaim it after calling serverStream.Send(). However, we noticed the following comment in the source code:

	// It is not safe to modify the message after calling SendMsg. Tracing
	// libraries and stats handlers may use the message lazily.

https://github.com/grpc/grpc-go/blob/master/stream.go#L1554-L1555

Given this, if we disable tracing and stats features, can we safely reclaim the bytes.Buffer? Furthermore, would this approach remain safe in future versions?

After reviewing the latest implementation, it appears that the gRPC server stream allocates a bytes.Buffer to store the newly compressed message and sends it out, followed by a defer free():
(https://github.com/grpc/grpc-go/blob/master/stream.go#L1678-L1682)

This suggests that, from a data integrity standpoint, modifying the data after calling Send() might be acceptable. We would greatly appreciate any insights or recommendations regarding this approach.

Thank you for your time and assistance.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions