Skip to content

Optimize array_diff() for long values#22648

Open
mehmetcansahin wants to merge 1 commit into
php:masterfrom
mehmetcansahin:array-diff-long-fast-path
Open

Optimize array_diff() for long values#22648
mehmetcansahin wants to merge 1 commit into
php:masterfrom
mehmetcansahin:array-diff-long-fast-path

Conversation

@mehmetcansahin

@mehmetcansahin mehmetcansahin commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

array_diff() currently builds its exclusion set from string representations, causing avoidable conversions for large integer-only inputs. This adds a collision-safe fast path using a bitset for compact integer ranges; mixed or sparse inputs continue through the existing path.

$a = range(0, 200000);
$b = range(100000, 300000);

$start = microtime(true);
for ($i = 0; $i < 10; $i++) {
    array_diff($a, $b);
}
printf("%.3fs\n", microtime(true) - $start);

Result: ~0.12s before, ~0.015s after (approximately 8x faster). Unlike the earlier integer-keyed HashTable approach, this retains linear worst-case behavior.

Tests cover integer limits, duplicates, key and reference preservation, and fallback behavior.

@mehmetcansahin mehmetcansahin requested a review from bukka as a code owner July 9, 2026 08:26
@mehmetcansahin mehmetcansahin marked this pull request as draft July 9, 2026 19:51
@mehmetcansahin mehmetcansahin force-pushed the array-diff-long-fast-path branch from abc4fc9 to 9e1e3c1 Compare July 9, 2026 20:15
@mehmetcansahin mehmetcansahin marked this pull request as ready for review July 9, 2026 20:44
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