Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Contributor

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

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:

alex@alex-macos php-src % hyperfine './sapi/cli/php.branch validation_benchmark.php' './sapi/cli/php.master validation_benchmark.php' --warmup 50
Benchmark 1: ./sapi/cli/php.branch validation_benchmark.php
  Time (mean ± σ):     313.4 ms ±   4.7 ms    [User: 309.0 ms, System: 2.8 ms]
  Range (min … max):   307.9 ms … 321.5 ms    10 runs
 
Benchmark 2: ./sapi/cli/php.master validation_benchmark.php
  Time (mean ± σ):     419.6 ms ±   6.9 ms    [User: 411.9 ms, System: 4.0 ms]
  Range (min … max):   412.4 ms … 435.6 ms    10 runs
 
Summary
  ./sapi/cli/php.branch validation_benchmark.php ran
    1.34 ± 0.03 times faster than ./sapi/cli/php.master validation_benchmark.php

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

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.

1 participant