Skip to content

ext/standard: Optimize array_column() - #23786

Merged
LamentXU123 merged 3 commits into
php:masterfrom
LamentXU123:opt------
Sep 20, 2026
Merged

LamentXU123 merged 3 commits into
php:masterfrom
LamentXU123:opt------

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

Just move some numeric-string key normalization stuff out of the main loop. Also return early for empty input.

Column selector Reduction
'id' 6.5%
'0' 29.6%
'123456789' 58.6%

Comment thread ext/standard/array.c Outdated
RETURN_EMPTY_ARRAY();
}

array_init_size(return_value, num_elements);

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.

nit: If I were you, I would put inside each case instead wdyt ?

@devnexen devnexen Sep 20, 2026

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.

did you benchmark when the key is not interned string btw ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

did you benchmark when the key is not interned string btw ?

The old benchmarks is already running on non-interned keys.

@devnexen devnexen Sep 20, 2026

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.

can you benchmark this one (esp K2 case) ?

<?php
$case = $argv[1];
$N = 200000;
$rt = substr('xid', 1);

$rows = [];
for ($i = 0; $i < $N; $i++) {
    $rows[] = ($case === 'K3' || $case === 'K4') ? [$rt => $i] : ['id' => $i];
}
$col = ($case === 'K2' || $case === 'K4') ? $rt : 'id';

$t = [];
for ($r = 0; $r < 25; $r++) {
    $s = hrtime(true);
    $out = array_column($rows, $col);
    $t[] = (hrtime(true) - $s) / 1e6;
    unset($out);
}
sort($t);
printf("%.3f\n", $t[12]);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

K2 observes ~14% improvements and K3 ~8%. Others are within noise

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.

not as good locally (~2% for K2) but it s all positive regardless.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah I am away from productive server testing this stuff on personal PC. So perhaps bigger noise than expected.

@LamentXU123
LamentXU123 merged commit 5d24c8b into php:master Sep 20, 2026
18 checks passed
@LamentXU123

Copy link
Copy Markdown
Member Author

Thanks!

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.

2 participants