Skip to content

Commit f0bad0e

Browse files
authored
Reserve vectors before pushing and other code quality changes (#11161)
1 parent 3e1904f commit f0bad0e

20 files changed

+106
-108
lines changed

src/client/clientmap.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,12 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
499499
static v3f z_directions[50] = {
500500
v3f(-100, 0, 0)
501501
};
502-
static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
502+
static f32 z_offsets[50] = {
503503
-1000,
504504
};
505505

506-
if(z_directions[0].X < -99){
507-
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
506+
if (z_directions[0].X < -99) {
507+
for (u32 i = 0; i < ARRLEN(z_directions); i++) {
508508
// Assumes FOV of 72 and 16/9 aspect ratio
509509
z_directions[i] = v3f(
510510
0.02 * myrand_range(-100, 100),
@@ -520,7 +520,8 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
520520
if(sunlight_min_d > 35*BS)
521521
sunlight_min_d = 35*BS;
522522
std::vector<int> values;
523-
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
523+
values.reserve(ARRLEN(z_directions));
524+
for (u32 i = 0; i < ARRLEN(z_directions); i++) {
524525
v3f z_dir = z_directions[i];
525526
core::CMatrix4<f32> a;
526527
a.buildRotateFromTo(v3f(0,1,0), z_dir);

src/client/clouds.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void Clouds::render()
170170

171171
// Read noise
172172

173-
std::vector<char> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2); // vector<bool> is broken
173+
std::vector<bool> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2);
174174
std::vector<video::S3DVertex> vertices;
175175
vertices.reserve(16 * m_cloud_radius_i * m_cloud_radius_i);
176176

src/client/hud.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,22 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
336336
irr::gui::IGUIFont* font = g_fontengine->getFont();
337337

338338
// Reorder elements by z_index
339-
std::vector<size_t> ids;
339+
std::vector<HudElement*> elems;
340+
elems.reserve(player->maxHudId());
340341

341342
for (size_t i = 0; i != player->maxHudId(); i++) {
342343
HudElement *e = player->getHud(i);
343344
if (!e)
344345
continue;
345346

346-
auto it = ids.begin();
347-
while (it != ids.end() && player->getHud(*it)->z_index <= e->z_index)
347+
auto it = elems.begin();
348+
while (it != elems.end() && (*it)->z_index <= e->z_index)
348349
++it;
349350

350-
ids.insert(it, i);
351+
elems.insert(it, e);
351352
}
352353

353-
for (size_t i : ids) {
354-
HudElement *e = player->getHud(i);
354+
for (HudElement *e : elems) {
355355

356356
v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
357357
floor(e->pos.Y * (float) m_screensize.Y + 0.5));
@@ -522,8 +522,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
522522
client->getMinimap()->drawMinimap(rect);
523523
break; }
524524
default:
525-
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type <<
526-
" of hud element ID " << i << " due to unrecognized type" << std::endl;
525+
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type
526+
<< " due to unrecognized type" << std::endl;
527527
}
528528
}
529529
}

src/client/sky.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
8282
// Ensures that sun and moon textures and tonemaps are correct.
8383
setSkyDefaults();
8484
m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
85-
tsrc->getTextureForMesh(m_sun_params.texture) : NULL;
85+
tsrc->getTextureForMesh(m_sun_params.texture) : nullptr;
8686
m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
87-
tsrc->getTextureForMesh(m_moon_params.texture) : NULL;
87+
tsrc->getTextureForMesh(m_moon_params.texture) : nullptr;
8888
m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
89-
tsrc->getTexture(m_sun_params.tonemap) : NULL;
89+
tsrc->getTexture(m_sun_params.tonemap) : nullptr;
9090
m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
91-
tsrc->getTexture(m_moon_params.tonemap) : NULL;
91+
tsrc->getTexture(m_moon_params.tonemap) : nullptr;
9292

9393
if (m_sun_texture) {
9494
m_materials[3] = baseMaterial();
@@ -744,14 +744,14 @@ void Sky::place_sky_body(
744744
}
745745
}
746746

747-
void Sky::setSunTexture(std::string sun_texture,
748-
std::string sun_tonemap, ITextureSource *tsrc)
747+
void Sky::setSunTexture(const std::string &sun_texture,
748+
const std::string &sun_tonemap, ITextureSource *tsrc)
749749
{
750750
// Ignore matching textures (with modifiers) entirely,
751751
// but lets at least update the tonemap before hand.
752752
m_sun_params.tonemap = sun_tonemap;
753753
m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
754-
tsrc->getTexture(m_sun_params.tonemap) : NULL;
754+
tsrc->getTexture(m_sun_params.tonemap) : nullptr;
755755
m_materials[3].Lighting = !!m_sun_tonemap;
756756

757757
if (m_sun_params.texture == sun_texture)
@@ -780,7 +780,7 @@ void Sky::setSunTexture(std::string sun_texture,
780780
}
781781
}
782782

783-
void Sky::setSunriseTexture(std::string sunglow_texture,
783+
void Sky::setSunriseTexture(const std::string &sunglow_texture,
784784
ITextureSource* tsrc)
785785
{
786786
// Ignore matching textures (with modifiers) entirely.
@@ -792,14 +792,14 @@ void Sky::setSunriseTexture(std::string sunglow_texture,
792792
);
793793
}
794794

795-
void Sky::setMoonTexture(std::string moon_texture,
796-
std::string moon_tonemap, ITextureSource *tsrc)
795+
void Sky::setMoonTexture(const std::string &moon_texture,
796+
const std::string &moon_tonemap, ITextureSource *tsrc)
797797
{
798798
// Ignore matching textures (with modifiers) entirely,
799799
// but lets at least update the tonemap before hand.
800800
m_moon_params.tonemap = moon_tonemap;
801801
m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
802-
tsrc->getTexture(m_moon_params.tonemap) : NULL;
802+
tsrc->getTexture(m_moon_params.tonemap) : nullptr;
803803
m_materials[4].Lighting = !!m_moon_tonemap;
804804

805805
if (m_moon_params.texture == moon_texture)
@@ -893,7 +893,7 @@ void Sky::setSkyColors(const SkyColor &sky_color)
893893
}
894894

895895
void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
896-
std::string use_sun_tint)
896+
const std::string &use_sun_tint)
897897
{
898898
// Change sun and moon tinting:
899899
m_sky_params.fog_sun_tint = sun_tint;
@@ -907,7 +907,7 @@ void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
907907
m_default_tint = true;
908908
}
909909

910-
void Sky::addTextureToSkybox(std::string texture, int material_id,
910+
void Sky::addTextureToSkybox(const std::string &texture, int material_id,
911911
ITextureSource *tsrc)
912912
{
913913
// Sanity check for more than six textures.

src/client/sky.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class Sky : public scene::ISceneNode
6565
}
6666

6767
void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
68-
void setSunTexture(std::string sun_texture,
69-
std::string sun_tonemap, ITextureSource *tsrc);
68+
void setSunTexture(const std::string &sun_texture,
69+
const std::string &sun_tonemap, ITextureSource *tsrc);
7070
void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
7171
void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
72-
void setSunriseTexture(std::string sunglow_texture, ITextureSource* tsrc);
72+
void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);
7373

7474
void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
75-
void setMoonTexture(std::string moon_texture,
76-
std::string moon_tonemap, ITextureSource *tsrc);
75+
void setMoonTexture(const std::string &moon_texture,
76+
const std::string &moon_tonemap, ITextureSource *tsrc);
7777
void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
7878

7979
void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
@@ -87,21 +87,21 @@ class Sky : public scene::ISceneNode
8787
void setVisible(bool visible) { m_visible = visible; }
8888
// Set only from set_sky API
8989
void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
90-
void setFallbackBgColor(const video::SColor &fallback_bg_color)
90+
void setFallbackBgColor(video::SColor fallback_bg_color)
9191
{
9292
m_fallback_bg_color = fallback_bg_color;
9393
}
94-
void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor)
94+
void overrideColors(video::SColor bgcolor, video::SColor skycolor)
9595
{
9696
m_bgcolor = bgcolor;
9797
m_skycolor = skycolor;
9898
}
9999
void setSkyColors(const SkyColor &sky_color);
100100
void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
101-
std::string use_sun_tint);
101+
const std::string &use_sun_tint);
102102
void setInClouds(bool clouds) { m_in_clouds = clouds; }
103103
void clearSkyboxTextures() { m_sky_params.textures.clear(); }
104-
void addTextureToSkybox(std::string texture, int material_id,
104+
void addTextureToSkybox(const std::string &texture, int material_id,
105105
ITextureSource *tsrc);
106106
const video::SColorf &getCurrentStarColor() const { return m_star_color; }
107107

@@ -126,7 +126,7 @@ class Sky : public scene::ISceneNode
126126
}
127127

128128
// Mix two colors by a given amount
129-
video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
129+
static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
130130
{
131131
video::SColor result = video::SColor(
132132
col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
@@ -135,7 +135,7 @@ class Sky : public scene::ISceneNode
135135
col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
136136
return result;
137137
}
138-
video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
138+
static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
139139
{
140140
video::SColorf result =
141141
video::SColorf(col1.r * (1 - factor) + col2.r * factor,

src/clientiface.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ void ClientInterface::UpdatePlayerList()
671671
std::vector<session_t> clients = getClientIDs();
672672
m_clients_names.clear();
673673

674-
675674
if (!clients.empty())
676675
infostream<<"Players:"<<std::endl;
677676

src/gui/guiButtonItemImage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace gui;
3030

3131
GUIButtonItemImage::GUIButtonItemImage(gui::IGUIEnvironment *environment,
3232
gui::IGUIElement *parent, s32 id, core::rect<s32> rectangle,
33-
ISimpleTextureSource *tsrc, std::string item, Client *client,
33+
ISimpleTextureSource *tsrc, const std::string &item, Client *client,
3434
bool noclip)
3535
: GUIButton (environment, parent, id, rectangle, tsrc, noclip)
3636
{
@@ -44,7 +44,7 @@ GUIButtonItemImage::GUIButtonItemImage(gui::IGUIEnvironment *environment,
4444

4545
GUIButtonItemImage *GUIButtonItemImage::addButton(IGUIEnvironment *environment,
4646
const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
47-
IGUIElement *parent, s32 id, const wchar_t *text, std::string item,
47+
IGUIElement *parent, s32 id, const wchar_t *text, const std::string &item,
4848
Client *client)
4949
{
5050
GUIButtonItemImage *button = new GUIButtonItemImage(environment,

src/gui/guiButtonItemImage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class GUIButtonItemImage : public GUIButton
3333
//! constructor
3434
GUIButtonItemImage(gui::IGUIEnvironment *environment, gui::IGUIElement *parent,
3535
s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
36-
std::string item, Client *client, bool noclip = false);
36+
const std::string &item, Client *client, bool noclip = false);
3737

3838
//! Do not drop returned handle
3939
static GUIButtonItemImage *addButton(gui::IGUIEnvironment *environment,
4040
const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
41-
IGUIElement *parent, s32 id, const wchar_t *text, std::string item,
42-
Client *client);
41+
IGUIElement *parent, s32 id, const wchar_t *text,
42+
const std::string &item, Client *client);
4343

4444
private:
4545
Client *m_client;

src/inventory.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,14 @@ InventoryList * Inventory::getList(const std::string &name)
965965
{
966966
s32 i = getListIndex(name);
967967
if(i == -1)
968-
return NULL;
968+
return nullptr;
969969
return m_lists[i];
970970
}
971971

972972
std::vector<const InventoryList*> Inventory::getLists()
973973
{
974974
std::vector<const InventoryList*> lists;
975+
lists.reserve(m_lists.size());
975976
for (auto list : m_lists) {
976977
lists.push_back(list);
977978
}
@@ -990,11 +991,11 @@ bool Inventory::deleteList(const std::string &name)
990991
return true;
991992
}
992993

993-
const InventoryList * Inventory::getList(const std::string &name) const
994+
const InventoryList *Inventory::getList(const std::string &name) const
994995
{
995996
s32 i = getListIndex(name);
996997
if(i == -1)
997-
return NULL;
998+
return nullptr;
998999
return m_lists[i];
9991000
}
10001001

src/nodedef.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,10 +1544,10 @@ void NodeDefManager::deSerialize(std::istream &is)
15441544
}
15451545

15461546

1547-
void NodeDefManager::addNameIdMapping(content_t i, std::string name)
1547+
void NodeDefManager::addNameIdMapping(content_t i, const std::string &name)
15481548
{
15491549
m_name_id_mapping.set(i, name);
1550-
m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
1550+
m_name_id_mapping_with_aliases.emplace(name, i);
15511551
}
15521552

15531553

0 commit comments

Comments
 (0)