Do not cache ItemStacks for every item

this is very memory inefficient, and only provides a performance advantage in cold code anyway.
This commit is contained in:
Dylan K. Taylor 2023-03-20 23:18:43 +00:00
parent 1992d3b6db
commit 63310cf764
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 14 additions and 21 deletions

View File

@ -413,7 +413,7 @@ class InventoryManager{
if($clientSideItem === null || !$clientSideItem->equals($currentItem)){
//no prediction or incorrect - do not associate this with the currently active itemstack request
$this->trackItemStack($inventoryEntry, $slot, $currentItem, null);
$inventoryEntry->pendingSyncs[$slot] = $slot;
$inventoryEntry->pendingSyncs[$slot] = $currentItem;
}else{
//correctly predicted - associate the change with the currently active itemstack request
$this->trackItemStack($inventoryEntry, $slot, $currentItem, $this->currentItemStackRequestId);
@ -422,7 +422,7 @@ class InventoryManager{
unset($inventoryEntry->predictions[$slot]);
}
public function syncSlot(Inventory $inventory, int $slot) : void{
public function syncSlot(Inventory $inventory, int $slot, ItemStack $itemStack) : void{
$entry = $this->inventories[spl_object_id($inventory)] ?? null;
if($entry === null){
throw new \LogicException("Cannot sync an untracked inventory");
@ -439,7 +439,7 @@ class InventoryManager{
$netSlot = $slot;
}
$itemStackWrapper = new ItemStackWrapper($itemStackInfo->getStackId(), $itemStackInfo->getItemStack());
$itemStackWrapper = new ItemStackWrapper($itemStackInfo->getStackId(), $itemStack);
if($windowId === ContainerIds::OFFHAND){
//TODO: HACK!
//The client may sometimes ignore the InventorySlotPacket for the offhand slot.
@ -482,7 +482,7 @@ class InventoryManager{
foreach($inventory->getContents(true) as $slot => $item){
$itemStack = TypeConverter::getInstance()->coreItemStackToNet($item);
$info = $this->trackItemStack($entry, $slot, $itemStack, null);
$contents[] = new ItemStackWrapper($info->getStackId(), $info->getItemStack());
$contents[] = new ItemStackWrapper($info->getStackId(), $itemStack);
}
if($entry->complexSlotMap !== null){
foreach($contents as $slotId => $info){
@ -513,6 +513,7 @@ class InventoryManager{
}
public function syncMismatchedPredictedSlotChanges() : void{
$typeConverter = TypeConverter::getInstance();
foreach($this->inventories as $entry){
$inventory = $entry->inventory;
foreach($entry->predictions as $slot => $expectedItem){
@ -522,7 +523,7 @@ class InventoryManager{
//any prediction that still exists at this point is a slot that was predicted to change but didn't
$this->session->getLogger()->debug("Detected prediction mismatch in inventory " . get_class($inventory) . "#" . spl_object_id($inventory) . " slot $slot");
$entry->pendingSyncs[$slot] = $slot;
$entry->pendingSyncs[$slot] = $typeConverter->coreItemStackToNet($inventory->getItem($slot));
}
$entry->predictions = [];
@ -541,8 +542,8 @@ class InventoryManager{
}
$inventory = $entry->inventory;
$this->session->getLogger()->debug("Syncing slots " . implode(", ", array_keys($entry->pendingSyncs)) . " in inventory " . get_class($inventory) . "#" . spl_object_id($inventory));
foreach($entry->pendingSyncs as $slot){
$this->syncSlot($inventory, $slot);
foreach($entry->pendingSyncs as $slot => $itemStack){
$this->syncSlot($inventory, $slot, $itemStack);
}
$entry->pendingSyncs = [];
}
@ -571,7 +572,7 @@ class InventoryManager{
$this->session->sendDataPacket(MobEquipmentPacket::create(
$this->player->getId(),
new ItemStackWrapper($itemStackInfo->getStackId(), $itemStackInfo->getItemStack()),
new ItemStackWrapper($itemStackInfo->getStackId(), TypeConverter::getInstance()->coreItemStackToNet($playerInventory->getItemInHand())),
$selected,
$selected,
ContainerIds::INVENTORY
@ -604,7 +605,7 @@ class InventoryManager{
private function trackItemStack(InventoryManagerEntry $entry, int $slotId, ItemStack $itemStack, ?int $itemStackRequestId) : ItemStackInfo{
//TODO: ItemStack->isNull() would be nice to have here
$info = new ItemStackInfo($itemStackRequestId, $itemStack->getId() === 0 ? 0 : $this->newItemStackId(), $itemStack);
$info = new ItemStackInfo($itemStackRequestId, $itemStack->getId() === 0 ? 0 : $this->newItemStackId());
return $entry->itemStackInfos[$slotId] = $info;
}
}

View File

@ -41,7 +41,7 @@ final class InventoryManagerEntry{
/**
* @var int[]
* @phpstan-var array<int, int>
* @phpstan-var array<int, ItemStack>
*/
public array $pendingSyncs = [];

View File

@ -23,19 +23,14 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe;
use pocketmine\network\mcpe\protocol\types\inventory\ItemStack;
final class ItemStackInfo{
public function __construct(
private ?int $requestId,
private int $stackId,
private ItemStack $itemStack
private int $stackId
){}
public function getRequestId() : ?int{ return $this->requestId; }
public function getStackId() : int{ return $this->stackId; }
public function getItemStack() : ItemStack{ return $this->itemStack; }
}

View File

@ -394,14 +394,11 @@ class InGamePacketHandler extends PacketHandler{
//it's technically possible to see this more than once, but a normal client should never do that.
$inventory = $this->player->getInventory();
$heldItemStack = $this->inventoryManager->getItemStackInfo($inventory, $inventory->getHeldItemIndex())?->getItemStack();
if($heldItemStack === null){
throw new AssumptionFailedError("Missing itemstack info for held item");
}
$heldItemStack = TypeConverter::getInstance()->coreItemStackToNet($inventory->getItemInHand());
$droppedItemStack = $networkInventoryAction->newItem->getItemStack();
//because the client doesn't tell us the expected itemstack ID, we have to deep-compare our known
//itemstack info with the one the client sent. This is costly, but we don't have any other option :(
if(!$heldItemStack->equalsWithoutCount($droppedItemStack) || $heldItemStack->getCount() < $droppedItemStack->getCount()){
if($heldItemStack->getCount() < $droppedItemStack->getCount() || !$heldItemStack->equalsWithoutCount($droppedItemStack)){
return false;
}