Skip to content

Commit 392e80e

Browse files
authoredApr 4, 2018
Huge LBM lookup performance improvement on mapblock loading (#7195)
* Huge LBM lookup performance improvement on mapblock loading
1 parent 5070ca2 commit 392e80e

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed
 

Diff for: ‎src/serverenvironment.cpp

+20-11
Original file line numberDiff line numberDiff line change
@@ -254,23 +254,32 @@ void LBMManager::applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp)
254254
MapNode n;
255255
content_t c;
256256
lbm_lookup_map::const_iterator it = getLBMsIntroducedAfter(stamp);
257-
for (pos.X = 0; pos.X < MAP_BLOCKSIZE; pos.X++)
258-
for (pos.Y = 0; pos.Y < MAP_BLOCKSIZE; pos.Y++)
259-
for (pos.Z = 0; pos.Z < MAP_BLOCKSIZE; pos.Z++)
260-
{
261-
n = block->getNodeNoEx(pos);
262-
c = n.getContent();
263-
for (LBMManager::lbm_lookup_map::const_iterator iit = it;
264-
iit != m_lbm_lookup.end(); ++iit) {
265-
const std::vector<LoadingBlockModifierDef *> *lbm_list =
266-
iit->second.lookup(c);
257+
for (; it != m_lbm_lookup.end(); ++it) {
258+
// Cache previous version to speedup lookup which has a very high performance
259+
// penalty on each call
260+
content_t previous_c{};
261+
std::vector<LoadingBlockModifierDef *> *lbm_list = nullptr;
262+
263+
for (pos.X = 0; pos.X < MAP_BLOCKSIZE; pos.X++)
264+
for (pos.Y = 0; pos.Y < MAP_BLOCKSIZE; pos.Y++)
265+
for (pos.Z = 0; pos.Z < MAP_BLOCKSIZE; pos.Z++) {
266+
n = block->getNodeNoEx(pos);
267+
c = n.getContent();
268+
269+
// If content_t are not matching perform an LBM lookup
270+
if (previous_c != c) {
271+
lbm_list = (std::vector<LoadingBlockModifierDef *> *)
272+
it->second.lookup(c);
273+
previous_c = c;
274+
}
275+
267276
if (!lbm_list)
268277
continue;
269278
for (auto lbmdef : *lbm_list) {
270279
lbmdef->trigger(env, pos + pos_of_block, n);
271280
}
272281
}
273-
}
282+
}
274283
}
275284

276285
/*

0 commit comments

Comments
 (0)
Please sign in to comment.