Skip to content

Commit 81ab092

Browse files
committed
Added EventLoop Optimization
1 parent 7355122 commit 81ab092

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/EventLoop/EventLoop.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,21 @@ private function __construct()
113113
);
114114
}
115115

116+
/**
117+
* Check if event loop is using UV extension.
118+
*
119+
* @return bool True if UV is available, false otherwise
120+
*/
116121
public function isUsingUv(): bool
117122
{
118123
return UvDetector::isUvAvailable();
119124
}
120125

126+
/**
127+
* Get the socket manager.
128+
*
129+
* @return SocketManager The socket manager instance
130+
*/
121131
public function getSocketManager(): SocketManager
122132
{
123133
return $this->socketManager;
@@ -264,15 +274,11 @@ public function run(): void
264274
$this->optimizeLoop();
265275
}
266276

267-
// Only sleep if NOT using UV
268277
if (!$isUsingUv && $this->sleepHandler->shouldSleep($hasImmediateWork)) {
269278
$sleepTime = $this->sleepHandler->calculateOptimalSleep();
270279
$this->sleepHandler->sleep($sleepTime);
271280
}
272281

273-
// When using UV, let the UV loop control timing
274-
// Don't add any manual sleep - UV will handle it
275-
276282
if ($this->iterationCount >= self::MAX_ITERATIONS) {
277283
$this->iterationCount = 0;
278284
}

test.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
echo "Using UV: " . ($loop->isUsingUv() ? "YES" : "NO") . "\n";
1010

11-
// Test timer precision
12-
$start = microtime(true);
13-
for ($i = 0; $i < 100; $i++) {
14-
$loop->addTimer(0.01 * $i, function () use ($i) {
15-
echo "Timer $i fired\n";
11+
for ($i = 1; $i <= 10; $i++) {
12+
$startTime = microtime(true);
13+
run(function () {
14+
$timers = array_fill(0, 30000, delay(1));
15+
await(all($timers));
1616
});
17+
$endTime = microtime(true);
18+
$executionTime = $endTime - $startTime;
19+
echo "Execution time: " . $executionTime . " seconds\n";
1720
}
18-
19-
$loop->run();

0 commit comments

Comments
 (0)