Optimize array_intersect() for integer and string values - #23019
Open
mehmetcansahin wants to merge 2 commits into
Open
Optimize array_intersect() for integer and string values#23019mehmetcansahin wants to merge 2 commits into
mehmetcansahin wants to merge 2 commits into
Conversation
array_intersect() compares values as strings through a sort-based algorithm that converts values on every comparison. When every value in every argument is an integer or a string, equality under string semantics maps exactly to symtable key normalization, so the result can be computed with a single hash set pass instead. Local benchmarks show supported inputs running between 4.9x and 111.8x faster, with approximately 1.3% to 1.8% overhead in the measured fallback cases. PHP_FUNCTION(array_intersect) now calls zend_parse_parameters() itself before choosing between the fast and generic paths, making it the call site that rejects named variadic arguments; extend the named_params test to pin this and the single-argument fallback.
Member
|
This looks sensible. But could you please provide the "Local benchmarks" you've run for us to verify. These days it's hard to tell a performance improvement without benchmarks. |
Contributor
Author
|
@LamentXU123 Thanks. I reran the benchmarks on an Apple M1, comparing base
Benchmark script: benchmark.php<?php
declare(strict_types=1);
const TARGET_SAMPLE_NS = 100_000_000;
const SAMPLE_COUNT = 11;
function makeStrings(int $start, int $size): array
{
$values = [];
for ($i = $start, $end = $start + $size; $i < $end; $i++) {
$values[] = "value_$i";
}
return $values;
}
function makeMixed(int $start, int $size): array
{
$values = [];
for ($i = $start, $end = $start + $size; $i < $end; $i++) {
$values[] = ($i & 1) === 0 ? $i : (string) $i;
}
return $values;
}
function scenarios(): array
{
$fallbackFirst = range(0, 9_999);
array_unshift($fallbackFirst, 0.5);
$fallbackLast = range(0, 9_999);
$fallbackLast[] = 0.5;
return [
'int-10' => [range(0, 9), range(5, 14)],
'int-1000' => [range(0, 999), range(500, 1_499)],
'int-100000' => [range(0, 99_999), range(50_000, 149_999)],
'string-10000' => [makeStrings(0, 10_000), makeStrings(5_000, 10_000)],
'mixed-int-string-10000' => [makeMixed(0, 10_000), makeMixed(5_000, 10_000)],
'int-10000-3-arrays' => [
range(0, 9_999),
range(2_500, 12_499),
range(5_000, 14_999),
],
'fallback-float-first-10000' => [$fallbackFirst, range(5_000, 14_999)],
'fallback-float-last-10000' => [$fallbackLast, range(5_000, 14_999)],
];
}
function measure(array $arrays, int $iterations): array
{
$checksum = 0;
$start = hrtime(true);
for ($i = 0; $i < $iterations; $i++) {
$checksum += count(array_intersect(...$arrays));
}
return [hrtime(true) - $start, $checksum];
}
$allScenarios = scenarios();
$selected = $argv[1] ?? null;
if ($selected === null || !isset($allScenarios[$selected])) {
fwrite(STDERR, "Usage: php benchmark.php <scenario>\n\nScenarios:\n");
foreach (array_keys($allScenarios) as $name) {
fwrite(STDERR, " $name\n");
}
exit(1);
}
$arrays = $allScenarios[$selected];
$iterations = 1;
do {
[$elapsed] = measure($arrays, $iterations);
if ($elapsed >= TARGET_SAMPLE_NS || $iterations >= 1_048_576) {
break;
}
$iterations *= 2;
} while (true);
measure($arrays, $iterations);
$samples = [];
$checksum = 0;
for ($sample = 0; $sample < SAMPLE_COUNT; $sample++) {
[$elapsed, $sampleChecksum] = measure($arrays, $iterations);
$samples[] = $elapsed / $iterations;
$checksum ^= $sampleChecksum;
}
sort($samples);
$median = $samples[intdiv(count($samples), 2)];
printf(
"%s iterations=%d samples=%d median_us=%.3f min_us=%.3f max_us=%.3f checksum=%d\n",
$selected,
$iterations,
SAMPLE_COUNT,
$median / 1_000,
$samples[0] / 1_000,
$samples[array_key_last($samples)] / 1_000,
$checksum,
); |
Member
|
I don't love the additional code complexity, but the benchmark result seems worth it :/ |
Member
|
Current algo:
New algo:
New algo is clearly superior. Could the same algorithm be used in all cases, not only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a hash-based fast path to
array_intersect()when all input values are integers or strings, while preserving existing behavior and falling back to the generic implementation for other value types.Local benchmarks show supported inputs running between 4.9x and 111.8x faster, with approximately 1.3% to 1.8% overhead in the measured fallback cases.