Repository navigation
Conversation
igbinary_zend_hash_add_or_find()/igbinary_zend_hash_index_add_or_find() fell back to the "unoptimized reference implementation" on PHP 8.1+, which probes the hash table twice per array element (zend_hash_find() followed by zend_hash_add_new()). PHP 8.1 added zend_hash_lookup()/zend_hash_index_lookup(), which have exactly the required semantics (return the existing value, or insert and return a new IS_NULL entry) in a single probe. They are what ext/standard's unserialize() uses as well. Also add a benchmark for arrays of associative arrays (database rows).
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.
Summary
On PHP 8.1+,
igbinary_zend_hash_add_or_find()/igbinary_zend_hash_index_add_or_find()fall back to the"unoptimized reference implementation", which probes the hash table twice for every array element:
zend_hash_find()followed byzend_hash_add_new().PHP 8.1 added
zend_hash_lookup()/zend_hash_index_lookup()(Zend/zend_hash.h), which have exactly thesemantics igbinary needs here: return the existing value, or insert and return a new
IS_NULLentry, in a singleprobe. They are also what
ext/standard'sunserialize()uses for the same purpose(
ext/standard/var_unserializer.re,process_nested_array_data).This PR uses them for
PHP_VERSION_ID >= 80100. The pre-7.2 reference implementation and the 7.2–8.0specialization are unchanged, and
IGBINARY_FORCE_REFERENCE_HASH_TABLE_FUNCTIONSstill forces the referenceimplementation.
Also adds
benchmark/unserialize-assocrowarray.b.php(an array of 200 associative arrays with 10 keys each,i.e. typical database rows / cached API responses), since that shape was not covered by the existing benchmarks.
Benchmarks
PHP 8.4.27 (NTS,
--disable-all,-O2), Linux x86_64, i7-13700H pinned to one performance core, best of 3interleaved rounds.
igbinary_unserialize()time per call:Existing
benchmark/scripts (seconds per run, min of 40 runs):A sampling profile of the 2000-assoc-row case on master showed
zend_hash_add_new(12.8%) andzend_hash_find(10.9%) as the two largest non-igbinary costs; with this change they are replaced by a single
zend_hash_lookupat 17%.
Testing
make test: 135 passed, 0 failed, 25 skipped on PHP 8.4.27 (release build), and the same result on a--enable-debugbuild.zval *the old find-or-add pair returned, includingIS_INDIRECTslots, which callers already handle.