Skip to content

ext/pdo_sqlite: Avoid copying bound strings and LOBs - #25655

Merged
iliaal merged 1 commit into
php:masterfrom
iliaal:opt/pdo-sqlite-retain-bound-strings
Nov 24, 2026
Merged

iliaal merged 1 commit into
php:masterfrom
iliaal:opt/pdo-sqlite-retain-bound-strings

Conversation

@iliaal

@iliaal iliaal commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Retain the bound zend_string until SQLite releases it, as Nora suggested in #25597. This avoids a copy each time PDO SQLite binds a text or LOB parameter. The bind APIs take an int length, so PDO SQLite now rejects text and BLOB parameters of 2 GiB or more with SQLITE_TOOBIG.

With SQLite 3.37.2, binding and inserting 96 values of 1 MiB each into an in-memory database took 14.5% less time for text and 12.4% less for BLOBs. These are medians across 18 paired runs, excluding the transaction commit.

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

I measured this on master 2550ff2 with GCC 15 and clang 21 (Ryzen 9 5950X, system SQLite 3.46.1, opcache on, JIT off). The win is large when SQLite would otherwise copy a big value, and short values stay the same.

Change in CPU cycles per bind + execute against master, negative = fewer cycles:

statement master, cycles (GCC 15) GCC 15 clang 21
SELECT length(?), 64 B 1,223 −1.1% +0.2%
SELECT length(?), 1 KiB 2,276 0% −1.4%
SELECT length(?), 64 KiB 76,090 −5.7% −5.8%
SELECT length(?), 1 MiB 4,424,090 −74% −74%
INSERT, text, 64 B 1,749 +3.7% +1.4%
INSERT, text, 1 MiB 3,268,003 −3.9% −4.5%
INSERT, BLOB, 1 MiB 3,231,157 −3.2% −3.6%
INSERT, 10 params of 16 B 5,139 −2.2% −1.9%
bindParam() 1 MiB, one byte changed before each execute() 4,454,728 −72% −72%

Short values run 1–3% fewer instructions. The +3.7% on a 64 B INSERT has passes from −7% to +4%, so I read it as noise. Changing a bound 1 MiB string between executes now separates it in PHP, and that's still much cheaper than SQLite's copy.

A differential test of 49 binding cases gives the same output as master, also under ASan. It covers sizes from 0 to 1 MiB, bindParam changes, unset after bindValue, UTF-16 databases, bind errors and persistent connections. The SQLite 3.7.17 sources call the destructor on a failed bind too, so the refcount stays balanced on the oldest supported version.

One more thing worth a line in the description. From reading the code, master passes the size_t length as int, so values of 2 GiB or more were bound with a wrong length. Now they fail with SQLITE_TOOBIG. I didn't test that path. LGTM.

Comment thread ext/pdo_sqlite/sqlite_driver.c Outdated
pefree(einfo->errmsg, dbh->is_persistent);
}
einfo->errmsg = pestrdup((char*)sqlite3_errmsg(H->db), dbh->is_persistent);
einfo->errmsg = pestrdup(errmsg ? errmsg : sqlite3_errstr(errcode), dbh->is_persistent);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
einfo->errmsg = pestrdup(errmsg ? errmsg : sqlite3_errstr(errcode), dbh->is_persistent);
einfo->errmsg = pestrdup(errmsg, dbh->is_persistent);

And then call sqlite3_errstr in the pdo_sqlite_error_stmt_code macro, wouldn't that be cleaner?

That aside, I wonder why we even need to duplicate this string.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQLite can overwrite the buffer from sqlite3_errmsg() before errorInfo() reads it. sqlite3_errstr() returns static text, but copying it lets us use the same cleanup for both.

<?php
$db = new PDO('sqlite::memory:');
foreach ([PDO::PARAM_STR, PDO::PARAM_LOB] as $type) {
$value = str_repeat('a', 3);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will still be an interned string when optimizations are enabled.

: sqlite3_bind_text(S->stmt, index, ZSTR_VAL(value), length, pdo_sqlite_release_string);
}
if (rc != SQLITE_OK) {
if (rc == SQLITE_TOOBIG && sqlite3_bind_null(S->stmt, index) == SQLITE_OK) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the bind to NULL? Just so we don't have anything dangling?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes a later execute([]) see NULL after an oversized bind fails. SQLite 3.37.2 can leave the rejected value bound. Above INT_MAX we skip the bind, so the previous value would survive.

Retain the zend_string for each binding and let SQLite release it when
the value is no longer needed. Clear a rejected oversized binding so a
later execute that omits the parameter still sees NULL.
@iliaal
iliaal force-pushed the opt/pdo-sqlite-retain-bound-strings branch from 4405371 to 0426b11 Compare November 24, 2026 19:46
@iliaal
iliaal merged commit eea31ca into php:master Nov 24, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants