Skip to content

logstore: pool MVCCStats to avoid hot path allocs#113742

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
pav-kv:pool-mvccstats
Nov 3, 2023
Merged

logstore: pool MVCCStats to avoid hot path allocs#113742
craig[bot] merged 1 commit intocockroachdb:masterfrom
pav-kv:pool-mvccstats

Conversation

@pav-kv
Copy link
Copy Markdown
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

@blathers-crl
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 PAST MAINTENANCE SUPPORT: 23.2 patch releases via ER request only label Nov 3, 2023
@pav-kv
Copy link
Copy Markdown
Collaborator Author

pav-kv commented Nov 3, 2023

bors r=erikgrinaker

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 3, 2023

Build succeeded:

@craig craig bot merged commit be845d1 into cockroachdb:master Nov 3, 2023
@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 PAST MAINTENANCE SUPPORT: 23.2 patch releases via ER request only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants