Skip to content

Commit 77597c4

Browse files
committedMay 6, 2017
Clean up numeric.h and split FacePositionCache from it
I also optiized FacePositionCache a bit: I removed a map lookup and vector copy from both branches of getFacePosition.
1 parent b6f4a9c commit 77597c4

17 files changed

+205
-157
lines changed
 

Diff for: ‎build/android/jni/Android.mk

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ LOCAL_SRC_FILES := \
143143
jni/src/dungeongen.cpp \
144144
jni/src/emerge.cpp \
145145
jni/src/environment.cpp \
146+
jni/src/face_position_cache.cpp \
146147
jni/src/filecache.cpp \
147148
jni/src/filesys.cpp \
148149
jni/src/fontengine.cpp \

Diff for: ‎src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ set(common_SRCS
388388
dungeongen.cpp
389389
emerge.cpp
390390
environment.cpp
391+
face_position_cache.cpp
391392
filesys.cpp
392393
genericobject.cpp
393394
gettext.cpp

Diff for: ‎src/clientenvironment.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
#include "raycast.h"
3131
#include "voxelalgorithms.h"
3232
#include "settings.h"
33+
#include <algorithm>
3334

3435
/*
3536
ClientEnvironment

Diff for: ‎src/clientiface.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include <sstream>
2121

2222
#include "clientiface.h"
23-
#include "util/numeric.h"
2423
#include "remoteplayer.h"
2524
#include "settings.h"
2625
#include "mapblock.h"
@@ -32,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3231
#include "log.h"
3332
#include "network/serveropcodes.h"
3433
#include "util/srp.h"
34+
#include "face_position_cache.h"
3535

3636
const char *ClientInterface::statenames[] = {
3737
"Invalid",

Diff for: ‎src/content_cao.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4444
#include "camera.h" // CameraModes
4545
#include "wieldmesh.h"
4646
#include "log.h"
47+
#include <algorithm>
4748

4849
class Settings;
4950
struct ToolCapabilities;

Diff for: ‎src/face_position_cache.cpp

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2015 Nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#include "face_position_cache.h"
21+
#include "threading/mutex_auto_lock.h"
22+
23+
24+
UNORDERED_MAP<u16, std::vector<v3s16> > FacePositionCache::cache;
25+
Mutex FacePositionCache::cache_mutex;
26+
27+
// Calculate the borders of a "d-radius" cube
28+
const std::vector<v3s16> &FacePositionCache::getFacePositions(u16 d)
29+
{
30+
MutexAutoLock lock(cache_mutex);
31+
UNORDERED_MAP<u16, std::vector<v3s16> >::iterator it = cache.find(d);
32+
if (it != cache.end())
33+
return it->second;
34+
35+
return generateFacePosition(d);
36+
}
37+
38+
const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
39+
{
40+
cache[d] = std::vector<v3s16>();
41+
std::vector<v3s16> &c = cache[d];
42+
if (d == 0) {
43+
c.push_back(v3s16(0,0,0));
44+
return c;
45+
}
46+
if (d == 1) {
47+
// This is an optimized sequence of coordinates.
48+
c.push_back(v3s16( 0, 1, 0)); // Top
49+
c.push_back(v3s16( 0, 0, 1)); // Back
50+
c.push_back(v3s16(-1, 0, 0)); // Left
51+
c.push_back(v3s16( 1, 0, 0)); // Right
52+
c.push_back(v3s16( 0, 0,-1)); // Front
53+
c.push_back(v3s16( 0,-1, 0)); // Bottom
54+
// 6
55+
c.push_back(v3s16(-1, 0, 1)); // Back left
56+
c.push_back(v3s16( 1, 0, 1)); // Back right
57+
c.push_back(v3s16(-1, 0,-1)); // Front left
58+
c.push_back(v3s16( 1, 0,-1)); // Front right
59+
c.push_back(v3s16(-1,-1, 0)); // Bottom left
60+
c.push_back(v3s16( 1,-1, 0)); // Bottom right
61+
c.push_back(v3s16( 0,-1, 1)); // Bottom back
62+
c.push_back(v3s16( 0,-1,-1)); // Bottom front
63+
c.push_back(v3s16(-1, 1, 0)); // Top left
64+
c.push_back(v3s16( 1, 1, 0)); // Top right
65+
c.push_back(v3s16( 0, 1, 1)); // Top back
66+
c.push_back(v3s16( 0, 1,-1)); // Top front
67+
// 18
68+
c.push_back(v3s16(-1, 1, 1)); // Top back-left
69+
c.push_back(v3s16( 1, 1, 1)); // Top back-right
70+
c.push_back(v3s16(-1, 1,-1)); // Top front-left
71+
c.push_back(v3s16( 1, 1,-1)); // Top front-right
72+
c.push_back(v3s16(-1,-1, 1)); // Bottom back-left
73+
c.push_back(v3s16( 1,-1, 1)); // Bottom back-right
74+
c.push_back(v3s16(-1,-1,-1)); // Bottom front-left
75+
c.push_back(v3s16( 1,-1,-1)); // Bottom front-right
76+
// 26
77+
return c;
78+
}
79+
80+
// Take blocks in all sides, starting from y=0 and going +-y
81+
for (s16 y = 0; y <= d - 1; y++) {
82+
// Left and right side, including borders
83+
for (s16 z =- d; z <= d; z++) {
84+
c.push_back(v3s16( d, y, z));
85+
c.push_back(v3s16(-d, y, z));
86+
if (y != 0) {
87+
c.push_back(v3s16( d, -y, z));
88+
c.push_back(v3s16(-d, -y, z));
89+
}
90+
}
91+
// Back and front side, excluding borders
92+
for (s16 x = -d + 1; x <= d - 1; x++) {
93+
c.push_back(v3s16(x, y, d));
94+
c.push_back(v3s16(x, y, -d));
95+
if (y != 0) {
96+
c.push_back(v3s16(x, -y, d));
97+
c.push_back(v3s16(x, -y, -d));
98+
}
99+
}
100+
}
101+
102+
// Take the bottom and top face with borders
103+
// -d < x < d, y = +-d, -d < z < d
104+
for (s16 x = -d; x <= d; x++)
105+
for (s16 z = -d; z <= d; z++) {
106+
c.push_back(v3s16(x, -d, z));
107+
c.push_back(v3s16(x, d, z));
108+
}
109+
return c;
110+
}

Diff for: ‎src/face_position_cache.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2015 Nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#ifndef FACE_POSITION_CACHE_HEADER
21+
#define FACE_POSITION_CACHE_HEADER
22+
23+
#include "irr_v3d.h"
24+
#include "threading/mutex.h"
25+
#include "util/cpp11_container.h"
26+
27+
#include <map>
28+
#include <vector>
29+
30+
/*
31+
* This class permits caching getFacePosition call results.
32+
* This reduces CPU usage and vector calls.
33+
*/
34+
class FacePositionCache {
35+
public:
36+
static const std::vector<v3s16> &getFacePositions(u16 d);
37+
38+
private:
39+
static const std::vector<v3s16> &generateFacePosition(u16 d);
40+
static UNORDERED_MAP<u16, std::vector<v3s16> > cache;
41+
static Mutex cache_mutex;
42+
};
43+
44+
#endif

Diff for: ‎src/mg_decoration.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "map.h"
2525
#include "log.h"
2626
#include "util/numeric.h"
27+
#include <algorithm>
28+
2729

2830
FlagDesc flagdesc_deco[] = {
2931
{"place_center_x", DECO_PLACE_CENTER_X},

Diff for: ‎src/mg_ore.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "mg_ore.h"
2121
#include "mapgen.h"
2222
#include "noise.h"
23-
#include "util/numeric.h"
2423
#include "map.h"
2524
#include "log.h"
25+
#include <algorithm>
26+
2627

2728
FlagDesc flagdesc_ore[] = {
2829
{"absheight", OREFLAG_ABSHEIGHT},

Diff for: ‎src/script/lua_api/l_env.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3535
#include "treegen.h"
3636
#include "emerge.h"
3737
#include "pathfinder.h"
38+
#include "face_position_cache.h"
3839

3940
struct EnumString ModApiEnvMod::es_ClearObjectsMode[] =
4041
{

Diff for: ‎src/script/lua_api/l_server.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include "environment.h"
2727
#include "player.h"
2828
#include "log.h"
29+
#include <algorithm>
2930

3031
// request_shutdown()
3132
int ModApiServer::l_request_shutdown(lua_State *L)

Diff for: ‎src/unittest/test_noderesolver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "gamedef.h"
2525
#include "nodedef.h"
2626

27+
#include <algorithm>
28+
2729

2830
class TestNodeResolver : public TestBase {
2931
public:

Diff for: ‎src/util/basic_macros.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#ifndef BASICMACROS_HEADER
2121
#define BASICMACROS_HEADER
2222

23-
#include <algorithm>
24-
2523
#define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
2624

2725
#define MYMIN(a, b) ((a) < (b) ? (a) : (b))
2826

2927
#define MYMAX(a, b) ((a) > (b) ? (a) : (b))
3028

29+
// Requires <algorithm>
3130
#define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end())
3231

3332
// To disable copy constructors and assignment operations for some class

Diff for: ‎src/util/numeric.cpp

+1-92
Original file line numberDiff line numberDiff line change
@@ -24,100 +24,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "../noise.h" // PseudoRandom, PcgRandom
2525
#include "../threading/mutex_auto_lock.h"
2626
#include <string.h>
27-
#include <iostream>
2827

29-
UNORDERED_MAP<u16, std::vector<v3s16> > FacePositionCache::m_cache;
30-
Mutex FacePositionCache::m_cache_mutex;
31-
// Calculate the borders of a "d-radius" cube
32-
// TODO: Make it work without mutex and data races, probably thread-local
33-
std::vector<v3s16> FacePositionCache::getFacePositions(u16 d)
34-
{
35-
MutexAutoLock cachelock(m_cache_mutex);
36-
if (m_cache.find(d) != m_cache.end())
37-
return m_cache[d];
38-
39-
generateFacePosition(d);
40-
return m_cache[d];
41-
42-
}
43-
44-
void FacePositionCache::generateFacePosition(u16 d)
45-
{
46-
m_cache[d] = std::vector<v3s16>();
47-
if(d == 0) {
48-
m_cache[d].push_back(v3s16(0,0,0));
49-
return;
50-
}
51-
if(d == 1) {
52-
/*
53-
This is an optimized sequence of coordinates.
54-
*/
55-
m_cache[d].push_back(v3s16( 0, 1, 0)); // top
56-
m_cache[d].push_back(v3s16( 0, 0, 1)); // back
57-
m_cache[d].push_back(v3s16(-1, 0, 0)); // left
58-
m_cache[d].push_back(v3s16( 1, 0, 0)); // right
59-
m_cache[d].push_back(v3s16( 0, 0,-1)); // front
60-
m_cache[d].push_back(v3s16( 0,-1, 0)); // bottom
61-
// 6
62-
m_cache[d].push_back(v3s16(-1, 0, 1)); // back left
63-
m_cache[d].push_back(v3s16( 1, 0, 1)); // back right
64-
m_cache[d].push_back(v3s16(-1, 0,-1)); // front left
65-
m_cache[d].push_back(v3s16( 1, 0,-1)); // front right
66-
m_cache[d].push_back(v3s16(-1,-1, 0)); // bottom left
67-
m_cache[d].push_back(v3s16( 1,-1, 0)); // bottom right
68-
m_cache[d].push_back(v3s16( 0,-1, 1)); // bottom back
69-
m_cache[d].push_back(v3s16( 0,-1,-1)); // bottom front
70-
m_cache[d].push_back(v3s16(-1, 1, 0)); // top left
71-
m_cache[d].push_back(v3s16( 1, 1, 0)); // top right
72-
m_cache[d].push_back(v3s16( 0, 1, 1)); // top back
73-
m_cache[d].push_back(v3s16( 0, 1,-1)); // top front
74-
// 18
75-
m_cache[d].push_back(v3s16(-1, 1, 1)); // top back-left
76-
m_cache[d].push_back(v3s16( 1, 1, 1)); // top back-right
77-
m_cache[d].push_back(v3s16(-1, 1,-1)); // top front-left
78-
m_cache[d].push_back(v3s16( 1, 1,-1)); // top front-right
79-
m_cache[d].push_back(v3s16(-1,-1, 1)); // bottom back-left
80-
m_cache[d].push_back(v3s16( 1,-1, 1)); // bottom back-right
81-
m_cache[d].push_back(v3s16(-1,-1,-1)); // bottom front-left
82-
m_cache[d].push_back(v3s16( 1,-1,-1)); // bottom front-right
83-
// 26
84-
return;
85-
}
8628

87-
// Take blocks in all sides, starting from y=0 and going +-y
88-
for(s16 y=0; y<=d-1; y++) {
89-
// Left and right side, including borders
90-
for(s16 z=-d; z<=d; z++) {
91-
m_cache[d].push_back(v3s16(d,y,z));
92-
m_cache[d].push_back(v3s16(-d,y,z));
93-
if(y != 0) {
94-
m_cache[d].push_back(v3s16(d,-y,z));
95-
m_cache[d].push_back(v3s16(-d,-y,z));
96-
}
97-
}
98-
// Back and front side, excluding borders
99-
for(s16 x=-d+1; x<=d-1; x++) {
100-
m_cache[d].push_back(v3s16(x,y,d));
101-
m_cache[d].push_back(v3s16(x,y,-d));
102-
if(y != 0) {
103-
m_cache[d].push_back(v3s16(x,-y,d));
104-
m_cache[d].push_back(v3s16(x,-y,-d));
105-
}
106-
}
107-
}
108-
109-
// Take the bottom and top face with borders
110-
// -d<x<d, y=+-d, -d<z<d
111-
for(s16 x=-d; x<=d; x++)
112-
for(s16 z=-d; z<=d; z++) {
113-
m_cache[d].push_back(v3s16(x,-d,z));
114-
m_cache[d].push_back(v3s16(x,d,z));
115-
}
116-
}
117-
118-
/*
119-
myrand
120-
*/
29+
// myrand
12130

12231
PcgRandom g_pcgrand;
12332

0 commit comments

Comments
 (0)
Please sign in to comment.