Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 13, 2018

Conversation

clue
Copy link
Member

@clue clue commented Jun 12, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants