Skip to content

Commit 368a5cb

Browse files
authored
Merge pull request #134 from WyriHaximus-secret-labs/function-name-look-up-performance-improvement
Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function
2 parents c1aad8e + ad61ee5 commit 368a5cb

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

src/Deferred.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ 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
}

src/Internal/CancellationQueue.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public function __invoke()
2222

2323
public function enqueue($cancellable)
2424
{
25-
if (!method_exists($cancellable, 'then') || !method_exists($cancellable, 'cancel')) {
25+
if (!\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) {
2626
return;
2727
}
2828

29-
$length = array_push($this->queue, $cancellable);
29+
$length = \array_push($this->queue, $cancellable);
3030

3131
if ($this->started && 1 === $length) {
3232
$this->drain();
@@ -35,7 +35,7 @@ public function enqueue($cancellable)
3535

3636
private function drain()
3737
{
38-
for ($i = key($this->queue); isset($this->queue[$i]); $i++) {
38+
for ($i = \key($this->queue); isset($this->queue[$i]); $i++) {
3939
$cancellable = $this->queue[$i];
4040

4141
$exception = null;

src/Internal/Queue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ final class Queue
1111

1212
public function enqueue(callable $task)
1313
{
14-
if (1 === array_push($this->queue, $task)) {
14+
if (1 === \array_push($this->queue, $task)) {
1515
$this->drain();
1616
}
1717
}
1818

1919
private function drain()
2020
{
21-
for ($i = key($this->queue); isset($this->queue[$i]); $i++) {
21+
for ($i = \key($this->queue); isset($this->queue[$i]); $i++) {
2222
$task = $this->queue[$i];
2323

2424
$exception = null;

src/Promise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ private function call(callable $callback)
181181
// function arguments is actually faster than blindly passing them.
182182
// Also, this helps avoiding unnecessary function arguments in the call stack
183183
// if the callback creates an Exception (creating garbage cycles).
184-
if (is_array($callback)) {
184+
if (\is_array($callback)) {
185185
$ref = new \ReflectionMethod($callback[0], $callback[1]);
186-
} elseif (is_object($callback) && !$callback instanceof \Closure) {
186+
} elseif (\is_object($callback) && !$callback instanceof \Closure) {
187187
$ref = new \ReflectionMethod($callback, '__invoke');
188188
} else {
189189
$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: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ function resolve($promiseOrValue = null)
88
return $promiseOrValue;
99
}
1010

11-
if (method_exists($promiseOrValue, 'then')) {
11+
if (\method_exists($promiseOrValue, 'then')) {
1212
$canceller = null;
1313

14-
if (method_exists($promiseOrValue, 'cancel')) {
14+
if (\method_exists($promiseOrValue, 'cancel')) {
1515
$canceller = [$promiseOrValue, 'cancel'];
1616
}
1717

@@ -63,7 +63,7 @@ function any(array $promisesOrValues)
6363
{
6464
return some($promisesOrValues, 1)
6565
->then(function ($val) {
66-
return array_shift($val);
66+
return \array_shift($val);
6767
});
6868
}
6969

@@ -73,12 +73,12 @@ function some(array $promisesOrValues, $howMany)
7373
return resolve([]);
7474
}
7575

76-
$len = count($promisesOrValues);
76+
$len = \count($promisesOrValues);
7777

7878
if ($len < $howMany) {
7979
return reject(
8080
new Exception\LengthException(
81-
sprintf(
81+
\sprintf(
8282
'Input array must contain at least %d item%s but contains only %s item%s.',
8383
$howMany,
8484
1 === $howMany ? '' : 's',
@@ -139,7 +139,7 @@ function map(array $promisesOrValues, callable $mapFunc)
139139
$cancellationQueue = new Internal\CancellationQueue();
140140

141141
return new Promise(function ($resolve, $reject) use ($promisesOrValues, $mapFunc, $cancellationQueue) {
142-
$toResolve = count($promisesOrValues);
142+
$toResolve = \count($promisesOrValues);
143143
$values = [];
144144

145145
foreach ($promisesOrValues as $i => $promiseOrValue) {
@@ -167,7 +167,7 @@ function reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = n
167167
$cancellationQueue = new Internal\CancellationQueue();
168168

169169
return new Promise(function ($resolve, $reject) use ($promisesOrValues, $reduceFunc, $initialValue, $cancellationQueue) {
170-
$total = count($promisesOrValues);
170+
$total = \count($promisesOrValues);
171171
$i = 0;
172172

173173
$wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $cancellationQueue, $total, &$i) {
@@ -184,7 +184,7 @@ function reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = n
184184

185185
$cancellationQueue->enqueue($initialValue);
186186

187-
array_reduce($promisesOrValues, $wrappedReduceFunc, resolve($initialValue))
187+
\array_reduce($promisesOrValues, $wrappedReduceFunc, resolve($initialValue))
188188
->done($resolve, $reject);
189189
}, $cancellationQueue);
190190
}
@@ -209,13 +209,13 @@ function enqueue(callable $task)
209209
function fatalError($error)
210210
{
211211
try {
212-
trigger_error($error, E_USER_ERROR);
212+
\trigger_error($error, E_USER_ERROR);
213213
} catch (\Throwable $e) {
214-
set_error_handler(null);
215-
trigger_error($error, E_USER_ERROR);
214+
\set_error_handler(null);
215+
\trigger_error($error, E_USER_ERROR);
216216
} catch (\Exception $e) {
217-
set_error_handler(null);
218-
trigger_error($error, E_USER_ERROR);
217+
\set_error_handler(null);
218+
\trigger_error($error, E_USER_ERROR);
219219
}
220220
}
221221

@@ -224,13 +224,13 @@ function fatalError($error)
224224
*/
225225
function _checkTypehint(callable $callback, $object)
226226
{
227-
if (!is_object($object)) {
227+
if (!\is_object($object)) {
228228
return true;
229229
}
230230

231-
if (is_array($callback)) {
231+
if (\is_array($callback)) {
232232
$callbackReflection = new \ReflectionMethod($callback[0], $callback[1]);
233-
} elseif (is_object($callback) && !$callback instanceof \Closure) {
233+
} elseif (\is_object($callback) && !$callback instanceof \Closure) {
234234
$callbackReflection = new \ReflectionMethod($callback, '__invoke');
235235
} else {
236236
$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)