Commit 14c65d1c284 for php
commit 14c65d1c284fe094689be0cefd23360960c72272
Author: Matthias Dötsch <matze@mdoetsch.de>
Date: Fri Oct 23 23:19:57 2026 +0200
ext/pdo: Size PDO::FETCH_BOTH rows for both keys of each column (#24565)
PDO::FETCH_BOTH, the default fetch mode, stores each value under its
column name and its index, but the row array was sized for one entry per
column. Rows with more than 4 columns were therefore resized and rehashed
once per fetched row. The final array size is unchanged.
50x fetchAll(PDO::FETCH_BOTH) of 10k rows with 11 columns (pdo_pgsql):
-4.9% instructions, -9% cycles
diff --git a/UPGRADING b/UPGRADING
index 39f8d9bbc0b..baa930594bd 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -183,6 +183,10 @@ PHP 8.7 UPGRADE NOTES
- MBString:
. Improved performance of mb_strlen for UTF-8 strings.
+- PDO:
+ . Improved performance of fetching rows with PDO::FETCH_BOTH, the default
+ fetch mode, for results with more than 4 columns.
+
- PDO_PGSQL:
. Improved performance of the SQL parser that handles placeholders in
PDO::prepare() and, with emulated prepares, PDOStatement::execute().
diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c
index eab4398a0d5..2a84d7ae2e9 100644
--- a/ext/pdo/pdo_stmt.c
+++ b/ext/pdo/pdo_stmt.c
@@ -735,7 +735,8 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
case PDO_FETCH_NUM:
case PDO_FETCH_NAMED:
if (!group_key) {
- array_init_size(return_value, stmt->column_count);
+ /* PDO::FETCH_BOTH stores every value under its name and its index */
+ array_init_size(return_value, how == PDO_FETCH_BOTH ? 2 * stmt->column_count : stmt->column_count);
} else {
array_init(return_value);
}