Skip to content

Commit

Permalink
Improved performance and quality.
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Jul 17, 2023
1 parent 8158985 commit e3bdce5
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/vennv/vapm/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ public static function getReturns(): array

private static function clearGarbage(): void
{
foreach (self::$returns as $id => $promise)
foreach (GeneratorManager::getFromArray(self::$returns) as $id => $promise)
{
/** @var Promise $promise */
if ($promise->canDrop())
{
/** @var int $id */
self::removeReturn($id);
}
}
Expand All @@ -132,8 +134,9 @@ protected static function run(): void
GreenThread::run();
}

foreach (self::$queues as $id => $promise)
foreach (GeneratorManager::getFromArray(self::$queues) as $id => $promise)
{
/** @var Promise $promise */
$fiber = $promise->getFiber();

if ($fiber->isSuspended())
Expand All @@ -147,6 +150,7 @@ protected static function run(): void

if ($fiber->isTerminated() && ($promise->getStatus() !== StatusPromise::PENDING || $promise->isJustGetResult()))
{
/** @var int $id */
MicroTask::addTask($id, $promise);
self::removeQueue($id);
}
Expand Down
57 changes: 57 additions & 0 deletions src/vennv/vapm/GeneratorManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* Copyright (c) 2023 VennV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types = 1);

namespace vennv\vapm;

use Generator;

final class GeneratorManager implements GeneratorManagerInterface
{

/**
* @param array $array
* @return Generator
* @phpstan-param array<int|float|string, mixed> $array
*
* This method is used to get a generator from an array.
*/
public static function getFromArray(array $array): Generator
{
yield from $array;
}

/**
* @param Generator $generator
* @return Generator
*
* This method is used to get a generator from a generator.
*/
public static function getFromGenerator(Generator $generator): Generator
{
yield from $generator;
}

}
51 changes: 51 additions & 0 deletions src/vennv/vapm/GeneratorManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* Copyright (c) 2023 VennV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types = 1);

namespace vennv\vapm;

use Generator;

interface GeneratorManagerInterface
{

/**
* @param array $array
* @return Generator
* @phpstan-param array<int|float|string, mixed> $array
*
* This method is used to get a generator from an array.
*/
public static function getFromArray(array $array): Generator;

/**
* @param Generator $generator
* @return Generator
*
* This method is used to get a generator from a generator.
*/
public static function getFromGenerator(Generator $generator): Generator;

}
3 changes: 2 additions & 1 deletion src/vennv/vapm/GreenThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function register(string|int $name, callable $callback, array $par
*/
public static function run(): void
{
foreach (self::$fibers as $i => $fiber)
foreach (GeneratorManager::getFromArray(self::$fibers) as $i => $fiber)
{
if (!self::$status[self::$names[$i]]->canWakeUp())
{
Expand All @@ -91,6 +91,7 @@ public static function run(): void

try
{
/** @var Fiber $fiber */
if (!$fiber->isStarted())
{
$fiber->start(...self::$params[$i]);
Expand Down
2 changes: 1 addition & 1 deletion src/vennv/vapm/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class Info
{

public const VERSION = "1.6.1";
public const VERSION = "1.6.7";

public const AUTHOR = "VennV";

Expand Down
3 changes: 2 additions & 1 deletion src/vennv/vapm/MacroTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ public static function getTasks(): array

public static function run(): void
{
foreach (self::$tasks as $task)
foreach (GeneratorManager::getFromArray(self::$tasks) as $task)
{
/** @var SampleMacro $task */
if ($task->checkTimeOut())
{
$task->run();
Expand Down
6 changes: 5 additions & 1 deletion src/vennv/vapm/MicroTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ public static function getTasks(): array
*/
public static function run(): void
{
foreach (self::$tasks as $id => $promise)
foreach (GeneratorManager::getFromArray(self::$tasks) as $id => $promise)
{
/**
* @var Promise $promise
* @var int $id
*/
$promise->useCallbacks();
$promise->setTimeEnd(microtime(true));

Expand Down
16 changes: 9 additions & 7 deletions src/vennv/vapm/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private function checkStatus(array $callbacks, mixed $return) : void
{
$cancel = false;

foreach ($callbacks as $case => $callable)
foreach (GeneratorManager::getFromArray($callbacks) as $case => $callable)
{
if ($return === null)
{
Expand All @@ -317,6 +317,7 @@ private function checkStatus(array $callbacks, mixed $return) : void

if (!is_null($queue1))
{
/** @var callable $callable */
$queue1->then($callable);

if (is_callable($this->callbackReject))
Expand All @@ -326,6 +327,7 @@ private function checkStatus(array $callbacks, mixed $return) : void
}
elseif (!is_null($queue2))
{
/** @var callable $callable */
$queue2->then($callable);

if (is_callable($this->callbackReject))
Expand Down Expand Up @@ -365,7 +367,7 @@ public static function all(array $promises): Promise

while ($isSolved === false)
{
foreach ($promises as $promise)
foreach (GeneratorManager::getFromArray($promises) as $promise)
{
if (is_callable($promise))
{
Expand Down Expand Up @@ -417,14 +419,14 @@ public static function all(array $promises): Promise
*/
public static function allSettled(array $promises): Promise
{
$promise = new Promise(function($resolve, $reject) use ($promises): void
$promise = new Promise(function($resolve) use ($promises): void
{
$results = [];
$isSolved = false;

while ($isSolved === false)
{
foreach ($promises as $promise)
foreach (GeneratorManager::getFromArray($promises) as $promise)
{
if (is_callable($promise))
{
Expand All @@ -437,7 +439,7 @@ public static function allSettled(array $promises): Promise

if ($return !== null)
{
$results[] = $return->getResult();
$results[] = new PromiseResult($return->getStatus(), $return->getResult());
}
}

Expand Down Expand Up @@ -474,7 +476,7 @@ public static function any(array $promises): Promise

while ($isSolved === false)
{
foreach ($promises as $promise)
foreach (GeneratorManager::getFromArray($promises) as $promise)
{
if (is_callable($promise))
{
Expand Down Expand Up @@ -532,7 +534,7 @@ public static function race(array $promises): Promise

while ($isSolved === false)
{
foreach ($promises as $promise)
foreach (GeneratorManager::getFromArray($promises) as $promise)
{
if (is_callable($promise))
{
Expand Down
52 changes: 52 additions & 0 deletions src/vennv/vapm/PromiseResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright (c) 2023 VennV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types = 1);

namespace vennv\vapm;

final class PromiseResult implements PromiseResultInterface
{

private string $status;

private mixed $result;

public function __construct(string $status, mixed $result)
{
$this->status = $status;
$this->result = $result;
}

public function getStatus(): string
{
return $this->status;
}

public function getResult(): mixed
{
return $this->result;
}

}
36 changes: 36 additions & 0 deletions src/vennv/vapm/PromiseResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* Copyright (c) 2023 VennV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types = 1);

namespace vennv\vapm;

interface PromiseResultInterface
{

public function getStatus(): string;

public function getResult(): mixed;

}
2 changes: 1 addition & 1 deletion virion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: LibVapmPMMP
version: 1.6.1
version: 1.6.7
api: 5.0.0
php:
- 8.1
Expand Down

0 comments on commit e3bdce5

Please sign in to comment.