Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Unreleased
=======
* Speed up igbinary_unserialize() of arrays on PHP 8.1+ by inserting elements with a single hash table probe
(zend_hash_lookup()/zend_hash_index_lookup()) instead of a find followed by an insert.
* Add a benchmark for unserializing arrays of associative arrays.

3.2.17RC1 2025-11-26
=======
* Support PHP 8.5
Expand Down
35 changes: 35 additions & 0 deletions benchmark/unserialize-assocrowarray.b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

// Description: Unserialize array of associative arrays (e.g. database rows)

require_once 'bench.php';

call_user_func(function () {
$b = new Bench('unserialize-assoc-row-array');

$array = [];
for ($i = 0; $i < 200; $i++) {
$array[] = [
'id' => $i,
'name' => "user_$i",
'email' => "user$i@example.com",
'score' => $i * 1.5,
'active' => ($i % 2) === 0,
'created_at' => '2026-10-23 10:00:00',
'tags' => ['a', 'b', 'c'],
'count' => $i * 1000,
'ratio' => 0.25,
'nullable' => null,
];
}
$ser = igbinary_serialize($array);

for ($i = 0; $i < 40; $i++) {
$b->start();
for ($j = 0; $j < 3000; $j++) {
$array = igbinary_unserialize($ser);
}
$b->stop($j);
$b->write();
}
});
15 changes: 14 additions & 1 deletion src/php7/igbinary_zend_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@

/* See https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implementation.html for an overview of PHP's hash table information */

#if PHP_VERSION_ID < 70200 || PHP_VERSION_ID >= 80100 || IGBINARY_FORCE_REFERENCE_HASH_TABLE_FUNCTIONS
#if PHP_VERSION_ID >= 80100 && !IGBINARY_FORCE_REFERENCE_HASH_TABLE_FUNCTIONS
/* PHP 8.1+ exports zend_hash_lookup()/zend_hash_index_lookup(), which have exactly the semantics needed here */
/* (return the existing value, or insert and return a new IS_NULL entry) using a single hash table probe. */
/* {{{ igbinary_zend_hash_add_or_find(HashTable *ht, zend_string *key) */
static zend_always_inline zval *igbinary_zend_hash_add_or_find(HashTable *ht, zend_string *key) {
return zend_hash_lookup(ht, key);
}
/* }}} */
/* {{{ igbinary_zend_hash_index_add_or_find(HashTable *ht, zend_long key) */
static zend_always_inline zval *igbinary_zend_hash_index_add_or_find(HashTable *ht, zend_long key) {
return zend_hash_index_lookup(ht, key);
}
/* }}} */
#elif PHP_VERSION_ID < 70200 || PHP_VERSION_ID >= 80100 || IGBINARY_FORCE_REFERENCE_HASH_TABLE_FUNCTIONS
/* {{{ igbinary_zend_hash_add_or_find(HashTable *ht, zend_string *key) unoptimized reference implementation. */
/* This will either return the pointer to the existing value, or add a brand new entry to the hash table with a PHP IS_NULL value. */
static zend_always_inline zval *igbinary_zend_hash_add_or_find(HashTable *ht, zend_string *key) {
Expand Down