php / php/php-src

Remember when large arrays don't contain any collectable elements, and avoid wasting GC time on them

Open
#19,608 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: GC Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

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));

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named in the issue. Start by reproducing the provided PHP script and measuring gc_collect_cycles() for the readonly and writable arrays, then trace the array handling involved in garbage-collector scanning. Done means demonstrating that arrays known to contain no collectable elements avoid repeated scans without changing collection behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.