Skip to content

ext/spl: Optimize php_spl_object_hash performance#22087

Open
arshidkv12 wants to merge 4 commits into
php:masterfrom
arshidkv12:spl_hash
Open

ext/spl: Optimize php_spl_object_hash performance#22087
arshidkv12 wants to merge 4 commits into
php:masterfrom
arshidkv12:spl_hash

Conversation

@arshidkv12
Copy link
Copy Markdown
Contributor

@arshidkv12 arshidkv12 commented May 19, 2026

Optimize php_spl_object_hash performance

  • Replace strpprintf() with manual hex conversion
  • Avoid printf formatting overhead
  • Use lookup-table-based hex encoding
  • Reduce runtime from ~412ms to ~244ms in benchmark
  • Achieve ~1.68x speed improvement
hyperfine --warmup 5 \                                                                                         
'./sapi/cli/php_old -dopcache.enable_cli=0 bench.php' \
'./sapi/cli/php -dopcache.enable_cli=0 bench.php'

Output:

Benchmark 1: ./sapi/cli/php_old -dopcache.enable_cli=0 bench.php
  Time (mean ± σ):     411.7 ms ±   2.3 ms    [User: 407.7 ms, System: 2.8 ms]
  Range (min … max):   407.7 ms … 416.5 ms    10 runs
 
Benchmark 2: ./sapi/cli/php -dopcache.enable_cli=0 bench.php
  Time (mean ± σ):     246.0 ms ±   1.6 ms    [User: 243.0 ms, System: 2.1 ms]
  Range (min … max):   243.9 ms … 249.0 ms    12 runs
 
Summary
  ./sapi/cli/php -dopcache.enable_cli=0 bench.php ran
    1.67 ± 0.01 times faster than ./sapi/cli/php_old -dopcache.enable_cli=0 bench.php
<?php
//bench.php
for ($i = 0; $i < 1000000; $i++) {
    spl_object_hash(new stdClass);
}

orlitzky and others added 3 commits May 19, 2026 13:40
@arshidkv12 arshidkv12 requested a review from Girgias as a code owner May 19, 2026 08:15
@arshidkv12 arshidkv12 changed the title Optimize php_spl_object_hash performance ext/spl: Optimize php_spl_object_hash performance May 19, 2026
@Girgias
Copy link
Copy Markdown
Member

Girgias commented May 19, 2026

Is this really a hot path in "normal" code? It might be because people use spl_object_hash() (or whatever the function is called) but does it actually make an impact in a non-synthetic benchmark?

@arshidkv12
Copy link
Copy Markdown
Contributor Author

I think, this improves WordPress hook performance.
https://i.ibb.co/jP56JpVs/wp.png
https://github.com/WordPress/WordPress/blob/master/wp-includes/plugin.php#L1014

@staabm
Copy link
Copy Markdown
Contributor

staabm commented May 19, 2026

could WP use spl_object_id which is the more modern/faster alternative to spl_object_hash?

@arshidkv12
Copy link
Copy Markdown
Contributor Author

arshidkv12 commented May 19, 2026

  • spl_object_id() exists only in PHP 7.2+
  • WordPress still supports older PHP versions

@staabm
Copy link
Copy Markdown
Contributor

staabm commented May 19, 2026

  • WordPress still supports older PHP versions

I think thats no longer true. lately they recommend PHP 8.3+
(and report compatibility with php 7.2+ for legacy)

see https://wordpress.org/about/requirements/

Comment thread ext/spl/php_spl.c Outdated
Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>
@kitrio
Copy link
Copy Markdown

kitrio commented May 19, 2026

Guzzle use this function

https://github.com/guzzle/guzzle/blob/b777df1776c667e287664dda75b0298ad8ae3a14/src/HandlerStack.php#L272

    private function debugCallable($fn): string
    {
        if (\is_string($fn)) {
            return "callable({$fn})";
        }

        if (\is_array($fn)) {
            return \is_string($fn[0])
                ? "callable({$fn[0]}::{$fn[1]})"
                : "callable(['".\get_class($fn[0])."', '{$fn[1]}'])";
        }

        /** @var object $fn */
        return 'callable('.\spl_object_hash($fn).')';
    }

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.

6 participants