Skip to content

Commit e3bdce5

Browse files
authored
Improved performance and quality.
1 parent 8158985 commit e3bdce5

File tree

11 files changed

+222
-14
lines changed

11 files changed

+222
-14
lines changed

src/vennv/vapm/EventLoop.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ public static function getReturns(): array
113113

114114
private static function clearGarbage(): void
115115
{
116-
foreach (self::$returns as $id => $promise)
116+
foreach (GeneratorManager::getFromArray(self::$returns) as $id => $promise)
117117
{
118+
/** @var Promise $promise */
118119
if ($promise->canDrop())
119120
{
121+
/** @var int $id */
120122
self::removeReturn($id);
121123
}
122124
}
@@ -132,8 +134,9 @@ protected static function run(): void
132134
GreenThread::run();
133135
}
134136

135-
foreach (self::$queues as $id => $promise)
137+
foreach (GeneratorManager::getFromArray(self::$queues) as $id => $promise)
136138
{
139+
/** @var Promise $promise */
137140
$fiber = $promise->getFiber();
138141

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

148151
if ($fiber->isTerminated() && ($promise->getStatus() !== StatusPromise::PENDING || $promise->isJustGetResult()))
149152
{
153+
/** @var int $id */
150154
MicroTask::addTask($id, $promise);
151155
self::removeQueue($id);
152156
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2023 VennV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
declare(strict_types = 1);
26+
27+
namespace vennv\vapm;
28+
29+
use Generator;
30+
31+
final class GeneratorManager implements GeneratorManagerInterface
32+
{
33+
34+
/**
35+
* @param array $array
36+
* @return Generator
37+
* @phpstan-param array<int|float|string, mixed> $array
38+
*
39+
* This method is used to get a generator from an array.
40+
*/
41+
public static function getFromArray(array $array): Generator
42+
{
43+
yield from $array;
44+
}
45+
46+
/**
47+
* @param Generator $generator
48+
* @return Generator
49+
*
50+
* This method is used to get a generator from a generator.
51+
*/
52+
public static function getFromGenerator(Generator $generator): Generator
53+
{
54+
yield from $generator;
55+
}
56+
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2023 VennV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
declare(strict_types = 1);
26+
27+
namespace vennv\vapm;
28+
29+
use Generator;
30+
31+
interface GeneratorManagerInterface
32+
{
33+
34+
/**
35+
* @param array $array
36+
* @return Generator
37+
* @phpstan-param array<int|float|string, mixed> $array
38+
*
39+
* This method is used to get a generator from an array.
40+
*/
41+
public static function getFromArray(array $array): Generator;
42+
43+
/**
44+
* @param Generator $generator
45+
* @return Generator
46+
*
47+
* This method is used to get a generator from a generator.
48+
*/
49+
public static function getFromGenerator(Generator $generator): Generator;
50+
51+
}

src/vennv/vapm/GreenThread.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function register(string|int $name, callable $callback, array $par
8080
*/
8181
public static function run(): void
8282
{
83-
foreach (self::$fibers as $i => $fiber)
83+
foreach (GeneratorManager::getFromArray(self::$fibers) as $i => $fiber)
8484
{
8585
if (!self::$status[self::$names[$i]]->canWakeUp())
8686
{
@@ -91,6 +91,7 @@ public static function run(): void
9191

9292
try
9393
{
94+
/** @var Fiber $fiber */
9495
if (!$fiber->isStarted())
9596
{
9697
$fiber->start(...self::$params[$i]);

src/vennv/vapm/Info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class Info
3030
{
3131

32-
public const VERSION = "1.6.1";
32+
public const VERSION = "1.6.7";
3333

3434
public const AUTHOR = "VennV";
3535

src/vennv/vapm/MacroTask.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public static function getTasks(): array
7878

7979
public static function run(): void
8080
{
81-
foreach (self::$tasks as $task)
81+
foreach (GeneratorManager::getFromArray(self::$tasks) as $task)
8282
{
83+
/** @var SampleMacro $task */
8384
if ($task->checkTimeOut())
8485
{
8586
$task->run();

src/vennv/vapm/MicroTask.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ public static function getTasks(): array
6565
*/
6666
public static function run(): void
6767
{
68-
foreach (self::$tasks as $id => $promise)
68+
foreach (GeneratorManager::getFromArray(self::$tasks) as $id => $promise)
6969
{
70+
/**
71+
* @var Promise $promise
72+
* @var int $id
73+
*/
7074
$promise->useCallbacks();
7175
$promise->setTimeEnd(microtime(true));
7276

src/vennv/vapm/Promise.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private function checkStatus(array $callbacks, mixed $return) : void
300300
{
301301
$cancel = false;
302302

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

318318
if (!is_null($queue1))
319319
{
320+
/** @var callable $callable */
320321
$queue1->then($callable);
321322

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

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

366368
while ($isSolved === false)
367369
{
368-
foreach ($promises as $promise)
370+
foreach (GeneratorManager::getFromArray($promises) as $promise)
369371
{
370372
if (is_callable($promise))
371373
{
@@ -417,14 +419,14 @@ public static function all(array $promises): Promise
417419
*/
418420
public static function allSettled(array $promises): Promise
419421
{
420-
$promise = new Promise(function($resolve, $reject) use ($promises): void
422+
$promise = new Promise(function($resolve) use ($promises): void
421423
{
422424
$results = [];
423425
$isSolved = false;
424426

425427
while ($isSolved === false)
426428
{
427-
foreach ($promises as $promise)
429+
foreach (GeneratorManager::getFromArray($promises) as $promise)
428430
{
429431
if (is_callable($promise))
430432
{
@@ -437,7 +439,7 @@ public static function allSettled(array $promises): Promise
437439

438440
if ($return !== null)
439441
{
440-
$results[] = $return->getResult();
442+
$results[] = new PromiseResult($return->getStatus(), $return->getResult());
441443
}
442444
}
443445

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

475477
while ($isSolved === false)
476478
{
477-
foreach ($promises as $promise)
479+
foreach (GeneratorManager::getFromArray($promises) as $promise)
478480
{
479481
if (is_callable($promise))
480482
{
@@ -532,7 +534,7 @@ public static function race(array $promises): Promise
532534

533535
while ($isSolved === false)
534536
{
535-
foreach ($promises as $promise)
537+
foreach (GeneratorManager::getFromArray($promises) as $promise)
536538
{
537539
if (is_callable($promise))
538540
{

src/vennv/vapm/PromiseResult.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2023 VennV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
declare(strict_types = 1);
26+
27+
namespace vennv\vapm;
28+
29+
final class PromiseResult implements PromiseResultInterface
30+
{
31+
32+
private string $status;
33+
34+
private mixed $result;
35+
36+
public function __construct(string $status, mixed $result)
37+
{
38+
$this->status = $status;
39+
$this->result = $result;
40+
}
41+
42+
public function getStatus(): string
43+
{
44+
return $this->status;
45+
}
46+
47+
public function getResult(): mixed
48+
{
49+
return $this->result;
50+
}
51+
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2023 VennV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
declare(strict_types = 1);
26+
27+
namespace vennv\vapm;
28+
29+
interface PromiseResultInterface
30+
{
31+
32+
public function getStatus(): string;
33+
34+
public function getResult(): mixed;
35+
36+
}

0 commit comments

Comments
 (0)