php / php/php-src

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

未关闭
#19,608 6 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Category: GC Feature Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

issue 中没有指定源文件或测试。首先复现提供的 PHP 脚本,并测量 readonly 数组和可写数组的 gc_collect_cycles();然后跟踪垃圾回收器扫描所涉及的数组处理。完成的标准是证明已知不包含可回收元素的数组可以避免重复扫描,同时不改变回收行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, php
领域
backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。