Skip to content

Improve memory consumption by cleaning up canceller function references when they are no longer needed - #119

Merged
jsor merged 1 commit into
reactphp:2.xfrom
clue-labs:canceller
May 7, 2018
Merged

Improve memory consumption by cleaning up canceller function references when they are no longer needed#119
jsor merged 1 commit into
reactphp:2.xfrom
clue-labs:canceller

Conversation

@clue

@clueclue commented May 6, 2018

Copy link
Copy Markdown
Member

Settling a pending Promise or Deferred means that its canceller function (if any) can no longer be invoked. By removing the reference to the canceller function in this case, we can ensure that an explicit reference back to the promise in the canceller 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 #117 and #118 and the ideas discussed in #46.

The gist here is that using a canceller that has an implicit reference to the promise (quite common due to closures bindings) now no longer causes a cyclic garbage reference in any exception trace and consumers of this package do not need to take special care of this.

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

Invoking the benchmarking example from #113 shows no effect, as reassigning variables is a rather cheap operation. In fact, explicitly adding some bogus references to this example shows a very significant performance and memory improvement! Initially this peaked somewhere around 8 MB on my system taking 7.3s. After applying this patch, this script reports a constant memory consumption of around 0.6 MB taking 1.9s.

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

@clueclue added this to the v2.6.0 milestone May 6, 2018
jsor
jsor approved these changes May 7, 2018
@jsor
jsor merged commit 308b916 into reactphp:2.xMay 7, 2018
@clue
clue deleted the canceller branch May 7, 2018 09:41
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>
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
clue added a commit to clue-labs/promise-timer that referenced this pull request Jun 11, 2018
@clueclue mentioned this pull request Jun 7, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@clue@jsor@WyriHaximus