Repository navigation
ext/pdo_sqlite: Avoid copying bound strings and LOBs - #25655
Conversation
|
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:
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 |
| 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); |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Why do we need the bind to NULL? Just so we don't have anything dangling?
There was a problem hiding this comment.
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.
4405371 to
0426b11
Compare
Retain the bound
zend_stringuntil 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 anintlength, so PDO SQLite now rejects text and BLOB parameters of 2 GiB or more withSQLITE_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.