Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feture: Reduce memory allocation during the Encode process to improve encoding performance. #727

Open
ethan256 opened this issue May 28, 2024 · 0 comments

Comments

@ethan256
Copy link

The current encoding process generates a large amount of memory allocation, and the GC pressure will increase when the QPS becomes large. Consider reducing the number of memory allocations to optimize the encoding process.

Here is my proposal:

  1. Modify the BinaryEncoder interface definition
type BinaryEncoder interface {
	Encode(s *Stream)
}

Reduce NewBuffer calls by stitching each field encoding result directly through Stream. the Stream definition is as follows:

type Stream struct {
	buf []byte
	byte []byte
	err error
}

Stream implements the WriteXxx method from the original Buffer.

  1. The Encode methods of all types that implement the BinaryEncoder interface require only minor modifications, such as DataValue:
func (d *DataValue) Encode(s *Stream) {
	s.WriteUint8(d.EncodingMask)

	if d.Has(DataValueValue) {
		s.WriteAny(d.Value)
	}
	if d.Has(DataValueStatusCode) {
		s.WriteUint32(uint32(d.Status))
	}
	if d.Has(DataValueSourceTimestamp) {
		s.WriteTime(d.SourceTimestamp)
	}
	if d.Has(DataValueSourcePicoseconds) {
		s.WriteUint16(d.SourcePicoseconds)
	}
	if d.Has(DataValueServerTimestamp) {
		s.WriteTime(d.ServerTimestamp)
	}
	if d.Has(DataValueServerPicoseconds) {
		s.WriteUint16(d.ServerPicoseconds)
	}
}

For detailed implementation, please refer to #726

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant