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

doc: document that sync.Pool should be used only with pointer types #16323

Closed
rasky opened this issue Jul 11, 2016 · 1 comment
Closed

doc: document that sync.Pool should be used only with pointer types #16323

rasky opened this issue Jul 11, 2016 · 1 comment
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rasky
Copy link
Member

rasky commented Jul 11, 2016

To avoid spurious allocations, sync.Pool should always be used with pointers. For instance, when storing buffers as slices, a pointer to slice should be used instead of the slice itself. This was brought up in http://golang.org/cl/24371 by @dvyukov, but it's not documented.

Since the primary goal of sync.Pool is to reduce allocations, and using it with non-pointers makes it de-facto useless, it should be prominently documented to avoid using non-pointers with it.

@ianlancetaylor ianlancetaylor added this to the Go1.8 milestone Jul 11, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 7, 2016
@rsc
Copy link
Contributor

rsc commented Oct 18, 2016

I disagree that it's de-facto useless to have a pool of non-pointers. If you are saving multi-kilobyte slices, you may not care at all if each pool operation happens to allocate one pointer. This is an optimization detail, not something to expose to everyone reading the sync.Pool docs. If it's a problem in a real program, profiling will show that.

@rsc rsc closed this as completed Oct 18, 2016
@golang golang locked and limited conversation to collaborators Oct 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants