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

GODRIVER-3132 Use dst buf pool for zstd encoding. #1577

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from

Conversation

qingyang-hu
Copy link
Collaborator

@qingyang-hu qingyang-hu commented Mar 6, 2024

GODRIVER-3132

Summary

Use a pool to reuse the dst buffer for zstd.EncodeAll.

benchstat results:

name                               old time/op    new time/op    delta
CompressPayload/CompressorZstd-10    62.8µs ± 1%    62.9µs ± 0%     ~     (p=0.541 n=9+8)

name                               old speed      new speed      delta
CompressPayload/CompressorZstd-10  32.8GB/s ± 1%  32.8GB/s ± 0%     ~     (p=0.541 n=9+8)

name                               old alloc/op   new alloc/op   delta
CompressPayload/CompressorZstd-10    3.48kB ± 0%    1.79kB ± 0%  -48.51%  (p=0.000 n=10+10)

name                               old allocs/op  new allocs/op  delta
CompressPayload/CompressorZstd-10      4.00 ± 0%      1.00 ± 0%  -75.00%  (p=0.000 n=10+10)

Copy link

API Change Report

No changes found!

@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the priority-3-low Low Priority PR for Review label Mar 6, 2024
@qingyang-hu qingyang-hu marked this pull request as ready for review March 6, 2024 21:15
Copy link
Collaborator

@prestonvasquez prestonvasquez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't directly address the problem described in GODRIVER-3132, which is that each zstd.Encoder allocates a bunch of memory and then never releases it. The best way to limit that "permanent" allocated memory seems to be using WithWindowSize to limit the max window size. Additionally, klauspost/compress v1.17.5 limits the max window size for better/best Zstd compression to 8MB, so it may be worth updating to that version or newer.

Comment on lines +133 to +134
ptr := zstdBufPool.Get().(*[]byte)
b := encoder.EncodeAll(in, *ptr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding an additional pool of buffers just for Zstd compression, can we leverage the existing op buffer pool to improve memory usage for all compression types?

For example, in compression.go, change CompressPayload to accept a destination byte slice:

func CompressPayload(src, dst []byte, opts CompressionOpts) ([]byte, error) {
	// ...
	return encoder.EncodeAll(src, dst)
	// ...

Then in connection.go, pass the existing destination byte slice into CompressPayload:

func (c *Connection) CompressWireMessage(src, dst []byte) ([]byte, error) {
	// ...
	compressed, err := driver.CompressPayload(rem, dst, opts)
	// ...

Then remove the following call to AppendCompressedCompressedMessage, which is just an alias for append.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-3-low Low Priority PR for Review
Projects
None yet
3 participants