Skip to content

Commit a51909b

Browse files
osjcnerzhul
authored andcommittedJan 13, 2019
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.
1 parent 5a00b11 commit a51909b

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed
 

Diff for: ‎src/craftdef.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static std::vector<std::string> craftGetItemNames(
112112
const std::vector<std::string> &itemstrings, IGameDef *gamedef)
113113
{
114114
std::vector<std::string> result;
115+
result.reserve(itemstrings.size());
115116
for (const auto &itemstring : itemstrings) {
116117
result.push_back(craftGetItemName(itemstring, gamedef));
117118
}
@@ -123,6 +124,7 @@ static std::vector<std::string> craftGetItemNames(
123124
const std::vector<ItemStack> &items, IGameDef *gamedef)
124125
{
125126
std::vector<std::string> result;
127+
result.reserve(items.size());
126128
for (const auto &item : items) {
127129
result.push_back(item.name);
128130
}
@@ -134,6 +136,7 @@ static std::vector<ItemStack> craftGetItems(
134136
const std::vector<std::string> &items, IGameDef *gamedef)
135137
{
136138
std::vector<ItemStack> result;
139+
result.reserve(items.size());
137140
for (const auto &item : items) {
138141
result.emplace_back(std::string(item), (u16)1,
139142
(u16)0, gamedef->getItemDefManager());

0 commit comments

Comments
 (0)