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

logstore: pool MVCCStats to avoid hot path allocs #113742

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

pav-kv
Copy link
Collaborator

@pav-kv pav-kv commented Nov 3, 2023

Before this commit, logstore.logAppend allocated MVCCStats on heap despite its lifetime being confined within this function's scope.

This commit switches to allocating MVCCStats on a sync.Pool. Since logAppend already has another pooled roachpb.Value with exactly the same lifetime, coalesce the allocation of both and put them in the same pool. This makes the cost of this change zero.

Microbenchmark results:

// before
BenchmarkLogStore_StoreEntries/bytes=1.0_KiB-24 837254   1270 ns/op  333 B/op  1 allocs/op
// after
BenchmarkLogStore_StoreEntries/bytes=1.0_KiB-24 1000000  1153 ns/op  157 B/op  0 allocs/op

Part of #111561
Epic: none
Release note: none

Copy link

blathers-crl bot commented Nov 3, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@pav-kv pav-kv force-pushed the pool-mvccstats branch 2 times, most recently from 33530bc to a176594 Compare November 3, 2023 10:39
@pav-kv pav-kv requested a review from erikgrinaker November 3, 2023 10:40
@pav-kv pav-kv marked this pull request as ready for review November 3, 2023 10:40
@pav-kv pav-kv requested a review from a team November 3, 2023 10:40
Before this commit, logstore.logAppend allocated MVCCStats on heap
despite its lifetime being confined within this function's scope.

This commit switches to allocating MVCCStats on a sync.Pool. Since
logAppend already has another pooled roachpb.Value with exactly the same
lifetime, coalesce the allocation of both and put them in the same pool.
This makes the cost of this change zero.

Microbenchmark results:

```
// before
BenchmarkLogStore_StoreEntries/bytes=1.0_KiB-24 837254   1270 ns/op  333 B/op  1 allocs/op
// after
BenchmarkLogStore_StoreEntries/bytes=1.0_KiB-24 1000000  1153 ns/op  157 B/op  0 allocs/op
```

Epic: none
Release note: none
Copy link
Contributor

@erikgrinaker erikgrinaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that we could reuse the pool.

Comment on lines +365 to +368
return new(struct {
roachpb.Value
enginepb.MVCCStats
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: may as well give this type a name, but don't really care either way.

Copy link
Collaborator Author

@pav-kv pav-kv Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, I think that's one of the cases where it's somewhat beneficial to not introduce a type explicitly (so that there is zero encouragement to use it elsewhere). There is exactly one caller/user of this type, and it's tied to the pool, and the pool is tied to a single function (also a reason why I renamed the pool after the function's name).

@erikgrinaker
Copy link
Contributor

erikgrinaker commented Nov 3, 2023

Btw, see benchstat for computing benchmark diffs from results.

https://pkg.go.dev/golang.org/x/perf/cmd/benchstat

@pav-kv pav-kv added the backport-23.2.x Flags PRs that need to be backported to 23.2. label Nov 3, 2023
@pav-kv
Copy link
Collaborator Author

pav-kv commented Nov 3, 2023

bors r=erikgrinaker

@craig
Copy link
Contributor

craig bot commented Nov 3, 2023

Build succeeded:

@craig craig bot merged commit be845d1 into cockroachdb:master Nov 3, 2023
3 checks passed
@pav-kv pav-kv deleted the pool-mvccstats branch November 3, 2023 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.2.x Flags PRs that need to be backported to 23.2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants