[php-src] PR #25516: PDO: build FETCH_OBJ rows as a properties table
| From: | ArtUkrainskiy | Date: | Tue, 17 Nov 2026 11:29:19 +0000 |
| Subject: | [php-src] PR #25516: PDO: build FETCH_OBJ rows as a properties table | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40723@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25516
Author: ArtUkrainskiy
FETCH_OBJ is Laravel's default fetch mode, so query builder results and Eloquent
models go through it. On master it takes 18–33% more cycles than FETCH_ASSOC for the
same rows.
do_fetch() wrote each column into an empty stdClass with
zend_update_property_ex(). Every write goes through
zend_std_write_property(). The properties table starts at 8 slots, so it rehashes at
the 9th and the 17th column. Now the row is collected into a hash table sized for the column count,
like FETCH_ASSOC does. Then object_and_properties_init() wraps it in a
stdClass.
Scripts see the same objects as before. Column names stay string keys, so "0"
doesn't turn into an integer key. That's why it's zend_hash_update() and
not zend_symtable_update(). A repeated name overwrites the value in place, as the
property handler does. The handler rejects names that start with "\0", so
rows with such a name keep the old path.
query() + fetchAll() of 100 rows, AMD Ryzen 9 5950X, negative = fewer
cycles (faster):
| call | master, CPU cycles per call (GCC 15.2) | GCC 15.2 | clang 21.1 |
|---|--:|--:|--:|
| FETCH_OBJ, SQLite, 1 column | 59,800 | −7% | 0% ~ |
| FETCH_OBJ, SQLite, 3 columns | 119,979 | −4% | −7% |
| FETCH_OBJ, SQLite, 10 columns | 304,032 | −12% | −13% |
| FETCH_OBJ, SQLite, 30 columns | 841,453 | −14% | −14% |
| FETCH_OBJ, MySQL 8.4, 10 columns | 199,072 | −16% | −19% |
| FETCH_OBJ, MySQL 8.4, native prepared statement, 10 columns | 178,149 | −16% |
−19% |
| FETCH_OBJ \| FETCH_GROUP, SQLite, 10 columns | 321,583 | −11% | −11% |
| FETCH_ASSOC, SQLite, 10 columns | 254,914 | 0% | 0% ~ |
| FETCH_CLASS, SQLite, 10 columns | 292,877 | +1% | −1% |
~: within noise, the change is no larger than the difference between master and master on the same
row.
With 10 columns a row takes 1,273–1,286 fewer instructions. With 10 or 30 columns
FETCH_OBJ is now 2–7% slower than FETCH_ASSOC, down from 19–33%.
There's one memory cost. The table is sized for all columns, but a repeated column name keeps
one entry. With 9 columns and 8 distinct names a row takes 928 bytes instead of 608. Other rows take
the same memory as on master. FETCH_ASSOC and FETCH_CLASS pay up to 3
instructions per row for the extra check.
Output is byte-identical to master on GCC, clang and ASan + UBSan, from SQLite and MySQL. That
includes duplicate, numeric, empty and non-ASCII column names, FETCH_GROUP and
FETCH_UNIQUE. The ext/pdo, pdo_sqlite and pdo_mysql tests pass, and valgrind is clean.
The new test covers the column name cases and passes on master too.
I haven't tried Intel, other compilers or PostgreSQL.
Full numbers and scripts: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/pdo-fetch-obj