Make MemoryManager aware of ChunkCache

This commit is contained in:
Dylan K. Taylor 2021-10-28 20:28:00 +01:00
parent eb40b741ae
commit 5db3915aad
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 15 additions and 3 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine;
use pocketmine\event\server\LowMemoryEvent;
use pocketmine\network\mcpe\cache\ChunkCache;
use pocketmine\scheduler\DumpWorkerMemoryTask;
use pocketmine\scheduler\GarbageCollectionTask;
use pocketmine\timings\Timings;
@ -187,6 +188,7 @@ class MemoryManager{
foreach($this->server->getWorldManager()->getWorlds() as $world){
$world->clearCache(true);
}
ChunkCache::pruneCaches();
}
if($this->lowMemChunkGC){

View File

@ -36,9 +36,6 @@ use function strlen;
/**
* This class is used by the current MCPE protocol system to store cached chunk packets for fast resending.
*
* TODO: make MemoryManager aware of this so the cache can be destroyed when memory is low
* TODO: this needs a hook for world unloading
*/
class ChunkCache implements ChunkListener{
/** @var self[][] */
@ -69,6 +66,19 @@ class ChunkCache implements ChunkListener{
return self::$instances[$worldId][$compressorId];
}
public static function pruneCaches() : void{
foreach(self::$instances as $compressorMap){
foreach($compressorMap as $chunkCache){
foreach($chunkCache->caches as $chunkHash => $promise){
if($promise->hasResult()){
//Do not clear promises that are not yet fulfilled; they will have requesters waiting on them
unset($chunkCache->caches[$chunkHash]);
}
}
}
}
}
/** @var World */
private $world;
/** @var Compressor */