Repository navigation
PDO: build FETCH_OBJ rows as a properties table - #25516
Open
ArtUkrainskiy wants to merge 1 commit into
Open
ArtUkrainskiy wants to merge 1 commit into
ArtUkrainskiy wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FETCH_OBJis Laravel's default fetch mode, so query builder results and Eloquent models go through it. On master it takes 18–33% more cycles thanFETCH_ASSOCfor the same rows.do_fetch()wrote each column into an emptystdClasswithzend_update_property_ex(). Every write goes throughzend_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, likeFETCH_ASSOCdoes. Thenobject_and_properties_init()wraps it in astdClass.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'szend_hash_update()and notzend_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):FETCH_OBJ, SQLite, 1 columnFETCH_OBJ, SQLite, 3 columnsFETCH_OBJ, SQLite, 10 columnsFETCH_OBJ, SQLite, 30 columnsFETCH_OBJ, MySQL 8.4, 10 columnsFETCH_OBJ, MySQL 8.4, native prepared statement, 10 columnsFETCH_OBJ | FETCH_GROUP, SQLite, 10 columnsFETCH_ASSOC, SQLite, 10 columnsFETCH_CLASS, SQLite, 10 columns~: 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_OBJis now 2–7% slower thanFETCH_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_ASSOCandFETCH_CLASSpay 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_GROUPandFETCH_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