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

improve converting to Go string performance #66

Merged
merged 2 commits into from
Jan 9, 2024

Conversation

ffmiruz
Copy link
Contributor

@ffmiruz ffmiruz commented Jan 8, 2024

Due to out of place performance on this bench for large case i thought there could be easy performance wins.
Profiling on that, performance hit as the string got bigger due to this loop https://github.com/zombiezen/go-sqlite/blob/f46dbf8b767e074a46642cd87c3d0e0cafcc79b2/sqlite.go#L1297C2-L1308

This PR instead convert to unsafe string all at once and then clone it to get safe string. The performance for 1 byte string is the same and then keep improving as the string get bigger.
~40% improvement for the large case on the benchmark. Also improve performance of other benchmark cases slightly.

Copy link
Owner

@zombiezen zombiezen left a comment

Choose a reason for hiding this comment

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

Thank you so much for getting to the bottom of this! I shuffled the code around slightly so that it's converting from []byte to string to avoid the strings.Clone call, which seems a little subtle to me.

I realized I don't have benchmarks for this library generally, so I added a small microbenchmark that proves this speedup. Went from 30ns/op to 15ns/op on my machine, nice work!

@zombiezen zombiezen merged commit d6efc7d into zombiezen:main Jan 9, 2024
8 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.

2 participants