Correct use of sync.Pool ?

Correct use of sync.Pool ?

What happens to data put back in a pool when it is still referenced ?

For instance, I would Put() a bytes.Buffer while its string might still be referenced ? What will happen ? Will the buffer be discarded from the pool, or will it be kept in the pool and returned with Get() only when there is no more references to it ?

My use case is as follow. I have functions that analyze a string and return a long slice of X as product. Most of the time the analyze will fail and the functions will return nil. This is a good use for the pool to store slice of x.

What I would like to know is what happens if I would Put the slice of X in any cases, even when it is returned as result. Would it break something ? Would it waste storage of the Pool ?

Submitted July 15, 2017 at 08:46PM by chmikes
via reddit


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s