From 841f09f1dd35116d8fb85aa406e5585e99062a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20D=C3=B6tsch?= Date: Thu, 22 Oct 2026 17:28:48 +0200 Subject: [PATCH] ext/pdo: Size PDO::FETCH_BOTH rows for both keys of each column 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 --- UPGRADING | 4 ++++ ext/pdo/pdo_stmt.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/UPGRADING b/UPGRADING index adfe422d90ab..95082ac45ae3 100644 --- a/UPGRADING +++ b/UPGRADING @@ -180,6 +180,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 eab4398a0d5e..2a84d7ae2e98 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); }