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

storage: reduce allocations when encoding MVCCValue's #120354

Merged
merged 2 commits into from
Mar 13, 2024

Conversation

stevendanna
Copy link
Collaborator

@stevendanna stevendanna commented Mar 12, 2024

This adds a new function that lets us pass a buffer to
use when encoding MVCC values. This is very useful in loops where we
might be allocating many MVCC values in a loop, such as in
ExportRequest or in SSTBatcher.

Before:

BenchmarkMVCCExportToSST/importEpochs=false/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                  1        1464855709 ns/op         45293904 B/op      39419 allocs/op
BenchmarkMVCCExportToSST/importEpochs=true/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                   1        1919207875 ns/op        207366256 B/op   6595176 allocs/op

After:

BenchmarkMVCCExportToSST/importEpochs=false/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                  1        1458935583 ns/op        45327256 B/op      39337 allocs/op
BenchmarkMVCCExportToSST/importEpochs=true/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                   1        1756803250 ns/op        49162648 B/op      41369 allocs/op

Epic: none
Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@stevendanna stevendanna force-pushed the allocator-plumbing branch 3 times, most recently from 51f7bb7 to 5315a0c Compare March 12, 2024 18:25
@stevendanna stevendanna changed the title wip: reduce allocations when encoding MVCCValue's storage: reduce allocations when encoding MVCCValue's Mar 12, 2024
@stevendanna stevendanna marked this pull request as ready for review March 12, 2024 19:18
@stevendanna stevendanna requested review from a team as code owners March 12, 2024 19:18
@stevendanna stevendanna requested review from itsbilal, dt, michae2, erikgrinaker, a team and DrewKimball and removed request for a team, itsbilal, michae2 and erikgrinaker March 12, 2024 19:18
@stevendanna stevendanna removed the request for review from DrewKimball March 12, 2024 19:19
@stevendanna
Copy link
Collaborator Author

Only review the last commit.

@stevendanna stevendanna force-pushed the allocator-plumbing branch 4 times, most recently from dac6d99 to 277c18f Compare March 12, 2024 20:35
Comment on lines 249 to 197
// TODO(ssd): Dedup with EncodeMVCCValue.
func EncodeMVCCValueToBuf(v MVCCValue, buf []byte) ([]byte, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've opted to duplicate this code for now to avoid touching EncodeMVCCValue at all.

@@ -193,6 +194,8 @@ type SSTBatcher struct {

asyncAddSSTs ctxgroup.Group

valueBuf bufalloc.ByteAllocator
Copy link
Member

Choose a reason for hiding this comment

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

this is a this is just scratch space, right? this isn't really so much about amortizing lots of small per key allocations into a few rare big ones, but about reusing the same buffer for every key, right?

Just making sure I'm reading it right.

I found bufalloc a little confusing for a pure scratch usecase, but maybe just naming the valScratch would have avoided that?

// the encoding scheme.
//
// If extended encoding is required, the given buffer will be used. If
// the provided buffer is not large enough an error is
Copy link
Member

Choose a reason for hiding this comment

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

2¢:
Instead of an error I'd just allocate a bigger buffer and return that. This makes your TODO easily implemented by EncodeMVCCValueToBuf(x) { return EncodeMVCCValueToBuf(x, nil) }.

And then I'd probably swap the bufalloc for just scratch []byte and call it as scratch, err = EncodeMVCCValueToBuf(x, scratch[:0]) so that if it allocates, you keep the result?

/2¢

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Very nice. This will clean things up a lot.

This adds a new function that lets us pass a pre-allocated buffer to
use when encoding MVCC values. This is very useful in loops where we
might be allocating many MVCC values in a loop, such as in
ExportRequest or in SSTBatcher.

Before:

```
BenchmarkMVCCExportToSST/importEpochs=false/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                  1        1464855709 ns/op         45293904 B/op      39419 allocs/op
BenchmarkMVCCExportToSST/importEpochs=true/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                   1        1919207875 ns/op        207366256 B/op   6595176 allocs/op
```

After:

```
BenchmarkMVCCExportToSST/importEpochs=false/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                  1        1458935583 ns/op        45327256 B/op      39337 allocs/op
BenchmarkMVCCExportToSST/importEpochs=true/numKeys=65536/numRevisions=100/exportAllRevisions=true-10                   1        1756803250 ns/op        49162648 B/op      41369 allocs/op
```

Epic: none
Release note: None
@stevendanna
Copy link
Collaborator Author

bors r=dt

@craig
Copy link
Contributor

craig bot commented Mar 13, 2024

Build failed:

@stevendanna
Copy link
Collaborator Author

bors retry

@craig
Copy link
Contributor

craig bot commented Mar 13, 2024

@craig craig bot merged commit 8da64fe into cockroachdb:master Mar 13, 2024
19 checks passed
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.

3 participants