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
util/pool: add package for storing and using a pool of items #12091
Conversation
@bradfitz you may recognize the shape of this as being inspired by your change in |
2606285
to
a33c6b8
Compare
// index is the current location of this item in pool.s. It gets set to | ||
// -1 when the item is removed from the pool. | ||
index *int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why -1 if it's a pointer? does niling it out not work? but -1 is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We copy the pointer to a Handle
, so having an underlying pointer makes it easy to detect (in Peek
/etc.) whether an item has been removed just given the Handle
a33c6b8
to
e1f9c42
Compare
This can be used to implement a persistent pool (i.e. one that isn't cleared like sync.Pool is) of items–e.g. database connections. Some benchmarks vs. a naive implementation that uses a single map iteration show a pretty meaningful improvement: $ benchstat -col /impl ./bench.txt goos: darwin goarch: arm64 pkg: tailscale.com/util/pool │ Pool │ map │ │ sec/op │ sec/op vs base │ Pool_AddDelete-10 10.56n ± 2% 15.11n ± 1% +42.97% (p=0.000 n=10) Pool_TakeRandom-10 56.75n ± 4% 1899.50n ± 20% +3246.84% (p=0.000 n=10) geomean 24.49n 169.4n +591.74% Updates tailscale/corp#19900 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: Ie509cb65573c4726cfc3da9a97093e61c216ca18
e1f9c42
to
3048e98
Compare
This can be used to implement a persistent pool (i.e. one that isn't cleared like sync.Pool is) of items–e.g. database connections.
Some benchmarks vs. a naive implementation that uses a single map iteration show a pretty meaningful improvement:
Updates https://github.com/tailscale/corp/issues/19900
Change-Id: Ie509cb65573c4726cfc3da9a97093e61c216ca18