ChunkCache: track strings in cache directly instead of CompressBatchPromise

this reduces memory footprint slightly, but more importantly reduces GC workload.
Since it also reduces the work done on cache hit, it might *slightly* improve performance,
but any improvement is likely to be minimal.
This commit is contained in:
Dylan K. Taylor
2024-12-23 21:04:33 +00:00
parent 306623e890
commit 8a5eb71432
2 changed files with 55 additions and 32 deletions

View File

@ -115,6 +115,7 @@ use pocketmine\utils\ObjectSet;
use pocketmine\utils\TextFormat;
use pocketmine\world\format\io\GlobalItemDataHandlers;
use pocketmine\world\Position;
use pocketmine\world\World;
use pocketmine\YmlServerProperties;
use function array_map;
use function array_values;
@ -1178,6 +1179,19 @@ class NetworkSession{
$this->sendDataPacket(ClientboundCloseFormPacket::create());
}
/**
* @phpstan-param \Closure() : void $onCompletion
*/
private function sendChunkPacket(string $chunkPacket, \Closure $onCompletion, World $world) : void{
$world->timings->syncChunkSend->startTiming();
try{
$this->queueCompressed($chunkPacket);
$onCompletion();
}finally{
$world->timings->syncChunkSend->stopTiming();
}
}
/**
* Instructs the networksession to start using the chunk at the given coordinates. This may occur asynchronously.
* @param \Closure $onCompletion To be called when chunk sending has completed.
@ -1185,8 +1199,12 @@ class NetworkSession{
*/
public function startUsingChunk(int $chunkX, int $chunkZ, \Closure $onCompletion) : void{
$world = $this->player->getLocation()->getWorld();
ChunkCache::getInstance($world, $this->compressor)->request($chunkX, $chunkZ)->onResolve(
$promiseOrPacket = ChunkCache::getInstance($world, $this->compressor)->request($chunkX, $chunkZ);
if(is_string($promiseOrPacket)){
$this->sendChunkPacket($promiseOrPacket, $onCompletion, $world);
return;
}
$promiseOrPacket->onResolve(
//this callback may be called synchronously or asynchronously, depending on whether the promise is resolved yet
function(CompressBatchPromise $promise) use ($world, $onCompletion, $chunkX, $chunkZ) : void{
if(!$this->isConnected()){
@ -1204,13 +1222,7 @@ class NetworkSession{
//to NEEDED if they want to be resent.
return;
}
$world->timings->syncChunkSend->startTiming();
try{
$this->queueCompressed($promise);
$onCompletion();
}finally{
$world->timings->syncChunkSend->stopTiming();
}
$this->sendChunkPacket($promise->getResult(), $onCompletion, $world);
}
);
}