Skip to content

colencoding: reuse scratch space when key decoding bytes and decimals#70734

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
yuzefovich:decoding-scratch
Sep 28, 2021
Merged

colencoding: reuse scratch space when key decoding bytes and decimals#70734
craig[bot] merged 1 commit intocockroachdb:masterfrom
yuzefovich:decoding-scratch

Conversation

@yuzefovich
Copy link
Copy Markdown
Member

@yuzefovich yuzefovich commented Sep 24, 2021

When we're key decoding bytes-like columns, we need to use the scratch
byte slice to decode into (in case of decimals, we might need the space
temporarily). Previously, we would always allocate a new byte slice only
to deep copy it later when calling coldata.Bytes.Set. This commit
teaches the cFetcher and the relevant decoding methods to reuse the same
scratch space which should reduce the memory allocations.

One notable change is that now when we're calling
DecodeBytesAscending, we have to make sure to perform a deep copy so
that it is safe to reuse the returned value as the scratch space in the
future.

Release note: None

@yuzefovich yuzefovich requested review from a team, mgartner and michae2 September 24, 2021 23:23
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@yuzefovich
Copy link
Copy Markdown
Member Author

Okay, this doesn't just work, yet.

@yuzefovich yuzefovich force-pushed the decoding-scratch branch 2 times, most recently from 7bed2fe to 5d469ae Compare September 25, 2021 17:43
@yuzefovich yuzefovich added the do-not-merge bors won't merge a PR with this label. label Sep 25, 2021
@yuzefovich yuzefovich force-pushed the decoding-scratch branch 2 times, most recently from 520f480 to d78f0aa Compare September 27, 2021 17:19
@yuzefovich yuzefovich removed the do-not-merge bors won't merge a PR with this label. label Sep 27, 2021
@yuzefovich
Copy link
Copy Markdown
Member Author

I think it should now work. RFAL.

Copy link
Copy Markdown
Contributor

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)


pkg/sql/colencoding/key_encoding.go, line 294 at r1 (raw file):

		}
		// Note that since we're about to perform a deep copy, it'll be ok to
		// return the scratch slice to be reused by the caller.

I don't understand this comment. Since we've already deep-copied, why does it matter that Set deep-copies or not?

Copy link
Copy Markdown
Member Author

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner and @michae2)


pkg/sql/colencoding/key_encoding.go, line 294 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I don't understand this comment. Since we've already deep-copied, why does it matter that Set deep-copies or not?

There are two concerns at play: one is about not referencing the memory used by key (to allow for GC to run on the batch response), another one is making sure it is ok to return scratch in such a manner that if scratch is later modified, then vec.Bytes().Get(idx) will still return the correct result. The first concern is addressed by making a deep-copy when decoding, the second concern is addressed automatically by vec.Bytes().Set() which performs the deep copy. The comment is about the second concern.

Do you think it's worth extending the comment? Or maybe removing it to reduce possible confusion?

Copy link
Copy Markdown
Contributor

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)


pkg/sql/colencoding/key_encoding.go, line 294 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

There are two concerns at play: one is about not referencing the memory used by key (to allow for GC to run on the batch response), another one is making sure it is ok to return scratch in such a manner that if scratch is later modified, then vec.Bytes().Get(idx) will still return the correct result. The first concern is addressed by making a deep-copy when decoding, the second concern is addressed automatically by vec.Bytes().Set() which performs the deep copy. The comment is about the second concern.

Do you think it's worth extending the comment? Or maybe removing it to reduce possible confusion?

Got it. I think maybe just reword it to something like: "Set() performs a deep copy, so it is safe to return the scratch slice to the caller. Any modifications to the scratch slice made by the caller will not affect the value in the vector".

@yuzefovich yuzefovich requested a review from a team as a code owner September 28, 2021 17:36
When we're key decoding bytes-like columns, we need to use the scratch
byte slice to decode into (in case of decimals, we might need the space
temporarily). Previously, we would always allocate a new byte slice only
to deep copy it later when calling `coldata.Bytes.Set`. This commit
teaches the cFetcher and the relevant decoding methods to reuse the same
scratch space which should reduce the memory allocations.

One notable change is that now when we're calling
`DecodeBytesAscending`, we have to make sure to perform a deep copy so
that it is safe to reuse the returned value as the scratch space in the
future.

Release note: None
Copy link
Copy Markdown
Member Author

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

TFTR!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @mgartner and @michae2)

@yuzefovich yuzefovich removed the request for review from a team September 28, 2021 17:39
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Sep 28, 2021

Build succeeded:

@craig craig bot merged commit bdb4c1a into cockroachdb:master Sep 28, 2021
@yuzefovich yuzefovich deleted the decoding-scratch branch September 28, 2021 21:02
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