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 by using static child canceller callback without binding to parent promise #117

Merged
merged 1 commit into from
May 6, 2018

Conversation

clue
Copy link
Member

@clue clue commented May 4, 2018

Calling cancel() on a child promise that follows its parent promise now no longer causes a cyclic garbage reference in any exception trace as discussed in #46. This builds on top of the implementation approach in #115 and #116 and the ideas discussed in #46.

The gist here is that $promise->then()->cancel() now no longer causes any unexpected cyclic garbage reference and consumers of this package do not need to take special care of this.

Similar patches have been introduced with #115 and #116 to implement a similar logic for the resolution and cancellation callbacks and this PR takes advantage of this for its internal implementation.

Invoking the benchmarking example from #113 shows no effect because it does not use child promises. After patching this to use $promise->then()->cancel() instead of $promise->cancel(), this shows a very significant performance and memory improvement! Initially this peaked somewhere around 15 MB on my system taking 24s. After applying this patch, this script reports a constant memory consumption of around 0.6 MB taking 5s.

This PR actually includes a test that shows how garbage memory references are no longer an issue in any supported PHP version and how cancelling a child promise no longer causes any such references on its own (this means that this requires no effort on the consumer side).

@clue clue added this to the v2.6.0 milestone May 4, 2018
@clue clue force-pushed the static-canceller branch from a8c9cf1 to 8d8996f Compare May 4, 2018 07:48
src/Promise.php Outdated

if ($this->requiredCancelRequests <= 0) {
$this->cancel();
return new static($this->resolver($onFulfilled, $onRejected, $onProgress), self::parentCancellerFunction($parent)); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: missing linebreak before the closing } of the method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting, fixed! :shipit:

@WyriHaximus WyriHaximus self-requested a review May 4, 2018 07:54
@WyriHaximus WyriHaximus merged commit 27e3b95 into reactphp:2.x May 6, 2018
@clue clue deleted the static-canceller branch May 6, 2018 09:28
jsor added a commit to jsor-labs/pact that referenced this pull request May 16, 2018
This incorporates the work done by @clue in the following PR's for reactphp/promise:

* reactphp/promise#115
* reactphp/promise#116
* reactphp/promise#117
* reactphp/promise#118
* reactphp/promise#119

Co-authored-by: Christian Lück <christian@lueck.tv>
jsor added a commit to jsor-labs/pact that referenced this pull request May 16, 2018
This incorporates the work done by @clue in the following PR's for reactphp/promise:

* reactphp/promise#113
* reactphp/promise#115
* reactphp/promise#116
* reactphp/promise#117
* reactphp/promise#118
* reactphp/promise#119

Co-authored-by: Christian Lück <christian@lueck.tv>
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