mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 18:32:55 +00:00
Reduce chunk memory usage by 20-60% by exploiting PHP copy-on-write behaviour (#2527)
This takes advantage of two key behaviours of PHP: 1. Assigning a string does not copy the string 2. Changing an offset in a string causes the string to be copied. These two factors combined, along with the fact that blocklight and skylight arrays are usually all-zeros, allow us to produce a significant memory usage reduction of loaded chunks. A freshly generated PM world with 3,332 chunks loaded drops from 310MB to 200MB memory usage with these changes applied.
This commit is contained in:
@ -840,8 +840,12 @@ class Chunk{
|
||||
*/
|
||||
public function collectGarbage() : void{
|
||||
foreach($this->subChunks as $y => $subChunk){
|
||||
if(!($subChunk instanceof EmptySubChunk) and $subChunk->isEmpty()){ //normal subchunk full of air, remove it and replace it with an empty stub
|
||||
$this->subChunks[$y] = $this->emptySubChunk;
|
||||
if($subChunk instanceof SubChunk){
|
||||
if($subChunk->isEmpty()){
|
||||
$this->subChunks[$y] = $this->emptySubChunk;
|
||||
}else{
|
||||
$subChunk->collectGarbage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user