perf: Optimize SQL string construction#13
Merged
Conversation
The `SQL()` function in `sql_writing.go` was inefficiently building the SQL string. It used an intermediate formatted string created with a `bytes.Buffer`, and then performed multiple `strings.ReplaceAll` calls to clean it up. This change introduces a custom `io.Writer` implementation, `onelineWriter`, that builds the final, single-line SQL string in a single pass. It replaces tabs and newlines with spaces and collapses multiple spaces into one on the fly. This new approach reduces memory allocations and CPU usage, improving the performance of every query built by the library.
This commit introduces two significant performance improvements to the pgtalk library.
1. **Optimized SQL String Construction:**
The `SQL()` function in `sql_writing.go` was inefficiently building the SQL string using multiple `strings.ReplaceAll` calls. This has been replaced with a custom `io.Writer` implementation (`onelineWriter`) that builds the final, single-line SQL string in a single pass, reducing memory allocations and CPU usage.
2. **Optimized Result Set Iteration:**
The `resultIterator.Next()` method was inefficiently matching result columns to selectors using a nested loop for every row. The iteration process has been optimized by pre-ordering the selectors to match the result set's field order. This matching is now done only once, before the iteration begins, significantly improving the performance of row scanning.
This commit introduces two significant performance improvements to the pgtalk library.
1. **Optimized SQL String Construction:**
The `SQL()` function in `sql_writing.go` was inefficiently building the SQL string using multiple `strings.ReplaceAll` calls. This has been replaced with a custom `io.Writer` implementation (`onelineWriter`) that builds the final, single-line SQL string in a single pass, reducing memory allocations and CPU usage.
2. **Optimized Result Set Iteration:**
The `resultIterator.Next()` method was inefficiently matching result columns to selectors using a nested loop for every row. The iteration process has been optimized by pre-ordering the selectors to match the result set's field order. This matching is now done only once, before the iteration begins, significantly improving the performance of row scanning.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes SQL string construction by replacing inefficient string manipulation with a custom writer implementation. The optimization improves performance by reducing memory allocations and CPU usage when building SQL queries.
Key changes:
- Introduces
onelineWriterthat formats SQL strings in a single pass instead of multiple string replacements - Optimizes result iteration by pre-ordering column selectors to avoid nested loops
- Refactors iterator logic to use ordered selectors for improved performance
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| sql_writing.go | Implements onelineWriter and refactors SQL() function for single-pass formatting |
| queryset.go | Pre-orders selectors using a map lookup to optimize result iteration |
| mutationset.go | Applies similar selector ordering optimization for mutation results |
| iterator.go | Updates to use pre-ordered selectors, eliminating nested loops in Next() |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
SQL()function insql_writing.gowas inefficiently building the SQL string. It used an intermediate formatted string created with abytes.Buffer, and then performed multiplestrings.ReplaceAllcalls to clean it up.This change introduces a custom
io.Writerimplementation,onelineWriter, that builds the final, single-line SQL string in a single pass. It replaces tabs and newlines with spaces and collapses multiple spaces into one on the fly.This new approach reduces memory allocations and CPU usage, improving the performance of every query built by the library.