forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix exponential memory allocation in SQLiteConn.Exec
This commit fixes SQLiteConn.Exec to not duplicate the provided SQL statement and to pass the Go string to libsqlite3 instead of creating a C string copy and passing that. The issue with the previous implementation was that it would create a new C string copy of the original Go string each time it stepped through a SQL statement. This led to exponential memory usage when Exec'ing an argument that contained multiple SQL statements (which is common when initializing a database from a SQL dump). This commit is a slimmed down version of PR mattn#1133: mattn#1133 ``` goos: darwin goarch: arm64 pkg: github.com/mattn/go-sqlite3 cpu: Apple M1 Max │ b.txt │ n.txt │ │ sec/op │ sec/op vs base │ Suite/BenchmarkExec-10 1.278µ ± 1% 1.022µ ± 2% -19.99% (p=0.000 n=10) Suite/BenchmarkExecStep-10 1762.9µ ± 0% 900.9µ ± 1% -48.90% (p=0.000 n=10) geomean 47.47µ 30.35µ -36.06% │ b.txt │ n.txt │ │ B/op │ B/op vs base │ Suite/BenchmarkExec-10 128.0 ± 0% 120.0 ± 0% -6.25% (p=0.000 n=10) Suite/BenchmarkExecStep-10 5279.94Ki ± 0% 31.34Ki ± 0% -99.41% (p=0.000 n=10) geomean 25.69Ki 1.916Ki -92.54% │ b.txt │ n.txt │ │ allocs/op │ allocs/op vs base │ Suite/BenchmarkExec-10 7.000 ± 0% 6.000 ± 0% -14.29% (p=0.000 n=10) Suite/BenchmarkExecStep-10 7.000k ± 0% 3.003k ± 0% -57.10% (p=0.000 n=10) geomean 221.4 134.2 -39.36% ```
- Loading branch information
1 parent
b3e6ac1
commit 6915212
Showing
4 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build !go1.21 | ||
// +build !go1.21 | ||
|
||
package sqlite3 | ||
|
||
import "unsafe" | ||
|
||
// stringData is a safe version of unsafe.StringData that handles empty strings. | ||
func stringData(s string) *byte { | ||
if len(s) != 0 { | ||
b := *(*[]byte)(unsafe.Pointer(&s)) | ||
return &b[0] | ||
} | ||
// The return value of unsafe.StringData | ||
// is unspecified if the string is empty. | ||
return &placeHolder[0] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
// The unsafe.StringData function was made available in Go 1.20 but it | ||
// was not until Go 1.21 that Go was changed to interpret the Go version | ||
// in go.mod (1.19 as of writing this) as the minimum version required | ||
// instead of the exact version. | ||
// | ||
// See: https://github.com/golang/go/issues/59033 | ||
|
||
package sqlite3 | ||
|
||
import "unsafe" | ||
|
||
// stringData is a safe version of unsafe.StringData that handles empty strings. | ||
func stringData(s string) *byte { | ||
if len(s) != 0 { | ||
return unsafe.StringData(s) | ||
} | ||
// The return value of unsafe.StringData | ||
// is unspecified if the string is empty. | ||
return &placeHolder[0] | ||
} |