Commit 580000f
committed
Reduce QueryContext allocations by reusing the channel
This commit reduces the number of allocations and memory usage of
QueryContext by inverting the goroutine: instead of processing the
request in the goroutine and having it send the result, we now process
the request in the method itself and goroutine is only used to interrupt
the query if the context is canceled. The advantage of this approach is
that we no longer need to send anything on the channel, but instead can
treat the channel as a semaphore (this reduces the amount of memory
allocated by this method).
Additionally, we now reuse the channel used to communicate with the
goroutine which reduces the number of allocations.
This commit also adds a test that actually exercises the
sqlite3_interrupt logic since the existing tests did not. Those tests
cancelled the context before scanning any of the rows and could be made
to pass without ever calling sqlite3_interrupt. The below version of
SQLiteRows.Next passes the previous tests:
```go
func (rc *SQLiteRows) Next(dest []driver.Value) error {
rc.s.mu.Lock()
defer rc.s.mu.Unlock()
if rc.s.closed {
return io.EOF
}
if err := rc.ctx.Err(); err != nil {
return err
}
return rc.nextSyncLocked(dest)
}
```
Benchmark results:
```
goos: darwin
goarch: arm64
pkg: github.com/mattn/go-sqlite3
cpu: Apple M1 Max
│ old.txt │ new.txt │
│ sec/op │ sec/op vs base │
Suite/BenchmarkQueryContext/Background-10 3.994µ ± 2% 4.034µ ± 1% ~ (p=0.289 n=10)
Suite/BenchmarkQueryContext/WithCancel-10 12.02µ ± 3% 11.56µ ± 4% -3.87% (p=0.003 n=10)
geomean 6.930µ 6.829µ -1.46%
│ old.txt │ new.txt │
│ B/op │ B/op vs base │
Suite/BenchmarkQueryContext/Background-10 400.0 ± 0% 400.0 ± 0% ~ (p=1.000 n=10) ¹
Suite/BenchmarkQueryContext/WithCancel-10 2.376Ki ± 0% 1.025Ki ± 0% -56.87% (p=0.000 n=10)
geomean 986.6 647.9 -34.33%
¹ all samples are equal
│ old.txt │ new.txt │
│ allocs/op │ allocs/op vs base │
Suite/BenchmarkQueryContext/Background-10 12.00 ± 0% 12.00 ± 0% ~ (p=1.000 n=10) ¹
Suite/BenchmarkQueryContext/WithCancel-10 38.00 ± 0% 28.00 ± 0% -26.32% (p=0.000 n=10)
geomean 21.35 18.33 -14.16%
¹ all samples are equal
```1 parent 41871ea commit 580000f
3 files changed
+255
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
402 | 405 | | |
403 | 406 | | |
404 | 407 | | |
| |||
2118 | 2121 | | |
2119 | 2122 | | |
2120 | 2123 | | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
2121 | 2127 | | |
2122 | 2128 | | |
2123 | 2129 | | |
| |||
2172 | 2178 | | |
2173 | 2179 | | |
2174 | 2180 | | |
2175 | | - | |
| 2181 | + | |
| 2182 | + | |
2176 | 2183 | | |
2177 | 2184 | | |
2178 | | - | |
2179 | | - | |
| 2185 | + | |
| 2186 | + | |
| 2187 | + | |
| 2188 | + | |
| 2189 | + | |
| 2190 | + | |
| 2191 | + | |
2180 | 2192 | | |
2181 | | - | |
2182 | | - | |
2183 | | - | |
2184 | | - | |
2185 | | - | |
2186 | | - | |
2187 | 2193 | | |
2188 | | - | |
2189 | | - | |
2190 | | - | |
| 2194 | + | |
2191 | 2195 | | |
2192 | | - | |
| 2196 | + | |
| 2197 | + | |
| 2198 | + | |
| 2199 | + | |
2193 | 2200 | | |
2194 | | - | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
2195 | 2213 | | |
| 2214 | + | |
2196 | 2215 | | |
2197 | 2216 | | |
2198 | 2217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
268 | 270 | | |
269 | 271 | | |
270 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
271 | 418 | | |
272 | 419 | | |
273 | 420 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
2030 | 2031 | | |
2031 | 2032 | | |
2032 | 2033 | | |
2033 | | - | |
| 2034 | + | |
2034 | 2035 | | |
2035 | 2036 | | |
2036 | 2037 | | |
| |||
2039 | 2040 | | |
2040 | 2041 | | |
2041 | 2042 | | |
2042 | | - | |
| 2043 | + | |
2043 | 2044 | | |
2044 | 2045 | | |
2045 | 2046 | | |
| |||
2068 | 2069 | | |
2069 | 2070 | | |
2070 | 2071 | | |
2071 | | - | |
2072 | | - | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
2073 | 2079 | | |
2074 | 2080 | | |
2075 | 2081 | | |
| |||
2084 | 2090 | | |
2085 | 2091 | | |
2086 | 2092 | | |
2087 | | - | |
2088 | | - | |
2089 | | - | |
| 2093 | + | |
| 2094 | + | |
| 2095 | + | |
| 2096 | + | |
| 2097 | + | |
2090 | 2098 | | |
2091 | 2099 | | |
2092 | 2100 | | |
| |||
2107 | 2115 | | |
2108 | 2116 | | |
2109 | 2117 | | |
| 2118 | + | |
2110 | 2119 | | |
2111 | 2120 | | |
2112 | 2121 | | |
| |||
2479 | 2488 | | |
2480 | 2489 | | |
2481 | 2490 | | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
| 2518 | + | |
| 2519 | + | |
| 2520 | + | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
| 2530 | + | |
| 2531 | + | |
| 2532 | + | |
| 2533 | + | |
| 2534 | + | |
| 2535 | + | |
| 2536 | + | |
| 2537 | + | |
| 2538 | + | |
| 2539 | + | |
| 2540 | + | |
| 2541 | + | |
| 2542 | + | |
| 2543 | + | |
| 2544 | + | |
| 2545 | + | |
| 2546 | + | |
| 2547 | + | |
| 2548 | + | |
| 2549 | + | |
2482 | 2550 | | |
2483 | 2551 | | |
2484 | 2552 | | |
| |||
0 commit comments