Skip to content

PDO: build FETCH_OBJ rows as a properties table - #25516

Open
ArtUkrainskiy wants to merge 1 commit into
php:masterfrom
ArtUkrainskiy:pdo-fetch-obj
Open

ArtUkrainskiy wants to merge 1 commit into
php:masterfrom
ArtUkrainskiy:pdo-fetch-obj

Conversation

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

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

do_fetch() wrote each column of a FETCH_OBJ row into an empty stdClass with
zend_update_property_ex(), which goes through the generic write_property
handler and grows the properties table from 8 slots. The row is now collected
into a hash table sized for the column count with zend_hash_update(), and the
stdClass is created around it with object_and_properties_init().

Column names are stored as string keys, as the property handler stores them,
and a repeated name replaces the earlier value in place. When a column name
starts with "\0", which the property handler rejects, the old path is used.

query() + fetchAll(PDO::FETCH_OBJ) of 100 rows from SQLite and MySQL 8.4,
AMD Zen 3, GCC 15.2 and clang 21.1: 12-19% fewer cycles with 10 or 30
columns, 1,273-1,286 fewer instructions per row with 10 columns. Same
output as master.
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.

1 participant