Skip to content

Commit

Permalink
Precalculate mapblock relative size. This permit to remove many s16 c…
Browse files Browse the repository at this point in the history
…alculs on runtime
  • Loading branch information
nerzhul committed Jul 29, 2015
1 parent 88a6b9f commit ca63f7f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/mapblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static const char *modified_reason_strings[] = {
MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
m_parent(parent),
m_pos(pos),
m_pos_relative(pos * MAP_BLOCKSIZE),
m_gamedef(gamedef),
m_modified(MOD_STATE_WRITE_NEEDED),
m_modified_reason(MOD_REASON_INITIAL),
Expand Down
10 changes: 9 additions & 1 deletion src/mapblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class MapBlock /*: public NodeContainer*/

inline v3s16 getPosRelative()
{
return m_pos * MAP_BLOCKSIZE;
return m_pos_relative;
}

inline core::aabbox3d<s16> getBox()
Expand Down Expand Up @@ -564,6 +564,14 @@ class MapBlock /*: public NodeContainer*/
// Position in blocks on parent
v3s16 m_pos;

/* This is the precalculated m_pos_relative value
* This caches the value, improving performance by removing 3 s16 multiplications
* at runtime on each getPosRelative call
* For a 5 minutes runtime with valgrind this removes 3 * 19M s16 multiplications
* The gain can be estimated in Release Build to 3 * 100M multiply operations for 5 mins
*/
v3s16 m_pos_relative;

IGameDef *m_gamedef;

/*
Expand Down

0 comments on commit ca63f7f

Please sign in to comment.