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

Add shared zstd encoder pools. #267

Merged
merged 3 commits into from Dec 21, 2020
Merged

Add shared zstd encoder pools. #267

merged 3 commits into from Dec 21, 2020

Conversation

rubensf
Copy link
Contributor

@rubensf rubensf commented Dec 21, 2020

On some sample testing this compression performs ~4x faster
than "wasting" the encoders.

@rubensf rubensf requested a review from gkousik December 21, 2020 15:29
@google-cla google-cla bot added the cla: yes The author signed a CLA label Dec 21, 2020
On some sample testing this compression performs ~4x faster
than "wasting" the encoders.
var encoderInit sync.Once
var encoders sync.Pool

func newEncoder() interface{} {
Copy link
Contributor

@mostynb mostynb Dec 21, 2020

Choose a reason for hiding this comment

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

I think this is leaky: zstd.NewWriter spawns goroutines that refer to some internal data, then when the zstd.Encoder is purged from the sync.Pool that internal data + those goroutines leak.

Here is an example workaround: https://github.com/mostynb/zstdpool-syncpool

And some background: klauspost/compress#264

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed not only the goroutines would leak but also the encoder itself since GC wouldn't ever catch it.

I imported your lib in - thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

No problem- feedback welcome. It hasn't been thoroughly tested yet, but I figure it makes sense to share the effort in figuring out how to use this zstd api safely/efficiently (hence the tiny lib).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will certainly send PRs if I find any issues :)

var encoders sync.Pool

func newEncoder() interface{} {
e, _ := zstd.NewWriter(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to use zstd.WithEncoderConcurrency(1), to avoid creating more goroutines than are necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah thanks!

@rubensf rubensf merged commit 3113302 into master Dec 21, 2020
@rubensf rubensf deleted the oneenc branch December 21, 2020 20:32
rubensf added a commit that referenced this pull request Feb 19, 2021
Like #267, but for decoders.
rubensf added a commit that referenced this pull request Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes The author signed a CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants