tailscale / tailscale

The easiest, most secure way to use WireGuard and 2FA.
https://tailscale.com
BSD 3-Clause "New" or "Revised" License
17.22k stars 1.31k forks source link

util/pool: add package for storing and using a pool of items #12091

Closed andrew-d closed 2 weeks ago

andrew-d commented 1 month ago

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 https://github.com/tailscale/corp/issues/19900

Change-Id: Ie509cb65573c4726cfc3da9a97093e61c216ca18

andrew-d commented 1 month ago

@bradfitz you may recognize the shape of this as being inspired by your change in database/sql