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

reuse buffers instead of allocating new ones during each read #581

Open
hessonsu opened this issue Jun 13, 2024 · 2 comments · May be fixed by #599
Open

reuse buffers instead of allocating new ones during each read #581

hessonsu opened this issue Jun 13, 2024 · 2 comments · May be fixed by #599
Labels
enhancement New feature or request

Comments

@hessonsu
Copy link

hessonsu commented Jun 13, 2024

issues 337

@hessonsu
Copy link
Author

hessonsu commented Jun 13, 2024

// Unmarshal decodes an interleaved frame.
func (f *InterleavedFrame) Unmarshal(br *bufio.Reader) error {
	var header [4]byte
	_, err := io.ReadFull(br, header[:])
	if err != nil {
		return err
	}

	if header[0] != InterleavedFrameMagicByte {
		return fmt.Errorf("invalid magic byte (0x%.2x)", header[0])
	}

	// it's useless to check payloadLen since it's limited to 65535
	payloadLen := int(uint16(header[2])<<8 | uint16(header[3]))

	f.Channel = int(header[1])
	f.Payload = make([]byte, payloadLen)

	_, err = io.ReadFull(br, f.Payload)
	return err
}

Discuss, how to use this method not to request memory make([]byte, payloadLen)
This increases the pressure on the GC

@hessonsu hessonsu reopened this Jun 13, 2024
@aler9
Copy link
Member

aler9 commented Jul 11, 2024

this is not a memory leak but a possible improvement, that is the ability to reuse buffers instead of allocating new ones.

@aler9 aler9 added the enhancement New feature or request label Jul 11, 2024
@aler9 aler9 changed the title memory leak? reuse buffers instead of allocating new ones during each read Jul 11, 2024
@kevmo314 kevmo314 linked a pull request Aug 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants