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

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

Merged
merged 1 commit into from May 24, 2024

Conversation

andrew-d
Copy link
Member

@andrew-d andrew-d commented May 10, 2024

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 andrew-d requested review from bradfitz and maisem May 10, 2024 18:28
@andrew-d
Copy link
Member Author

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

@andrew-d andrew-d force-pushed the andrew/util-pool branch 2 times, most recently from 2606285 to a33c6b8 Compare May 13, 2024 15:16
util/pool/pool.go Outdated Show resolved Hide resolved
util/pool/pool.go Outdated Show resolved Hide resolved
Comment on lines +32 to +35
// 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
Copy link
Member

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.

Copy link
Member Author

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

util/pool/pool.go Outdated Show resolved Hide resolved
util/pool/pool.go Outdated Show resolved Hide resolved
util/pool/pool.go Outdated Show resolved Hide resolved
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
@andrew-d andrew-d merged commit 8e4a294 into main May 24, 2024
48 checks passed
@andrew-d andrew-d deleted the andrew/util-pool branch May 24, 2024 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants