Hi, internals,

> On 26 Jul 2017, at 22:57, Nicolas Grekas <nicolas.gre...@gmail.com> wrote:
> 
> I'm hitting more and more the GC threshold in projects I work on (the "10k
> roots" one), leading to high CPU usage. Usually, the GC finds nothing to
> clean, so this is just a waste of CPU.

More and more php frameworks use object/array as an global container to store
config, and these container variable will be used every where, but seldom
introduce the circular reference. Apart from this, these container variable
will always be a huge tree, so iterate them will lead to high CPU usage.

> There is a way to work around: just call gc_disable(). But this means
> dealing with side effects of the engine in userland. Moreover, this also
> means leaking memory, since meanwhile roots are not populated.

In PHP-7.2, the GC_COLLECTABLE flag has been moved into the gc struct, and
this make it possible to prevent gc trace for certain zval.

So I propose to make the gc_disable function accept one zval reference as
parameter. And if gc_disable get that zval, gc_disable just drop the zval’s
GC_COLLECTABLE flag, which will hint the PHP gc not to trace that zval.

Here is my patch https://github.com/php/php-src/pull/2665/files, and a demo,

<?php
function hi($a)
{
    return $a->name;
}

$a = new stdclass;
$a->name = 'foo';

gc_disable($a);

hi($a); // these will change the refcount of $a, but never trace gc


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to