From 44f7062ac27a4f38d3927c7ea8ece149489c968a Mon Sep 17 00:00:00 2001 From: Nenavizhu Leto Date: Wed, 11 Feb 2026 20:35:19 +0500 Subject: [PATCH 1/2] feat: improved ReplacePlaceholders performance Changed use of fmt.Fprintf to strconv.Itoa 1. Perfomance increase by a factor of 3 (from ~150ns/op to ~50ns/op) 2. Cut allocations by half (from 4 to 2) --- placeholder.go | 4 +++- placeholder_test.go | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/placeholder.go b/placeholder.go index 8e97a6c6..dd1760bf 100644 --- a/placeholder.go +++ b/placeholder.go @@ -3,6 +3,7 @@ package squirrel import ( "bytes" "fmt" + "strconv" "strings" ) @@ -104,7 +105,8 @@ func replacePositionalPlaceholders(sql, prefix string) (string, error) { } else { i++ buf.WriteString(sql[:p]) - fmt.Fprintf(buf, "%s%d", prefix, i) + buf.WriteString(prefix) + buf.WriteString(strconv.Itoa(i)) sql = sql[p+1:] } } diff --git a/placeholder_test.go b/placeholder_test.go index 8e7f43f4..d720692f 100644 --- a/placeholder_test.go +++ b/placeholder_test.go @@ -65,3 +65,11 @@ func BenchmarkPlaceholdersArray(b *testing.B) { func BenchmarkPlaceholdersStrings(b *testing.B) { Placeholders(b.N) } + +func BenchmarkReplacePlaceholders(b *testing.B) { + var ph dollarFormat + + for i := 0; i < b.N; i++ { + ph.ReplacePlaceholders("?") + } +} From ba486944247eeb1383dd89e2ed6e52ed22849df9 Mon Sep 17 00:00:00 2001 From: Nenavizhu Leto Date: Wed, 11 Feb 2026 20:45:46 +0500 Subject: [PATCH 2/2] fix: unused imports --- placeholder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/placeholder.go b/placeholder.go index dd1760bf..6771edc3 100644 --- a/placeholder.go +++ b/placeholder.go @@ -2,7 +2,6 @@ package squirrel import ( "bytes" - "fmt" "strconv" "strings" )