Skip to content

Commit

Permalink
Fixed logic mismatch
Browse files Browse the repository at this point in the history
GC threshold recalculation is supposed to use the root count _after_ GC, not before (presumably so it doesn't count non-cyclic objects).
  • Loading branch information
dktapps committed Dec 3, 2024
1 parent 6d7f05d commit 9ecc23c
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/MemoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ public function trigger(int $memory, int $limit, bool $global = false, int $trig

private int $gcThreshold = self::GC_THRESHOLD_DEFAULT;

private function adjustGcThreshold(int $count, int $roots) : void{
private function adjustGcThreshold(int $count) : void{
//TODO Very simple heuristic for dynamic GC buffer resizing:
//If there are "too few" collections, increase the collection threshold
//by a fixed step
//Adapted from zend_gc.c/gc_adjust_threshold() as of PHP 8.3.14
$roots = gc_status()["roots"];
if($count < self::GC_THRESHOLD_TRIGGER || $roots >= $this->gcThreshold){
$this->gcThreshold = min(self::GC_THRESHOLD_MAX, $this->gcThreshold + self::GC_THRESHOLD_STEP);
}elseif($this->gcThreshold > self::GC_THRESHOLD_DEFAULT){
Expand Down Expand Up @@ -259,15 +260,11 @@ public function check() : void{
if($this->garbageCollectionPeriod > 0 && ++$this->garbageCollectionTicker >= $this->garbageCollectionPeriod){
$this->garbageCollectionTicker = 0;
$this->triggerGarbageCollector();
}else{
$status = gc_status();
$roots = $status["roots"];
if($roots >= $this->gcThreshold){
Timings::$garbageCollector->startTiming();
$cycles = gc_collect_cycles();
$this->adjustGcThreshold($cycles, $roots);
Timings::$garbageCollector->stopTiming();
}
}elseif(gc_status()["roots"] >= $this->gcThreshold){
Timings::$garbageCollector->startTiming();
$cycles = gc_collect_cycles();
$this->adjustGcThreshold($cycles);
Timings::$garbageCollector->stopTiming();
}

Timings::$memoryManager->stopTiming();
Expand Down

0 comments on commit 9ecc23c

Please sign in to comment.