Skip to content

Commit 3e50850

Browse files
authoredJul 26, 2017
TileLayer: use shared_ptr for FrameSpec vector (#6171)
* TileLayer: use shared_ptr for vector framespec This reduce memory copy of TileLayer from (4 to 16) * FrameSpec where FrameSpec = (sizeof(int) + 3 * sizeof(ptr)) to int + sizeof(ptr) Callgrind difference Before: https://lut.im/RGkiJqQb8T/LeQIEXpAuRzfl7gd.png� After: https://lut.im/bcqmwee1xu/cTwtptY5tRuS9lp0.png * Fix one push_back to use vector::emplace_back & optimize inclusions
1 parent 9a17b65 commit 3e50850

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed
 

Diff for: ‎src/client/tile.h

+2-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 <string>
2727
#include <vector>
2828
#include <SMaterial.h>
29+
#include <memory>
2930
#include "util/numeric.h"
3031

3132
class IGameDef;
@@ -284,7 +285,7 @@ struct TileLayer
284285
//! If true, the tile has its own color.
285286
bool has_color = false;
286287

287-
std::vector<FrameSpec> frames;
288+
std::shared_ptr<std::vector<FrameSpec>> frames = nullptr;
288289

289290
/*!
290291
* The color of the tile, or if the tile does not own

Diff for: ‎src/mapblock_mesh.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818
*/
1919

2020
#include "mapblock_mesh.h"
21-
#include "light.h"
2221
#include "mapblock.h"
2322
#include "map.h"
2423
#include "profiler.h"
25-
#include "nodedef.h"
2624
#include "mesh.h"
2725
#include "minimap.h"
2826
#include "content_mapblock.h"
29-
#include "noise.h"
30-
#include "shader.h"
31-
#include "settings.h"
3227
#include "util/directiontables.h"
3328
#include "client/renderingengine.h"
3429

@@ -602,7 +597,8 @@ static void makeFastFace(const TileSpec &tile, u16 li0, u16 li1, u16 li2, u16 li
602597
if (layer->texture_id == 0)
603598
continue;
604599

605-
dest.push_back(FastFace());
600+
// equivalent to dest.push_back(FastFace()) but faster
601+
dest.emplace_back();
606602
FastFace& face = *dest.rbegin();
607603

608604
for (u8 i = 0; i < 4; i++) {
@@ -1126,7 +1122,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
11261122
m_animation_frame_offsets[std::pair<u8, u32>(layer, i)] = 0;
11271123
}
11281124
// Replace tile texture with the first animation frame
1129-
p.layer.texture = p.layer.frames[0].texture;
1125+
p.layer.texture = (*p.layer.frames)[0].texture;
11301126
}
11311127

11321128
if (!m_enable_shaders) {
@@ -1314,7 +1310,7 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
13141310
scene::IMeshBuffer *buf = m_mesh[i->first.first]->
13151311
getMeshBuffer(i->first.second);
13161312

1317-
const FrameSpec &animation_frame = tile.frames[frame];
1313+
const FrameSpec &animation_frame = (*tile.frames)[frame];
13181314
buf->getMaterial().setTexture(0, animation_frame.texture);
13191315
if (m_enable_shaders) {
13201316
if (animation_frame.normal_texture) {

Diff for: ‎src/nodedef.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,10 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileLayer *tile,
637637
tile->material_flags &= ~MATERIAL_FLAG_ANIMATION;
638638
} else {
639639
std::ostringstream os(std::ios::binary);
640-
tile->frames.resize(frame_count);
640+
if (!tile->frames) {
641+
tile->frames = std::make_shared<std::vector<FrameSpec>>();
642+
}
643+
tile->frames->resize(frame_count);
641644

642645
for (int i = 0; i < frame_count; i++) {
643646

@@ -652,7 +655,7 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileLayer *tile,
652655
if (tile->normal_texture)
653656
frame.normal_texture = tsrc->getNormalTexture(os.str());
654657
frame.flags_texture = tile->flags_texture;
655-
tile->frames[i] = frame;
658+
(*tile->frames)[i] = frame;
656659
}
657660
}
658661
}

Diff for: ‎src/particles.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef,
631631

632632
// Only use first frame of animated texture
633633
if (tile.material_flags & MATERIAL_FLAG_ANIMATION)
634-
texture = tile.frames[0].texture;
634+
texture = (*tile.frames)[0].texture;
635635
else
636636
texture = tile.texture;
637637

Diff for: ‎src/wieldmesh.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,15 @@ void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
593593
material.MaterialType = *mattype;
594594
}
595595
if (layer->animation_frame_count > 1) {
596-
FrameSpec animation_frame = layer->frames[0];
596+
const FrameSpec &animation_frame = (*layer->frames)[0];
597597
material.setTexture(0, animation_frame.texture);
598598
} else {
599599
material.setTexture(0, layer->texture);
600600
}
601601
if (use_shaders) {
602602
if (layer->normal_texture) {
603603
if (layer->animation_frame_count > 1) {
604-
FrameSpec animation_frame = layer->frames[0];
604+
const FrameSpec &animation_frame = (*layer->frames)[0];
605605
material.setTexture(1, animation_frame.normal_texture);
606606
} else
607607
material.setTexture(1, layer->normal_texture);

0 commit comments

Comments
 (0)