]> granicus.if.org Git - php/commit
Speed up array_intersect/array_diff/array_filter
authorTyson Andre <tysonandre775@hotmail.com>
Sat, 7 Dec 2019 15:09:32 +0000 (10:09 -0500)
committerTyson Andre <tysonandre775@hotmail.com>
Wed, 11 Dec 2019 00:29:49 +0000 (19:29 -0500)
commit366713d250d1ee01ec20f3b71fcd1c720519edcd
treea0f22a271d839d5dc37a84f07768a999133745b4
parent8fb3ef6e3784d9f07e688cea4571ab42dfaa8895
Speed up array_intersect/array_diff/array_filter

Use zend_hash_update instead of zend_hash_add.

These are taking a subset of keys from an array with unique keys,
so the result should also have unique keys.
(this is already done for array_map())

Also, speed up array_intersect and array_diff slightly by
using ZEND_HASH_FOREACH macros.
This way, it doesn't need to load the same buckets and array counts
from memory every time (compiler previously couldn't infer they won't change)

```php
<?php
// $n=10000 now takes 0.095 seconds instead of 0.102
function test_bench(int $n) {
    $values = range(0,1000);
    $other = range(0,1000);
    unset($other[500]);
    unset($values[400]);

    $total = 0;
    for ($i = 0; $i < $n; $i++) {
        $total += count(array_intersect_key($values, $other));
    }
    return $total;
}
```
ext/standard/array.c