Skip to content

Commit

Permalink
Speed up the craft definition handling (#8097)
Browse files Browse the repository at this point in the history
The craft definition handling code that collects the names of
the craftable nodes suffers from vector reallocation
performance hits, slowing down instances with lots of
crafting recipes (VanessaE's DreamBuilder and most public
server some to my mind when thinking about this). As in each
instance the size of the resulting vector is already known,
add a reserve() call before the offending loops to allocate
the needed chunk of memory within the result vector in one
go, getting rid of the overhead.
  • Loading branch information
osjc authored and nerzhul committed Jan 13, 2019
1 parent 5a00b11 commit a51909b
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/craftdef.cpp
Expand Up @@ -112,6 +112,7 @@ static std::vector<std::string> craftGetItemNames(
const std::vector<std::string> &itemstrings, IGameDef *gamedef)
{
std::vector<std::string> result;
result.reserve(itemstrings.size());
for (const auto &itemstring : itemstrings) {
result.push_back(craftGetItemName(itemstring, gamedef));
}
Expand All @@ -123,6 +124,7 @@ static std::vector<std::string> craftGetItemNames(
const std::vector<ItemStack> &items, IGameDef *gamedef)
{
std::vector<std::string> result;
result.reserve(items.size());
for (const auto &item : items) {
result.push_back(item.name);
}
Expand All @@ -134,6 +136,7 @@ static std::vector<ItemStack> craftGetItems(
const std::vector<std::string> &items, IGameDef *gamedef)
{
std::vector<ItemStack> result;
result.reserve(items.size());
for (const auto &item : items) {
result.emplace_back(std::string(item), (u16)1,
(u16)0, gamedef->getItemDefManager());
Expand Down

0 comments on commit a51909b

Please sign in to comment.