Commit e5d3f72
committed
sqlite3: reduce C to Go string conversions in SQLiteConn.{query,exec}
This commit fixes an issue where SQLiteConn.{exec,query} would use an
exponential amount of memory when processing a prepared statement that
consisted of multiple SQL statements ("stmt 1; stmt 2; ...").
Previously both exec/query used SQLiteConn.prepare() which converts the
"tail" pointer set by sqlite3_prepare_v2() back into a Go string, which
then has to be converted back to a C string for the next call to
prepare() (assuming there are multiple SQL statements in the provided
query).
This commit fixes this by changing both exec and query to use the
returned "tail" pointer for the next call to exec/query.
It also changes prepare() to use pointer arithmetic to calculate the
offset of the remaining "tail" portion of the query as a substring of
the original query. This saves a call to C.GoString() and an allocation.
Benchmarks:
```
goos: darwin
goarch: arm64
pkg: github.com/mattn/go-sqlite3
│ base.10.txt │ new.10.txt │
│ sec/op │ sec/op vs base │
Suite/BenchmarkExec-10 1.351µ ± 1% 1.247µ ± 1% -7.74% (p=0.000 n=10)
Suite/BenchmarkQuery-10 3.830µ ± 1% 3.558µ ± 1% -7.11% (p=0.000 n=10)
Suite/BenchmarkParams-10 4.221µ ± 0% 4.228µ ± 1% ~ (p=1.000 n=10)
Suite/BenchmarkStmt-10 2.906µ ± 1% 2.864µ ± 1% -1.45% (p=0.001 n=10)
Suite/BenchmarkRows-10 149.1µ ± 4% 148.2µ ± 1% -0.61% (p=0.023 n=10)
Suite/BenchmarkStmtRows-10 147.3µ ± 1% 145.6µ ± 0% -1.16% (p=0.000 n=10)
Suite/BenchmarkExecStep-10 1898.9µ ± 3% 889.0µ ± 1% -53.18% (p=0.000 n=10)
Suite/BenchmarkQueryStep-10 1848.0µ ± 1% 894.6µ ± 1% -51.59% (p=0.000 n=10)
geomean 38.56µ 31.30µ -18.84%
│ base.10.txt │ new.10.txt │
│ B/op │ B/op vs base │
Suite/BenchmarkExec-10 184.0 ± 0% 176.0 ± 0% -4.35% (p=0.000 n=10)
Suite/BenchmarkQuery-10 864.0 ± 0% 856.0 ± 0% -0.93% (p=0.000 n=10)
Suite/BenchmarkParams-10 1.289Ki ± 0% 1.281Ki ± 0% -0.61% (p=0.000 n=10)
Suite/BenchmarkStmt-10 1.078Ki ± 0% 1.078Ki ± 0% ~ (p=1.000 n=10) ¹
Suite/BenchmarkRows-10 34.45Ki ± 0% 34.45Ki ± 0% -0.02% (p=0.000 n=10)
Suite/BenchmarkStmtRows-10 34.40Ki ± 0% 34.40Ki ± 0% ~ (p=1.000 n=10) ¹
Suite/BenchmarkExecStep-10 5334.61Ki ± 0% 70.41Ki ± 0% -98.68% (p=0.000 n=10)
Suite/BenchmarkQueryStep-10 5397.4Ki ± 0% 133.2Ki ± 0% -97.53% (p=0.000 n=10)
geomean 17.06Ki 6.208Ki -63.62%
¹ all samples are equal
│ base.10.txt │ new.10.txt │
│ allocs/op │ allocs/op vs base │
Suite/BenchmarkExec-10 13.00 ± 0% 12.00 ± 0% -7.69% (p=0.000 n=10)
Suite/BenchmarkQuery-10 46.00 ± 0% 45.00 ± 0% -2.17% (p=0.000 n=10)
Suite/BenchmarkParams-10 54.00 ± 0% 53.00 ± 0% -1.85% (p=0.000 n=10)
Suite/BenchmarkStmt-10 49.00 ± 0% 49.00 ± 0% ~ (p=1.000 n=10) ¹
Suite/BenchmarkRows-10 2.042k ± 0% 2.041k ± 0% -0.05% (p=0.000 n=10)
Suite/BenchmarkStmtRows-10 2.038k ± 0% 2.038k ± 0% ~ (p=1.000 n=10) ¹
Suite/BenchmarkExecStep-10 13.000k ± 0% 8.004k ± 0% -38.43% (p=0.000 n=10)
Suite/BenchmarkQueryStep-10 11.013k ± 0% 6.017k ± 0% -45.36% (p=0.000 n=10)
geomean 418.6 359.8 -14.04%
¹ all samples are equal
```1 parent 7ce62b2 commit e5d3f72
2 files changed
+93
-29
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
82 | 93 | | |
83 | 94 | | |
84 | 95 | | |
| |||
99 | 110 | | |
100 | 111 | | |
101 | 112 | | |
102 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
103 | 118 | | |
104 | 119 | | |
105 | 120 | | |
| |||
122 | 137 | | |
123 | 138 | | |
124 | 139 | | |
125 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
126 | 145 | | |
127 | 146 | | |
128 | 147 | | |
| |||
848 | 867 | | |
849 | 868 | | |
850 | 869 | | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
851 | 877 | | |
852 | 878 | | |
853 | | - | |
854 | | - | |
855 | | - | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
856 | 883 | | |
| 884 | + | |
857 | 885 | | |
858 | | - | |
859 | | - | |
| 886 | + | |
860 | 887 | | |
861 | 888 | | |
862 | | - | |
| 889 | + | |
863 | 890 | | |
864 | 891 | | |
865 | 892 | | |
866 | 893 | | |
867 | 894 | | |
868 | | - | |
| 895 | + | |
869 | 896 | | |
870 | 897 | | |
871 | 898 | | |
| |||
874 | 901 | | |
875 | 902 | | |
876 | 903 | | |
877 | | - | |
| 904 | + | |
| 905 | + | |
878 | 906 | | |
879 | | - | |
| 907 | + | |
880 | 908 | | |
881 | 909 | | |
882 | 910 | | |
883 | 911 | | |
884 | | - | |
885 | | - | |
886 | | - | |
| 912 | + | |
| 913 | + | |
887 | 914 | | |
888 | 915 | | |
889 | 916 | | |
890 | 917 | | |
891 | 918 | | |
892 | 919 | | |
893 | | - | |
| 920 | + | |
894 | 921 | | |
895 | 922 | | |
896 | 923 | | |
| |||
907 | 934 | | |
908 | 935 | | |
909 | 936 | | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
910 | 944 | | |
911 | 945 | | |
912 | | - | |
913 | | - | |
914 | | - | |
915 | | - | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
916 | 950 | | |
917 | | - | |
| 951 | + | |
918 | 952 | | |
919 | 953 | | |
920 | 954 | | |
921 | 955 | | |
922 | 956 | | |
923 | 957 | | |
924 | 958 | | |
925 | | - | |
| 959 | + | |
926 | 960 | | |
927 | 961 | | |
928 | 962 | | |
| |||
931 | 965 | | |
932 | 966 | | |
933 | 967 | | |
934 | | - | |
| 968 | + | |
935 | 969 | | |
936 | | - | |
| 970 | + | |
937 | 971 | | |
938 | 972 | | |
939 | 973 | | |
940 | | - | |
941 | | - | |
| 974 | + | |
942 | 975 | | |
943 | 976 | | |
944 | 977 | | |
945 | | - | |
946 | | - | |
| 978 | + | |
| 979 | + | |
947 | 980 | | |
948 | 981 | | |
949 | 982 | | |
| |||
1805 | 1838 | | |
1806 | 1839 | | |
1807 | 1840 | | |
1808 | | - | |
1809 | | - | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
| 1845 | + | |
1810 | 1846 | | |
1811 | 1847 | | |
1812 | 1848 | | |
| |||
1899 | 1935 | | |
1900 | 1936 | | |
1901 | 1937 | | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
1902 | 1945 | | |
1903 | 1946 | | |
1904 | 1947 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2111 | 2111 | | |
2112 | 2112 | | |
2113 | 2113 | | |
| 2114 | + | |
| 2115 | + | |
2114 | 2116 | | |
2115 | 2117 | | |
2116 | 2118 | | |
| |||
2568 | 2570 | | |
2569 | 2571 | | |
2570 | 2572 | | |
| 2573 | + | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
| 2578 | + | |
| 2579 | + | |
| 2580 | + | |
| 2581 | + | |
| 2582 | + | |
| 2583 | + | |
| 2584 | + | |
| 2585 | + | |
| 2586 | + | |
| 2587 | + | |
| 2588 | + | |
| 2589 | + | |
| 2590 | + | |
| 2591 | + | |
0 commit comments