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

kv: avoid extra key allocations in optimizePuts #104669

Merged
merged 1 commit into from
Jun 27, 2023

Conversation

cucaroach
Copy link
Contributor

The code correctly pointed out that the lookup didn't allocate a copy,
the go compiler intelligently constructs a string on the stack pointing
directly to the byte slice. However if the string isn't in the map it
has to allocate a new string for the put. Now we avoid that.

Release note: none
Epic: none

@blathers-crl
Copy link

blathers-crl bot commented Jun 9, 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

@cucaroach cucaroach marked this pull request as ready for review June 9, 2023 16:11
@cucaroach cucaroach requested a review from a team as a code owner June 9, 2023 16:11
@cucaroach cucaroach requested a review from tbg June 9, 2023 16:11
Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

Nice catch! Do you have benchmark results that demonstrate the impact of this change?

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


pkg/kv/kvserver/replica_evaluate.go line 58 at r1 (raw file):

	// Returns false on occurrence of a duplicate key.
	maybeAddPut := func(key roachpb.Key) bool {
		// The lookup will not copy key but the put will, but since map doesn't

s/the put/the map insertion/


pkg/kv/kvserver/replica_evaluate.go line 61 at r1 (raw file):

		// escape and we don't mutate the keys its safe to not
		// allocate for both.
		mapKey := *(*string)(unsafe.Pointer(&key))

Want to call encoding.UnsafeConvertBytesToString here?

Copy link
Contributor Author

@cucaroach cucaroach 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 @nvanbenschoten and @tbg)


pkg/kv/kvserver/replica_evaluate.go line 58 at r1 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

s/the put/the map insertion/

Done


pkg/kv/kvserver/replica_evaluate.go line 61 at r1 (raw file):

Previously, nvanbenschoten (Nathan VanBenschoten) wrote…

Want to call encoding.UnsafeConvertBytesToString here?

At first I didn't want to introduce a new dependency but now I see other files in this package already depend on util/encoding so +1.

@cucaroach
Copy link
Contributor Author

Nice catch! Do you have benchmark results that demonstrate the impact of this change?

I tried a couple things but failed to extract any meaningful signal, this cost is just a drop in the bucket it seems. But its good to fix and maybe some day when the rest of the bulk insert path is better optimized changes like this will become more important.

@tbg tbg removed their request for review June 14, 2023 10:09
Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @cucaroach)

@cucaroach
Copy link
Contributor Author

bors r=nvanbenschoten

@craig
Copy link
Contributor

craig bot commented Jun 26, 2023

Build failed:

The code correctly pointed out that the lookup didn't allocate a copy,
the go compiler intelligently constructs a string on the stack pointing
directly to the byte slice. However if the string isn't in the map it
has to allocate a new string for the put. Now we avoid that.

Release note: none
Epic: none
@cucaroach
Copy link
Contributor Author

bors r=nvanbenschoten

@craig
Copy link
Contributor

craig bot commented Jun 27, 2023

Build succeeded:

@craig craig bot merged commit 4135fd1 into cockroachdb:master Jun 27, 2023
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