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 only passing resolver args to resolver and canceller if callback requires them #113

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,36 @@ private function extract($promise)

private function call(callable $callback)
{
// Use reflection to inspect number of arguments expected by this callback.
// We did some careful benchmarking here: Using reflection to avoid unneeded
// function arguments is actually faster than blindly passing them.
Copy link

Choose a reason for hiding this comment

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

The reason for it being faster isn't avoiding the arguments being passed, but rather the three closure creations that are saved.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kelunik Do you feel that this helps with understanding the motivation for this change? If so, I encourage you to file this as a new documentation PR and maybe back this with some numbers :shipit:

Copy link

Choose a reason for hiding this comment

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

@clue I think the motivation is completely covered by the circular references, the rest is just additional background on the performance of the patch. It's not super important to change, just a minor note I had while reading.

// Also, this helps avoiding unnecessary function arguments in the call stack
// if the callback creates an Exception (creating garbage cycles).
if (is_array($callback)) {
$ref = new \ReflectionMethod($callback[0], $callback[1]);
} elseif (is_object($callback) && !$callback instanceof \Closure) {
$ref = new \ReflectionMethod($callback, '__invoke');
} else {
$ref = new \ReflectionFunction($callback);
}
$args = $ref->getNumberOfParameters();

try {
$callback(
function ($value = null) {
$this->resolve($value);
},
function ($reason = null) {
$this->reject($reason);
},
function ($update = null) {
$this->notify($update);
}
);
if ($args === 0) {
$callback();
} else {
$callback(
function ($value = null) {
$this->resolve($value);
},
function ($reason = null) {
$this->reject($reason);
},
function ($update = null) {
$this->notify($update);
}
);
}
} catch (\Throwable $e) {
$this->reject($e);
} catch (\Exception $e) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public function shouldRejectIfResolverThrowsException()
->then($this->expectCallableNever(), $mock);
}

/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException()
{
gc_collect_cycles();
$promise = new Promise(function () {
throw new \Exception('foo');
});
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldFulfillIfFullfilledWithSimplePromise()
{
Expand Down
27 changes: 21 additions & 6 deletions tests/PromiseTest/CancelTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,30 @@ abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function cancelShouldCallCancellerWithResolverArguments()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->isType('callable'), $this->isType('callable'), $this->isType('callable'));
$args = null;
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject, $notify) use (&$args) {
$args = func_get_args();
});

$adapter = $this->getPromiseTestAdapter($mock);
$adapter->promise()->cancel();

$this->assertCount(3, $args);
$this->assertTrue(is_callable($args[0]));
$this->assertTrue(is_callable($args[1]));
$this->assertTrue(is_callable($args[2]));
}

/** @test */
public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed()
{
$args = null;
$adapter = $this->getPromiseTestAdapter(function () use (&$args) {
$args = func_num_args();
});

$adapter->promise()->cancel();

$this->assertSame(0, $args);
}

/** @test */
Expand Down