From 9987bb6be26e8acb937ece4f0ddbec29fd54db29 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Sun, 7 May 2023 22:46:47 +1000 Subject: [PATCH] Avoid unnecessary item initialisation and copy --- Source/inv.cpp | 4 ++-- Source/items.cpp | 9 ++++++--- Source/items.h | 6 +++--- Source/msg.cpp | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Source/inv.cpp b/Source/inv.cpp index 8f3a8a071..0b222f1b4 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -1765,7 +1765,7 @@ int SyncDropItem(Point position, _item_indexes idx, uint16_t icreateinfo, int is item._iAC = ac; item.dwBuff = ibuff; - return PlaceItemInWorld(item, position); + return PlaceItemInWorld(std::move(item), position); } int SyncDropEar(Point position, uint16_t icreateinfo, uint32_t iseed, uint8_t cursval, string_view heroname) @@ -1776,7 +1776,7 @@ int SyncDropEar(Point position, uint16_t icreateinfo, uint32_t iseed, uint8_t cu Item item; RecreateEar(item, icreateinfo, iseed, cursval, heroname); - return PlaceItemInWorld(item, position); + return PlaceItemInWorld(std::move(item), position); } int8_t CheckInvHLight() diff --git a/Source/items.cpp b/Source/items.cpp index 20b76d717..9bf5d230d 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -3108,13 +3108,16 @@ int AllocateItem() return inum; } -int PlaceItemInWorld(Item &&item, WorldTilePosition position) +uint8_t PlaceItemInWorld(Item &&item, WorldTilePosition position) { - int ii = AllocateItem(); + assert(ActiveItemCount < MAXITEMS); + + uint8_t ii = ActiveItems[ActiveItemCount]; + ActiveItemCount++; dItem[position.x][position.y] = ii + 1; auto &item_ = Items[ii]; - item_ = item; + item_ = std::move(item); item_.position = position; RespawnItem(item_, true); diff --git a/Source/items.h b/Source/items.h index e7a1b8b3c..00396c083 100644 --- a/Source/items.h +++ b/Source/items.h @@ -500,12 +500,12 @@ void CreatePlrItems(Player &player); bool ItemSpaceOk(Point position); int AllocateItem(); /** - * @brief Puts the item onto the floor of the current dungeon level - * @param item The source of the item data + * @brief Moves the item onto the floor of the current dungeon level + * @param item The source of the item data, should not be used after calling this function * @param position Coordinates of the tile to place the item on * @return The index assigned to the item */ -int PlaceItemInWorld(const Item &item, WorldTilePosition position); +uint8_t PlaceItemInWorld(Item &&item, WorldTilePosition position); Point GetSuperItemLoc(Point position); void GetItemAttrs(Item &item, _item_indexes itemData, int lvl); void SetupItem(Item &item); diff --git a/Source/msg.cpp b/Source/msg.cpp index f255b9398..76a117ff6 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -1315,7 +1315,7 @@ size_t OnPutItem(const TCmd *pCmd, size_t pnum) if (isSelf) { std::optional itemTile = FindAdjacentPositionForItem(player.position.tile, GetDirection(player.position.tile, position)); if (itemTile) - ii = PlaceItemInWorld(ItemLimbo, *itemTile); + ii = PlaceItemInWorld(std::move(ItemLimbo), *itemTile); else ii = -1; } else