Skip to content

Avoid allocating a row array when a statement is done - #758

Merged
flavorjones merged 4 commits into
sparklemotion:mainfrom
thomasklemm:thomasklemm/statement-done-allocation
Oct 26, 2026
Merged

flavorjones merged 4 commits into
sparklemotion:mainfrom
thomasklemm:thomasklemm/statement-done-allocation

Conversation

@thomasklemm

Copy link
Copy Markdown
Contributor

Statement#step allocates a row array before handling SQLITE_DONE, then discards it. Handle completion after the suppressed-callback exception check but before sqlite3_column_count / rb_ary_new2. Row conversion, freezing, and the error/reset paths are unchanged.

This saves one array per completed statement, not per row. A warmed 10,000-iteration GC.stat(:total_allocated_objects) probe on Ruby 4.0.7 measured 2 → 1 objects for an empty SELECT, 4 → 3 for a one-row SELECT, and 2 → 1 for an empty UPDATE. Stepping an already-done statement remains at zero allocations. This is an allocation-only improvement; no speed, latency, or CPU no-regression claim is made.

Validation on the PR head (Ruby 4.0.7):

  • bundle exec rake: RuboCop clean; 379 tests / 772 assertions, no failures or skips (vendored SQLite 3.54.1).
  • SQLITE3_TEST_GC_LEVEL=verify bundle exec rake test: 379 / 772, no failures or skips.
  • bundle exec rake compile test:valgrind: 379 / 749, no failures or memory errors; 7 test-defined skips.
  • System SQLite 3.40.1: build and normal/GC-verify suites pass, 379 / 761 with 11 feature/platform skips. Compiler diagnostics match baseline.

Before/after behavior probes also agree for retained frozen rows, Unicode/blob/null values, reset/repeated DONE, constraint and callback exceptions, compaction, BUSY/LOCKED retries, and statement lifetime after closing the database.

@flavorjones

Copy link
Copy Markdown
Member

Thank you for this contribution! If it goes green, I'll merge it.

@flavorjones
flavorjones merged commit 526d4e1 into sparklemotion:main Oct 26, 2026
135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants