Skip to content

Commit c5f6f9f

Browse files
committed
Increase performance of getLight() by at least 2x
Leads to the following increases: getSmoothLight() approx. 40% increase getTileInfo() approx. 25% increase MapBlockMesh::MapBlockMesh() 25-30%
1 parent a1ea017 commit c5f6f9f

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/mapblock_mesh.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ static u16 getSmoothLightCombined(v3s16 p, MeshMakeData *data)
259259
light_source_max = f.light_source;
260260
// Check f.solidness because fast-style leaves look better this way
261261
if (f.param_type == CPT_LIGHT && f.solidness != 2) {
262-
light_day += decode_light(n.getLight(LIGHTBANK_DAY, ndef));
263-
light_night += decode_light(n.getLight(LIGHTBANK_NIGHT, ndef));
262+
light_day += decode_light(n.getLightNoChecks(LIGHTBANK_DAY, &f));
263+
light_night += decode_light(n.getLightNoChecks(LIGHTBANK_NIGHT, &f));
264264
light_count++;
265265
} else {
266266
ambient_occlusion++;

src/mapnode.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ u8 MapNode::getLight(enum LightBank bank, INodeDefManager *nodemgr) const
8888
return MYMAX(f.light_source, light);
8989
}
9090

91+
u8 MapNode::getLightNoChecks(enum LightBank bank, const ContentFeatures *f)
92+
{
93+
return MYMAX(f->light_source,
94+
bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f);
95+
}
96+
9197
bool MapNode::getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const
9298
{
9399
// Select the brightest of [light source, propagated light]

src/mapnode.h

+21
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ enum Rotation {
108108
#define LEVELED_MASK 0x3F
109109
#define LEVELED_MAX LEVELED_MASK
110110

111+
112+
struct ContentFeatures;
113+
111114
/*
112115
This is the stuff what the whole world consists of.
113116
*/
@@ -188,6 +191,24 @@ struct MapNode
188191

189192
void setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr);
190193
u8 getLight(enum LightBank bank, INodeDefManager *nodemgr) const;
194+
195+
/**
196+
* This function differs from getLight(enum LightBank bank, INodeDefManager *nodemgr)
197+
* in that the ContentFeatures of the node in question are not retrieved by
198+
* the function itself. Thus, if you have already called nodemgr->get() to
199+
* get the ContentFeatures you pass it to this function instead of the
200+
* function getting ContentFeatures itself. Since INodeDefManager::get()
201+
* is relatively expensive this can lead to significant performance
202+
* improvements in some situations. Call this function if (and only if)
203+
* you have already retrieved the ContentFeatures by calling
204+
* INodeDefManager::get() for the node you're working with and the
205+
* pre-conditions listed are true.
206+
*
207+
* @pre f != NULL
208+
* @pre f->param_type == CPT_LIGHT
209+
*/
210+
u8 getLightNoChecks(LightBank bank, const ContentFeatures *f);
211+
191212
bool getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const;
192213

193214
// 0 <= daylight_factor <= 1000

0 commit comments

Comments
 (0)