-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
Description
Large arrays pay a very large and frequent GC penalty in long-lived applications.
It should be possible to significantly reduce this impact by introducing a flag like GC_MAY_HAVE_COLLECTABLE_ELEMENTS for arrays, and not scanning the array if no such elements are present.
In the following test case, GC runs take several hundred ms with these arrays containing only primitives. If $test
is repeatedly ref'd and unref'd, as is the case for long-lived objects in some applications, these huge arrays will repeatedly get scanned for GC-able elements, wasting CPU time.
Of particular interest is the fact that the readonly
array is obviously not modifiable, so it can't possibly contain any GC-able elements if it didn't have any the first time it was scanned. Yet if you modify the test case you can easily tell from the length of time the GC spends that it does still repeatedly scan this array.
class Test{
public function __construct(
private readonly array $readonlyArray,
public array $writableArray = []
){}
}
ini_set('memory_limit', '-1');
$test = new Test(array_fill(0, 50_000_000, 0), array_fill(0, 50_000_000, 0));
$ref2 = new \stdClass();
$ref2->test = $test; //increase refcount
unset($test); //decrease refcount, object is now in gc root buffer
$start = hrtime(true);
gc_collect_cycles();
var_dump(number_format(hrtime(true) - $start));
$test = $ref2->test; //increase refcount
unset($ref2->test); //decrease refcount
$start = hrtime(true);
gc_collect_cycles();
var_dump(number_format(hrtime(true) - $start));