Skip to content

Commit

Permalink
Reserve vectors before pushing and other code quality changes (#11161)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Apr 5, 2021
1 parent 3e1904f commit f0bad0e
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 108 deletions.
9 changes: 5 additions & 4 deletions src/client/clientmap.cpp
Expand Up @@ -499,12 +499,12 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
static v3f z_directions[50] = {
v3f(-100, 0, 0)
};
static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
static f32 z_offsets[50] = {
-1000,
};

if(z_directions[0].X < -99){
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
if (z_directions[0].X < -99) {
for (u32 i = 0; i < ARRLEN(z_directions); i++) {
// Assumes FOV of 72 and 16/9 aspect ratio
z_directions[i] = v3f(
0.02 * myrand_range(-100, 100),
Expand All @@ -520,7 +520,8 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
if(sunlight_min_d > 35*BS)
sunlight_min_d = 35*BS;
std::vector<int> values;
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
values.reserve(ARRLEN(z_directions));
for (u32 i = 0; i < ARRLEN(z_directions); i++) {
v3f z_dir = z_directions[i];
core::CMatrix4<f32> a;
a.buildRotateFromTo(v3f(0,1,0), z_dir);
Expand Down
2 changes: 1 addition & 1 deletion src/client/clouds.cpp
Expand Up @@ -170,7 +170,7 @@ void Clouds::render()

// Read noise

std::vector<char> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2); // vector<bool> is broken
std::vector<bool> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2);
std::vector<video::S3DVertex> vertices;
vertices.reserve(16 * m_cloud_radius_i * m_cloud_radius_i);

Expand Down
16 changes: 8 additions & 8 deletions src/client/hud.cpp
Expand Up @@ -336,22 +336,22 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
irr::gui::IGUIFont* font = g_fontengine->getFont();

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

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

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

ids.insert(it, i);
elems.insert(it, e);
}

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

v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
floor(e->pos.Y * (float) m_screensize.Y + 0.5));
Expand Down Expand Up @@ -522,8 +522,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
client->getMinimap()->drawMinimap(rect);
break; }
default:
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type <<
" of hud element ID " << i << " due to unrecognized type" << std::endl;
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type
<< " due to unrecognized type" << std::endl;
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/client/sky.cpp
Expand Up @@ -82,13 +82,13 @@ Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
// Ensures that sun and moon textures and tonemaps are correct.
setSkyDefaults();
m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
tsrc->getTextureForMesh(m_sun_params.texture) : NULL;
tsrc->getTextureForMesh(m_sun_params.texture) : nullptr;
m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
tsrc->getTextureForMesh(m_moon_params.texture) : NULL;
tsrc->getTextureForMesh(m_moon_params.texture) : nullptr;
m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
tsrc->getTexture(m_sun_params.tonemap) : NULL;
tsrc->getTexture(m_sun_params.tonemap) : nullptr;
m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
tsrc->getTexture(m_moon_params.tonemap) : NULL;
tsrc->getTexture(m_moon_params.tonemap) : nullptr;

if (m_sun_texture) {
m_materials[3] = baseMaterial();
Expand Down Expand Up @@ -744,14 +744,14 @@ void Sky::place_sky_body(
}
}

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

if (m_sun_params.texture == sun_texture)
Expand Down Expand Up @@ -780,7 +780,7 @@ void Sky::setSunTexture(std::string sun_texture,
}
}

void Sky::setSunriseTexture(std::string sunglow_texture,
void Sky::setSunriseTexture(const std::string &sunglow_texture,
ITextureSource* tsrc)
{
// Ignore matching textures (with modifiers) entirely.
Expand All @@ -792,14 +792,14 @@ void Sky::setSunriseTexture(std::string sunglow_texture,
);
}

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

if (m_moon_params.texture == moon_texture)
Expand Down Expand Up @@ -893,7 +893,7 @@ void Sky::setSkyColors(const SkyColor &sky_color)
}

void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
std::string use_sun_tint)
const std::string &use_sun_tint)
{
// Change sun and moon tinting:
m_sky_params.fog_sun_tint = sun_tint;
Expand All @@ -907,7 +907,7 @@ void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
m_default_tint = true;
}

void Sky::addTextureToSkybox(std::string texture, int material_id,
void Sky::addTextureToSkybox(const std::string &texture, int material_id,
ITextureSource *tsrc)
{
// Sanity check for more than six textures.
Expand Down
22 changes: 11 additions & 11 deletions src/client/sky.h
Expand Up @@ -65,15 +65,15 @@ class Sky : public scene::ISceneNode
}

void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
void setSunTexture(std::string sun_texture,
std::string sun_tonemap, ITextureSource *tsrc);
void setSunTexture(const std::string &sun_texture,
const std::string &sun_tonemap, ITextureSource *tsrc);
void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
void setSunriseTexture(std::string sunglow_texture, ITextureSource* tsrc);
void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);

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

void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
Expand All @@ -87,21 +87,21 @@ class Sky : public scene::ISceneNode
void setVisible(bool visible) { m_visible = visible; }
// Set only from set_sky API
void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
void setFallbackBgColor(const video::SColor &fallback_bg_color)
void setFallbackBgColor(video::SColor fallback_bg_color)
{
m_fallback_bg_color = fallback_bg_color;
}
void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor)
void overrideColors(video::SColor bgcolor, video::SColor skycolor)
{
m_bgcolor = bgcolor;
m_skycolor = skycolor;
}
void setSkyColors(const SkyColor &sky_color);
void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
std::string use_sun_tint);
const std::string &use_sun_tint);
void setInClouds(bool clouds) { m_in_clouds = clouds; }
void clearSkyboxTextures() { m_sky_params.textures.clear(); }
void addTextureToSkybox(std::string texture, int material_id,
void addTextureToSkybox(const std::string &texture, int material_id,
ITextureSource *tsrc);
const video::SColorf &getCurrentStarColor() const { return m_star_color; }

Expand All @@ -126,7 +126,7 @@ class Sky : public scene::ISceneNode
}

// Mix two colors by a given amount
video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
{
video::SColor result = video::SColor(
col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
Expand All @@ -135,7 +135,7 @@ class Sky : public scene::ISceneNode
col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
return result;
}
video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
{
video::SColorf result =
video::SColorf(col1.r * (1 - factor) + col2.r * factor,
Expand Down
1 change: 0 additions & 1 deletion src/clientiface.cpp
Expand Up @@ -671,7 +671,6 @@ void ClientInterface::UpdatePlayerList()
std::vector<session_t> clients = getClientIDs();
m_clients_names.clear();


if (!clients.empty())
infostream<<"Players:"<<std::endl;

Expand Down
4 changes: 2 additions & 2 deletions src/gui/guiButtonItemImage.cpp
Expand Up @@ -30,7 +30,7 @@ using namespace gui;

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

GUIButtonItemImage *GUIButtonItemImage::addButton(IGUIEnvironment *environment,
const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
IGUIElement *parent, s32 id, const wchar_t *text, std::string item,
IGUIElement *parent, s32 id, const wchar_t *text, const std::string &item,
Client *client)
{
GUIButtonItemImage *button = new GUIButtonItemImage(environment,
Expand Down
6 changes: 3 additions & 3 deletions src/gui/guiButtonItemImage.h
Expand Up @@ -33,13 +33,13 @@ class GUIButtonItemImage : public GUIButton
//! constructor
GUIButtonItemImage(gui::IGUIEnvironment *environment, gui::IGUIElement *parent,
s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
std::string item, Client *client, bool noclip = false);
const std::string &item, Client *client, bool noclip = false);

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

private:
Client *m_client;
Expand Down
7 changes: 4 additions & 3 deletions src/inventory.cpp
Expand Up @@ -965,13 +965,14 @@ InventoryList * Inventory::getList(const std::string &name)
{
s32 i = getListIndex(name);
if(i == -1)
return NULL;
return nullptr;
return m_lists[i];
}

std::vector<const InventoryList*> Inventory::getLists()
{
std::vector<const InventoryList*> lists;
lists.reserve(m_lists.size());
for (auto list : m_lists) {
lists.push_back(list);
}
Expand All @@ -990,11 +991,11 @@ bool Inventory::deleteList(const std::string &name)
return true;
}

const InventoryList * Inventory::getList(const std::string &name) const
const InventoryList *Inventory::getList(const std::string &name) const
{
s32 i = getListIndex(name);
if(i == -1)
return NULL;
return nullptr;
return m_lists[i];
}

Expand Down
4 changes: 2 additions & 2 deletions src/nodedef.cpp
Expand Up @@ -1544,10 +1544,10 @@ void NodeDefManager::deSerialize(std::istream &is)
}


void NodeDefManager::addNameIdMapping(content_t i, std::string name)
void NodeDefManager::addNameIdMapping(content_t i, const std::string &name)
{
m_name_id_mapping.set(i, name);
m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
m_name_id_mapping_with_aliases.emplace(name, i);
}


Expand Down
2 changes: 1 addition & 1 deletion src/nodedef.h
Expand Up @@ -725,7 +725,7 @@ class NodeDefManager {
* @param i a content ID
* @param name a node name
*/
void addNameIdMapping(content_t i, std::string name);
void addNameIdMapping(content_t i, const std::string &name);

/*!
* Removes a content ID from all groups.
Expand Down
16 changes: 7 additions & 9 deletions src/nodemetadata.cpp
Expand Up @@ -206,10 +206,9 @@ NodeMetadataList::~NodeMetadataList()
std::vector<v3s16> NodeMetadataList::getAllKeys()
{
std::vector<v3s16> keys;

NodeMetadataMap::const_iterator it;
for (it = m_data.begin(); it != m_data.end(); ++it)
keys.push_back(it->first);
keys.reserve(m_data.size());
for (const auto &it : m_data)
keys.push_back(it.first);

return keys;
}
Expand All @@ -218,7 +217,7 @@ NodeMetadata *NodeMetadataList::get(v3s16 p)
{
NodeMetadataMap::const_iterator n = m_data.find(p);
if (n == m_data.end())
return NULL;
return nullptr;
return n->second;
}

Expand All @@ -235,7 +234,7 @@ void NodeMetadataList::remove(v3s16 p)
void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
{
remove(p);
m_data.insert(std::make_pair(p, d));
m_data.emplace(p, d);
}

void NodeMetadataList::clear()
Expand All @@ -251,9 +250,8 @@ void NodeMetadataList::clear()
int NodeMetadataList::countNonEmpty() const
{
int n = 0;
NodeMetadataMap::const_iterator it;
for (it = m_data.begin(); it != m_data.end(); ++it) {
if (!it->second->empty())
for (const auto &it : m_data) {
if (!it.second->empty())
n++;
}
return n;
Expand Down
2 changes: 1 addition & 1 deletion src/pathfinder.cpp
Expand Up @@ -1428,7 +1428,7 @@ std::string Pathfinder::dirToName(PathDirections dir)
}

/******************************************************************************/
void Pathfinder::printPath(std::vector<v3s16> path)
void Pathfinder::printPath(const std::vector<v3s16> &path)
{
unsigned int current = 0;
for (std::vector<v3s16>::iterator i = path.begin();
Expand Down
5 changes: 3 additions & 2 deletions src/settings.cpp
Expand Up @@ -43,11 +43,11 @@ std::unordered_map<std::string, const FlagDesc *> Settings::s_flags;
Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag)
{
if ((int)sl < 0 || sl >= SL_TOTAL_COUNT)
throw new BaseException("Invalid settings layer");
throw BaseException("Invalid settings layer");

Settings *&pos = s_layers[(size_t)sl];
if (pos)
throw new BaseException("Setting layer " + std::to_string(sl) + " already exists");
throw BaseException("Setting layer " + std::to_string(sl) + " already exists");

pos = new Settings(end_tag);
pos->m_settingslayer = sl;
Expand Down Expand Up @@ -638,6 +638,7 @@ std::vector<std::string> Settings::getNames() const
MutexAutoLock lock(m_mutex);

std::vector<std::string> names;
names.reserve(m_settings.size());
for (const auto &settings_it : m_settings) {
names.push_back(settings_it.first);
}
Expand Down

0 comments on commit f0bad0e

Please sign in to comment.