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:
Dylan T
2018-11-17 14:46:05 +00:00
committed by GitHub
parent 554c029fbd
commit f8bfbc107d
2 changed files with 27 additions and 4 deletions

View File

@ -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();
}
}
}
}