Skip to content

Commit

Permalink
Merge pull request #88 from clue-labs/cleanup-map
Browse files Browse the repository at this point in the history
Improve performance by avoiding unneeded references in `FiberMap`
  • Loading branch information
SimonFrings authored May 24, 2024
2 parents 16d653f + 221956d commit 9c3dc0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 31 deletions.
24 changes: 1 addition & 23 deletions src/FiberMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,9 @@
*/
final class FiberMap
{
/** @var array<int,bool> */
private static array $status = [];

/** @var array<int,PromiseInterface<T>> */
private static array $map = [];

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function register(\Fiber $fiber): void
{
self::$status[\spl_object_id($fiber)] = false;
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function cancel(\Fiber $fiber): void
{
self::$status[\spl_object_id($fiber)] = true;
}

/**
* @param \Fiber<mixed,mixed,mixed,mixed> $fiber
* @param PromiseInterface<T> $promise
Expand All @@ -40,9 +25,8 @@ public static function setPromise(\Fiber $fiber, PromiseInterface $promise): voi

/**
* @param \Fiber<mixed,mixed,mixed,mixed> $fiber
* @param PromiseInterface<T> $promise
*/
public static function unsetPromise(\Fiber $fiber, PromiseInterface $promise): void
public static function unsetPromise(\Fiber $fiber): void
{
unset(self::$map[\spl_object_id($fiber)]);
}
Expand All @@ -55,10 +39,4 @@ public static function getPromise(\Fiber $fiber): ?PromiseInterface
{
return self::$map[\spl_object_id($fiber)] ?? null;
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function unregister(\Fiber $fiber): void
{
unset(self::$status[\spl_object_id($fiber)], self::$map[\spl_object_id($fiber)]);
}
}
13 changes: 5 additions & 8 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,13 @@ function async(callable $function): callable
$reject($exception);
} finally {
assert($fiber instanceof \Fiber);
FiberMap::unregister($fiber);
FiberMap::unsetPromise($fiber);
}
});

FiberMap::register($fiber);

$fiber->start();
}, function () use (&$fiber): void {
assert($fiber instanceof \Fiber);
FiberMap::cancel($fiber);
$promise = FiberMap::getPromise($fiber);
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
$promise->cancel();
Expand Down Expand Up @@ -294,9 +291,9 @@ function await(PromiseInterface $promise): mixed
$lowLevelFiber = \Fiber::getCurrent();

$promise->then(
function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFiber, $promise): void {
function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFiber): void {
if ($lowLevelFiber !== null) {
FiberMap::unsetPromise($lowLevelFiber, $promise);
FiberMap::unsetPromise($lowLevelFiber);
}

/** @var ?\Fiber<mixed,mixed,mixed,mixed> $fiber */
Expand All @@ -309,9 +306,9 @@ function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFibe

$fiber->resume($value);
},
function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowLevelFiber, $promise): void {
function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowLevelFiber): void {
if ($lowLevelFiber !== null) {
FiberMap::unsetPromise($lowLevelFiber, $promise);
FiberMap::unsetPromise($lowLevelFiber);
}

if (!$throwable instanceof \Throwable) {
Expand Down

0 comments on commit 9c3dc0e

Please sign in to comment.