reactphp / promise

Promises/A implementation for PHP.
https://reactphp.org/promise/
MIT License
2.38k stars 146 forks source link

Improve memory consumption for pending promises by using static internal callbacks without binding to self #124

Closed clue closed 6 years ago

clue commented 6 years ago

All previous changes that landed in https://github.com/reactphp/promise/releases/tag/v2.6.0 improved memory consumption for settled promises somewhat. Similarly, this PR addresses memory consumption for pending promises that are no longer referenced.

<?php

use React\Promise\Promise;

require __DIR__ . '/vendor/autoload.php';

for ($i = 0; $i < 1000000; ++$i) {
    $promise = new Promise(function () { });
    $promise->then('var_dump');
    unset($promise);
}

var_dump(memory_get_usage());
var_dump(gc_collect_cycles());

Initially this peaked somewhere around 8 MB on my system taking 4s. After applying this patch, this script reports a constant memory consumption of around 0.6 MB taking 1.8s

Note that this PR does not resolve all unexpected memory issues. However, it addresses a rather common problem for some consumers of this library and makes many of the higher level work-arounds obsolete. My vote would to be get this in here now as it addresses a relevant memory issue and eventually address any additional issues on top of this. :shipit:

Builds on top of #123