Skip to content

Optimize ConvertToBytes by avoiding unnecessary string <-> bytes conversions and copies.#2932

Merged
nicktobey merged 3 commits intomainfrom
nicktobey/wrapper-bytes1
Apr 10, 2025
Merged

Optimize ConvertToBytes by avoiding unnecessary string <-> bytes conversions and copies.#2932
nicktobey merged 3 commits intomainfrom
nicktobey/wrapper-bytes1

Conversation

@nicktobey
Copy link
Copy Markdown
Contributor

@nicktobey nicktobey commented Apr 10, 2025

ConvertToBytes is a commonly called function to get the string representation of a value. However, it has some unnecessary allocations where a child function allocates a byte buffer, only for the result to be copied into the buffer provided by the parent function. In other places we needlessly round-trip between string and []byte.

This PR improves performance by removing some of these unneeded copies. In places where ConvertToBytes calls a function that allocates a buffer (instead of using the buffer that ConvertToBytes can provide), we can optimize by using the returned value without copying it again.

Dolt shows a 7% improvement in the types_table_scan benchmark.

We can potentially do even better by allowing these child functions to take a buffer, removing the need for an extra allocation.

Copy link
Copy Markdown
Contributor

@max-hoffman max-hoffman left a comment

Choose a reason for hiding this comment

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

LGTM!

func UnquoteBytes(b []byte) ([]byte, error) {
outIdx := 0
for i := 0; i < len(b); i++ {
if b[i] == '\\' {
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.

huh, didn't realize \\ was a single char

Copy link
Copy Markdown
Contributor Author

@nicktobey nicktobey Apr 10, 2025

Choose a reason for hiding this comment

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

You got to escape the backslash character. '\' would be escaping the closing quote.

@nicktobey nicktobey merged commit 7c05ba9 into main Apr 10, 2025
7 of 8 checks passed
@nicktobey nicktobey deleted the nicktobey/wrapper-bytes1 branch April 10, 2025 21:25
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.

2 participants