Skip to content

Add a 1-char fastpath to implode() - #19276

Merged
alexandre-daubois merged 1 commit into
php:masterfrom
alexandre-daubois:implode-opt
Sep 10, 2025
Merged

alexandre-daubois merged 1 commit into
php:masterfrom
alexandre-daubois:implode-opt

Conversation

@alexandre-daubois

@alexandre-daubois alexandre-daubois commented Jul 28, 2025 •

Copy link
Copy Markdown
Member

I think it's worth adding a 1 char fast path for implode(). A quick search on Github reveals the following stats:

  • Implode usage with one char glue: /implode\(\s*["'][^"']{1}["']\s*,\s*/ language:PHP reveals 1.4 million occurrences
  • Implode usage with 2+ char glue: /implode\(\s*["'][^"']{2,}["']\s*,\s*/ language:PHP reveals 1.1 million occurrences

Here is the benchmark code:

<?php

$iterations = 1000;
$array = array_fill(0, 10000, 'item');

for ($i = 0; $i < $iterations; $i++) {
    implode(',', $array);
}

And the results:

Benchmark 1: ./sapi/cli/php.branch bench.php
  Time (mean ± σ):      33.7 ms ±   1.2 ms    [User: 30.8 ms, System: 2.1 ms]
  Range (min … max):    32.7 ms …  41.4 ms    82 runs
 
Benchmark 2: ./sapi/cli/php.master bench.php
  Time (mean ± σ):      45.1 ms ±   6.7 ms    [User: 41.4 ms, System: 2.2 ms]
  Range (min … max):    42.9 ms …  97.5 ms    65 runs

Summary
  ./sapi/cli/php.branch bench.php ran
    1.34 ± 0.21 times faster than ./sapi/cli/php.master bench.php

No change for multi char glue, but a nice improved performance for single char glue especially one big arrays.

@ndossche

Copy link
Copy Markdown
Member

1-char bench on an i7-4790:

Benchmark 1: ./sapi/cli/php x.php 
  Time (mean ± σ):      47.9 ms ±   0.7 ms    [User: 45.5 ms, System: 2.0 ms]
  Range (min … max):    46.5 ms …  49.3 ms    60 runs
 
Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      69.8 ms ±   1.1 ms    [User: 67.7 ms, System: 1.8 ms]
  Range (min … max):    67.4 ms …  73.0 ms    42 runs
 
Summary
  ./sapi/cli/php x.php  ran
    1.46 ± 0.03 times faster than ./sapi/cli/php_old x.php

Curiously a large difference with your measurement, did you benchmark a release build?

2-char glue is slightly slower:

Benchmark 1: ./sapi/cli/php x.php 
  Time (mean ± σ):      72.5 ms ±   1.9 ms    [User: 70.2 ms, System: 1.9 ms]
  Range (min … max):    70.2 ms …  82.4 ms    40 runs
 
Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      66.4 ms ±   0.8 ms    [User: 63.9 ms, System: 2.0 ms]
  Range (min … max):    64.5 ms …  68.9 ms    45 runs
 
Summary
  ./sapi/cli/php_old x.php ran
    1.09 ± 0.03 times faster than ./sapi/cli/php x.php 

@alexandre-daubois

alexandre-daubois commented Aug 10, 2025 •

Copy link
Copy Markdown
Member Author

I checked again and --enable-debug was used in configure. Removing it reveals results very close to yours. 2-char glue results on about the same perf on Apple M4:

Summary
  ./sapi/cli/php.branch bench.php ran
    1.01 ± 0.03 times faster than ./sapi/cli/php.master bench.php

Given the number of occurrences of 1 char glue as written in the description, I think this fast path would still worth it?

Description updated with the non-debug numbers for 1-char glue.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for me

@alexandre-daubois
alexandre-daubois merged commit 29c7ee4 into php:master Sep 10, 2025
9 checks passed
@alexandre-daubois
alexandre-daubois deleted the implode-opt branch September 10, 2025 14:52
Comment thread ext/standard/string.c
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.

3 participants