mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-20 02:14:01 +00:00
BlockStateDictionary: reduce memory footprint of id/meta -> state ID lookup by >60%
the aim of the game here is to avoid allocating lots of tiny arrays, which have a terrible overhead:useful-data ratio. This reduces the footprint of the mapping from 1.6 MB to 600 KB.
This commit is contained in:
parent
09e823e304
commit
f2c6a75145
@ -51,7 +51,7 @@ final class BlockStateDictionary{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int[][]|null
|
* @var int[][]|null
|
||||||
* @phpstan-var array<int, array<string, int>>|null
|
* @phpstan-var array<string, array<int, int>|int>|null
|
||||||
*/
|
*/
|
||||||
private ?array $idMetaToStateIdLookupCache = null;
|
private ?array $idMetaToStateIdLookupCache = null;
|
||||||
|
|
||||||
@ -80,15 +80,25 @@ final class BlockStateDictionary{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int[][]
|
* @return int[][]
|
||||||
* @phpstan-return array<int, array<string, int>>
|
* @phpstan-return array<string, array<int, int>|int>
|
||||||
*/
|
*/
|
||||||
private function getIdMetaToStateIdLookup() : array{
|
private function getIdMetaToStateIdLookup() : array{
|
||||||
if($this->idMetaToStateIdLookupCache === null){
|
if($this->idMetaToStateIdLookupCache === null){
|
||||||
|
$table = [];
|
||||||
//TODO: if we ever allow mutating the dictionary, this would need to be rebuilt on modification
|
//TODO: if we ever allow mutating the dictionary, this would need to be rebuilt on modification
|
||||||
$this->idMetaToStateIdLookupCache = [];
|
|
||||||
|
|
||||||
foreach($this->states as $i => $state){
|
foreach($this->states as $i => $state){
|
||||||
$this->idMetaToStateIdLookupCache[$state->getMeta()][$state->getStateName()] = $i;
|
$table[$state->getStateName()][$state->getMeta()] = $i;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->idMetaToStateIdLookupCache = [];
|
||||||
|
foreach(Utils::stringifyKeys($table) as $name => $metaToStateId){
|
||||||
|
//if only one meta value exists
|
||||||
|
if(count($metaToStateId) === 1){
|
||||||
|
$this->idMetaToStateIdLookupCache[$name] = $metaToStateId[array_key_first($metaToStateId)];
|
||||||
|
}else{
|
||||||
|
$this->idMetaToStateIdLookupCache[$name] = $metaToStateId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +137,12 @@ final class BlockStateDictionary{
|
|||||||
* This is used for deserializing crafting recipe inputs.
|
* This is used for deserializing crafting recipe inputs.
|
||||||
*/
|
*/
|
||||||
public function lookupStateIdFromIdMeta(string $id, int $meta) : ?int{
|
public function lookupStateIdFromIdMeta(string $id, int $meta) : ?int{
|
||||||
return $this->getIdMetaToStateIdLookup()[$meta][$id] ?? null;
|
$metas = $this->getIdMetaToStateIdLookup()[$id] ?? null;
|
||||||
|
return match(true){
|
||||||
|
$metas === null => null,
|
||||||
|
is_int($metas) => $metas,
|
||||||
|
is_array($metas) => $metas[$meta] ?? null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user