Skip to content

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Oct 7, 2025

Instead of using a boolean flag to check for each element whether or not it is the first, we just unconditionally append a comma after each element and then remove the last comma at the end.

For:

<?php

$len = 0;
for ($i = 0; $i < 3_000_000; $i++) {
	$len += strlen(json_encode(array_fill(0, 20, [])));
}
var_dump($len);

This is ~1.06 faster for a gcc 13.3 release build on a Intel(R) Core(TM) i7-1365U.

Benchmark 1: /tmp/bench/before /tmp/bench/test6.php
  Time (mean ± σ):     819.6 ms ±   2.8 ms    [User: 816.4 ms, System: 2.4 ms]
  Range (min … max):   816.9 ms … 825.0 ms    10 runs

Benchmark 2: /tmp/bench/after /tmp/bench/test6.php
  Time (mean ± σ):     770.8 ms ±   5.8 ms    [User: 766.6 ms, System: 2.9 ms]
  Range (min … max):   765.3 ms … 785.8 ms    10 runs

Summary
  /tmp/bench/after /tmp/bench/test6.php ran
    1.06 ± 0.01 times faster than /tmp/bench/before /tmp/bench/test6.php

Instead of using a boolean flag to check for each element whether or not it is
the first, we just unconditionally append a comma after each element and then
remove the last comma at the end.

For:

    <?php

    $len = 0;
    for ($i = 0; $i < 3_000_000; $i++) {
    	$len += strlen(json_encode(array_fill(0, 20, [])));
    }
    var_dump($len);

This is ~1.06 faster for a gcc 13.3 release build on a Intel(R) Core(TM)
i7-1365U.

    Benchmark 1: /tmp/bench/before /tmp/bench/test6.php
      Time (mean ± σ):     819.6 ms ±   2.8 ms    [User: 816.4 ms, System: 2.4 ms]
      Range (min … max):   816.9 ms … 825.0 ms    10 runs

    Benchmark 2: /tmp/bench/after /tmp/bench/test6.php
      Time (mean ± σ):     770.8 ms ±   5.8 ms    [User: 766.6 ms, System: 2.9 ms]
      Range (min … max):   765.3 ms … 785.8 ms    10 runs

    Summary
      /tmp/bench/after /tmp/bench/test6.php ran
        1.06 ± 0.01 times faster than /tmp/bench/before /tmp/bench/test6.php
@TimWolla TimWolla requested a review from nielsdos October 7, 2025 10:14
@TimWolla TimWolla requested a review from bukka as a code owner October 7, 2025 10:14
@nielsdos
Copy link
Member

nielsdos commented Oct 7, 2025

Tested on a much older CPU (i7-4790):

Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      1.119 s ±  0.004 s    [User: 1.113 s, System: 0.003 s]
  Range (min … max):    1.112 s …  1.126 s    10 runs
 
Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      1.100 s ±  0.007 s    [User: 1.095 s, System: 0.002 s]
  Range (min … max):    1.092 s …  1.115 s    10 runs
 
Summary
  ./sapi/cli/php_old x.php ran
    1.02 ± 0.01 times faster than ./sapi/cli/php x.php

Still seems worthwhile as the difference is negligible and my system has a noticeably slower memory subsystem which likely contributes to this result.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Anyway I looked at this and this seems legit. Nice one.

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