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

sstable: generate streamlined blockIter code #3373

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RaduBerinde
Copy link
Member

sstable: separate blockIter code

sstable: generate streamlined blockIter code

We automatically generate a "streamlined" version of the blockIter code (in
block_iter_streamlined.gen.go). The streamlined code can be used in the
common case and does not support all features. This improves performance by
reducing the conditionals in the hot path.

The streamlinedBlockIter constant can be used in if statements; in the
streamlined version, it gets replaced by "true" (allowing the compiler to
omit blocks of code from the streamlined version).

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@RaduBerinde RaduBerinde force-pushed the block-code-gen branch 2 times, most recently from fdf1c5e to 9ee5a1f Compare March 2, 2024 00:30
@RaduBerinde
Copy link
Member Author

sstable/block_iter_streamlined.gen.go line 19 at r2 (raw file):

func (i *blockIter) streamlinedReadEntry() {
	ptr := unsafe.Pointer(uintptr(i.ptr) + uintptr(i.offset))

Comments directly inside the method are missing. I couldn't quite figure out how to fix that, but it doesn't really matter (everyone should always work with the original code).

@RaduBerinde RaduBerinde force-pushed the block-code-gen branch 4 times, most recently from 126e941 to 53a81d3 Compare March 2, 2024 15:01
Copy link
Collaborator

@petermattis petermattis left a comment

Choose a reason for hiding this comment

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

Looks straightforward and understandable. Other storage folks should definitely give this a review, but I'm thumbs-up on an approach like this.

Reviewable status: 0 of 7 files reviewed, 4 unresolved discussions (waiting on @RaduBerinde)


sstable/block-iter-codegen/main.go line 5 at r4 (raw file):

// the LICENSE file.

package main

It's worthwhile to add a comment explaining what this code is doing. It is relatively straightforward, but I still suspect our future selves will thank us. Especially if this code grows more complex over time.


sstable/block-iter-codegen/main.go line 18 at r4 (raw file):

)

var methods = map[string]struct{}{

Rather than maintaining a list of methods like this, could you automatically deduce the methods by walking over the function declarations and marking any method which references the streamlinedBlockIter constant and any method which transitively calls such a method as needing a rewrite? I'm hoping you'll get the same list (though you might want to exclude init), but then we don't have the possibility of introducing another streamlinedBlockIter path and forget to update this list.


sstable/block-iter-codegen/main.go line 46 at r4 (raw file):

	}

	// Rename streamlinedBlockIter ot streamlinedBlockIterTrue.

Is this comment still valid? Seems like you're doing more than what the comment states.

Nit: s/ot/to/g

Copy link
Member Author

@RaduBerinde RaduBerinde 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: 0 of 7 files reviewed, 4 unresolved discussions (waiting on @petermattis)


sstable/block-iter-codegen/main.go line 5 at r4 (raw file):

Previously, petermattis (Peter Mattis) wrote…

It's worthwhile to add a comment explaining what this code is doing. It is relatively straightforward, but I still suspect our future selves will thank us. Especially if this code grows more complex over time.

Agreed, it's just an initial draft


sstable/block-iter-codegen/main.go line 18 at r4 (raw file):

Previously, petermattis (Peter Mattis) wrote…

Rather than maintaining a list of methods like this, could you automatically deduce the methods by walking over the function declarations and marking any method which references the streamlinedBlockIter constant and any method which transitively calls such a method as needing a rewrite? I'm hoping you'll get the same list (though you might want to exclude init), but then we don't have the possibility of introducing another streamlinedBlockIter path and forget to update this list.

Interesting. I think I'd need some more advanced walking infrastructure than Inspect. Might not be worth the trouble, but I'll give it a shot

Copy link
Collaborator

@petermattis petermattis 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: 0 of 7 files reviewed, 4 unresolved discussions (waiting on @RaduBerinde)


sstable/block-iter-codegen/main.go line 18 at r4 (raw file):

Previously, RaduBerinde wrote…

Interesting. I think I'd need some more advanced walking infrastructure than Inspect. Might not be worth the trouble, but I'll give it a shot

Yeah, my suggestion may not be worth the effort. Perhaps just leave a TODO comment in case this becomes some sort of headache in the future.

@RaduBerinde
Copy link
Member Author

sstable/block-iter-codegen/main.go line 18 at r4 (raw file):

Previously, petermattis (Peter Mattis) wrote…

Yeah, my suggestion may not be worth the effort. Perhaps just leave a TODO comment in case this becomes some sort of headache in the future.

I looked at it briefly and I think it's not bad at all, when I get it working I'll update and we can decide then.

@RaduBerinde RaduBerinde force-pushed the block-code-gen branch 2 times, most recently from 6275b88 to 2901c89 Compare March 4, 2024 19:41
Copy link
Member Author

@RaduBerinde RaduBerinde 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: 0 of 7 files reviewed, 4 unresolved discussions (waiting on @petermattis)


sstable/block-iter-codegen/main.go line 5 at r4 (raw file):

Previously, RaduBerinde wrote…

Agreed, it's just an initial draft

Done.


sstable/block-iter-codegen/main.go line 18 at r4 (raw file):

Previously, RaduBerinde wrote…

I looked at it briefly and I think it's not bad at all, when I get it working I'll update and we can decide then.

Done.

Copy link
Collaborator

@petermattis petermattis left a comment

Choose a reason for hiding this comment

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

This all looks good to me, but let's get some other Storage 👀 on this PR.

Reviewable status: 0 of 7 files reviewed, 4 unresolved discussions (waiting on @RaduBerinde)


sstable/block_iter_streamlined.gen.go line 19 at r2 (raw file):

Previously, RaduBerinde wrote…

Comments directly inside the method are missing. I couldn't quite figure out how to fix that, but it doesn't really matter (everyone should always work with the original code).

Hmm, I thought that parser.ParseComments flag would have handled keeping the comments around. Perhaps there is a similar flag that needs to be specified when outputting the AST. I see there is a printer.CommentedNode type. I agree that this isn't worth worrying about.


sstable/block-iter-codegen/main.go line 18 at r4 (raw file):

Previously, RaduBerinde wrote…

Done.

Nice!


sstable/block-iter-codegen/main.go line 10 at r5 (raw file):

//
// The block_iter.go code uses a special constant streamlinedBlockIter which is
// always false in th code. This program generates streamlined versions of the

Nit: s/th/the/g

Copy link
Member Author

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

TFTR! I will rope in some more reviewers once I wire it up and maybe demonstrate some gains by inlining the crdb comparer.

Reviewable status: 0 of 7 files reviewed, 3 unresolved discussions (waiting on @petermattis)


sstable/block_iter_streamlined.gen.go line 19 at r2 (raw file):

Previously, petermattis (Peter Mattis) wrote…

Hmm, I thought that parser.ParseComments flag would have handled keeping the comments around. Perhaps there is a similar flag that needs to be specified when outputting the AST. I see there is a printer.CommentedNode type. I agree that this isn't worth worrying about.

Yeah, me too. It does keep most comments, just not the ones that are at the top level inside the body. Apparently they are in separate nodes or something like that.

@RaduBerinde
Copy link
Member Author

sstable/block_iter_streamlined.gen.go line 19 at r2 (raw file):

Previously, RaduBerinde wrote…

Yeah, me too. It does keep most comments, just not the ones that are at the top level inside the body. Apparently they are in separate nodes or something like that.

Yeah, the missing ones all show up under ast.File.Comments

We automatically generate a "streamlined" version of the blockIter code (in
`block_iter_streamlined.gen.go`). The streamlined code can be used in the
common case and does not support all features. This improves performance by
reducing the conditionals in the hot path.

The streamlinedBlockIter constant can be used in if statements; in the
streamlined version, it gets replaced by "true" (allowing the compiler to
omit blocks of code from the streamlined version).
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