Skip to content

Commit 79820b9

Browse files
committed
Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function
1 parent 8e41741 commit 79820b9

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

src/CancellationQueue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public function __invoke()
1919

2020
public function enqueue($cancellable)
2121
{
22-
if (!method_exists($cancellable, 'then') || !method_exists($cancellable, 'cancel')) {
22+
if (!\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) {
2323
return;
2424
}
2525

26-
$length = array_push($this->queue, $cancellable);
26+
$length = \array_push($this->queue, $cancellable);
2727

2828
if ($this->started && 1 === $length) {
2929
$this->drain();

src/Deferred.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function resolve($value = null)
3333
{
3434
$this->promise();
3535

36-
call_user_func($this->resolveCallback, $value);
36+
\call_user_func($this->resolveCallback, $value);
3737
}
3838

3939
public function reject($reason = null)
4040
{
4141
$this->promise();
4242

43-
call_user_func($this->rejectCallback, $reason);
43+
\call_user_func($this->rejectCallback, $reason);
4444
}
4545

4646
/**
@@ -51,7 +51,7 @@ public function notify($update = null)
5151
{
5252
$this->promise();
5353

54-
call_user_func($this->notifyCallback, $update);
54+
\call_user_func($this->notifyCallback, $update);
5555
}
5656

5757
/**

src/LazyPromise.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function promise()
5050
{
5151
if (null === $this->promise) {
5252
try {
53-
$this->promise = resolve(call_user_func($this->factory));
53+
$this->promise = resolve(\call_user_func($this->factory));
5454
} catch (\Throwable $exception) {
5555
$this->promise = new RejectedPromise($exception);
5656
} catch (\Exception $exception) {

src/Promise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ private function call(callable $cb)
201201
// function arguments is actually faster than blindly passing them.
202202
// Also, this helps avoiding unnecessary function arguments in the call stack
203203
// if the callback creates an Exception (creating garbage cycles).
204-
if (is_array($callback)) {
204+
if (\is_array($callback)) {
205205
$ref = new \ReflectionMethod($callback[0], $callback[1]);
206-
} elseif (is_object($callback) && !$callback instanceof \Closure) {
206+
} elseif (\is_object($callback) && !$callback instanceof \Closure) {
207207
$ref = new \ReflectionMethod($callback, '__invoke');
208208
} else {
209209
$ref = new \ReflectionFunction($callback);

src/UnhandledRejectionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct($reason)
1919
{
2020
$this->reason = $reason;
2121

22-
$message = sprintf('Unhandled Rejection: %s', json_encode($reason));
22+
$message = \sprintf('Unhandled Rejection: %s', \json_encode($reason));
2323

2424
parent::__construct($message, 0);
2525
}

src/functions.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ function resolve($promiseOrValue = null)
1010

1111
// Check is_object() first to avoid method_exists() triggering
1212
// class autoloaders if $promiseOrValue is a string.
13-
if (is_object($promiseOrValue) && method_exists($promiseOrValue, 'then')) {
13+
if (\is_object($promiseOrValue) && \method_exists($promiseOrValue, 'then')) {
1414
$canceller = null;
1515

16-
if (method_exists($promiseOrValue, 'cancel')) {
16+
if (\method_exists($promiseOrValue, 'cancel')) {
1717
$canceller = [$promiseOrValue, 'cancel'];
1818
}
1919

@@ -70,7 +70,7 @@ function any($promisesOrValues)
7070
{
7171
return some($promisesOrValues, 1)
7272
->then(function ($val) {
73-
return array_shift($val);
73+
return \array_shift($val);
7474
});
7575
}
7676

@@ -82,16 +82,16 @@ function some($promisesOrValues, $howMany)
8282
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $howMany, $cancellationQueue) {
8383
resolve($promisesOrValues)
8484
->done(function ($array) use ($howMany, $cancellationQueue, $resolve, $reject, $notify) {
85-
if (!is_array($array) || $howMany < 1) {
85+
if (!\is_array($array) || $howMany < 1) {
8686
$resolve([]);
8787
return;
8888
}
8989

90-
$len = count($array);
90+
$len = \count($array);
9191

9292
if ($len < $howMany) {
9393
throw new Exception\LengthException(
94-
sprintf(
94+
\sprintf(
9595
'Input array must contain at least %d item%s but contains only %s item%s.',
9696
$howMany,
9797
1 === $howMany ? '' : 's',
@@ -148,12 +148,12 @@ function map($promisesOrValues, callable $mapFunc)
148148
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $mapFunc, $cancellationQueue) {
149149
resolve($promisesOrValues)
150150
->done(function ($array) use ($mapFunc, $cancellationQueue, $resolve, $reject, $notify) {
151-
if (!is_array($array) || !$array) {
151+
if (!\is_array($array) || !$array) {
152152
$resolve([]);
153153
return;
154154
}
155155

156-
$toResolve = count($array);
156+
$toResolve = \count($array);
157157
$values = [];
158158

159159
foreach ($array as $i => $promiseOrValue) {
@@ -186,11 +186,11 @@ function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null)
186186
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $reduceFunc, $initialValue, $cancellationQueue) {
187187
resolve($promisesOrValues)
188188
->done(function ($array) use ($reduceFunc, $initialValue, $cancellationQueue, $resolve, $reject, $notify) {
189-
if (!is_array($array)) {
189+
if (!\is_array($array)) {
190190
$array = [];
191191
}
192192

193-
$total = count($array);
193+
$total = \count($array);
194194
$i = 0;
195195

196196
// Wrap the supplied $reduceFunc with one that handles promises and then
@@ -209,7 +209,7 @@ function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null)
209209

210210
$cancellationQueue->enqueue($initialValue);
211211

212-
array_reduce($array, $wrappedReduceFunc, resolve($initialValue))
212+
\array_reduce($array, $wrappedReduceFunc, resolve($initialValue))
213213
->done($resolve, $reject, $notify);
214214
}, $reject, $notify);
215215
}, $cancellationQueue);
@@ -218,13 +218,13 @@ function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null)
218218
// Internal functions
219219
function _checkTypehint(callable $callback, $object)
220220
{
221-
if (!is_object($object)) {
221+
if (!\is_object($object)) {
222222
return true;
223223
}
224224

225-
if (is_array($callback)) {
225+
if (\is_array($callback)) {
226226
$callbackReflection = new \ReflectionMethod($callback[0], $callback[1]);
227-
} elseif (is_object($callback) && !$callback instanceof \Closure) {
227+
} elseif (\is_object($callback) && !$callback instanceof \Closure) {
228228
$callbackReflection = new \ReflectionMethod($callback, '__invoke');
229229
} else {
230230
$callbackReflection = new \ReflectionFunction($callback);

src/functions_include.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
if (!function_exists('React\Promise\resolve')) {
3+
if (!\function_exists('React\Promise\resolve')) {
44
require __DIR__.'/functions.php';
55
}

0 commit comments

Comments
 (0)