perf(builder): replace fmt.Sprintf with string concatenation in hot paths - #59
Open
VojtechVitek wants to merge 2 commits into
Open
perf(builder): replace fmt.Sprintf with string concatenation in hot paths#59VojtechVitek wants to merge 2 commits into
VojtechVitek wants to merge 2 commits into
Conversation
Add benchmarks for Sort.String() (one call per ORDER BY column on every paginated/keyset build) and RawQuery/RawSQL.Prepare (the ?->$N rewrite), plus a test pinning their exact output strings. Baselines for the fmt.Sprintf -> string-concatenation change in the next commit; the output test must stay green across it. Pure, no database. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
fmt.Sprintf runs a format-string parser and boxes its args into
interface{} (one alloc each); for fixed "col ORDER" / "$N" shapes plain
concatenation is several times cheaper. Replace it in three hot paths:
- Sort.String() (page.go) fmt.Sprintf("%s %s", ...) -> +
- Paginator.getOrder() (page.go) same
- RawSQL.Prepare() (querier.go) fmt.Sprintf("$%d", ...) -> strconv;
bytes.Buffer -> strings.Builder
(zero-copy String())
Output strings are unchanged (pinned by TestStringConcatOutputUnchanged).
Benchmarks, count=8, p=0.000:
SortString 124.9ns -> 68.4ns (-45%) 5 -> 2 allocs
RawQueryPrepare 322ns -> 145ns (-55%) 9 -> 4 allocs
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.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.
What
fmt.Sprintfruns a format-string parser and boxes each arg intointerface{}(one alloc apiece). For the fixed"col ORDER"and"$N"shapes, plain concatenation is several times cheaper. Replaced in three query-building hot paths:Sort.String()page.gofmt.Sprintf("%s %s", ...)→col + " " + orderPaginator.getOrder()page.goRawSQL.Prepare()querier.gofmt.Sprintf("$%d", ...)→strconv.Itoa;bytes.Buffer→strings.Builder(zero-copyString())Output strings are unchanged.
Commits
test(builder)— benchmarks forSort.String()andRawQuery/Prepare, plusTestStringConcatOutputUnchangedpinning the exact output strings.perf(builder)— the concatenation change.Result
count=8, all
p=0.000:SortStringRawQueryPrepareSort.String()runs once per ORDER BY column on every paginated/keyset query build.Testing
./tests/...passes.go build ./...,go vet ./clean.🤖 Generated with Claude Code