Speed up the craft definition handling (#8097)

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.
This commit is contained in:
Jozef Behran 2019-01-13 09:11:47 -05:00 committed by Герхард PICCORO Lenz McKAY
parent c1b07acf4a
commit 8507e651eb

View File

@ -112,9 +112,9 @@ static std::vector<std::string> craftGetItemNames(
const std::vector<std::string> &itemstrings, IGameDef *gamedef)
{
std::vector<std::string> result;
for (std::vector<std::string>::size_type i = 0;
i < itemstrings.size(); i++) {
result.push_back(craftGetItemName(itemstrings[i], gamedef));
result.reserve(itemstrings.size());
for (const auto &itemstring : itemstrings) {
result.push_back(craftGetItemName(itemstring, gamedef));
}
return result;
}
@ -124,9 +124,9 @@ static std::vector<std::string> craftGetItemNames(
const std::vector<ItemStack> &items, IGameDef *gamedef)
{
std::vector<std::string> result;
for (std::vector<ItemStack>::size_type i = 0;
i < items.size(); i++) {
result.push_back(items[i].name);
result.reserve(items.size());
for (const auto &item : items) {
result.push_back(item.name);
}
return result;
}
@ -136,10 +136,10 @@ static std::vector<ItemStack> craftGetItems(
const std::vector<std::string> &items, IGameDef *gamedef)
{
std::vector<ItemStack> result;
for (std::vector<std::string>::size_type i = 0;
i < items.size(); i++) {
result.push_back(ItemStack(std::string(items[i]), (u16)1,
(u16)0, gamedef->getItemDefManager()));
result.reserve(items.size());
for (const auto &item : items) {
result.emplace_back(std::string(item), (u16)1,
(u16)0, gamedef->getItemDefManager());
}
return result;
}
@ -935,7 +935,7 @@ public:
// Get output, then decrement input (if requested)
output = out;
if (decrementInput)
def->decrementInput(input, output_replacement, gamedef);
/*errorstream << "Check RETURNS TRUE" << std::endl;*/