Optimize parameter passing to avoid unnecessary copying

This commit is contained in:
Pugemon
2024-06-07 04:00:38 +03:00
parent 46ca1bb04a
commit f25a425cb9
123 changed files with 578 additions and 522 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ bool ChunksController::loadVisible(){
return true;
}
bool ChunksController::buildLights(std::shared_ptr<Chunk> chunk) {
bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
int surrounding = 0;
for (int oz = -1; oz <= 1; oz++){
for (int ox = -1; ox <= 1; ox++){
+1 -1
View File
@@ -21,7 +21,7 @@ private:
/// @brief Process one chunk: load it or calculate lights for it
bool loadVisible();
bool buildLights(std::shared_ptr<Chunk> chunk);
bool buildLights(const std::shared_ptr<Chunk>& chunk);
void createChunk(int x, int y);
public:
ChunksController(Level* level, uint padding);
+5 -4
View File
@@ -4,6 +4,7 @@
#include "../util/stringutil.hpp"
#include <iostream>
#include <utility>
using namespace cmd;
@@ -156,7 +157,7 @@ public:
}
return Command(
name, std::move(args), std::move(kwargs),
std::string(description), executor
std::string(description), std::move(executor)
);
}
@@ -256,7 +257,7 @@ public:
dynamic::Value applyRelative(
Argument* arg,
dynamic::Value value,
dynamic::Value origin
const dynamic::Value& origin
) {
if (origin.index() == 0) {
return value;
@@ -380,7 +381,7 @@ public:
};
Command Command::create(std::string_view scheme, std::string_view description, executor_func executor) {
return CommandParser("<string>", scheme).parseScheme(executor, description);
return CommandParser("<string>", scheme).parseScheme(std::move(executor), description);
}
void CommandsRepository::add(
@@ -388,7 +389,7 @@ void CommandsRepository::add(
std::string_view description,
executor_func executor
) {
Command command = Command::create(scheme, description, executor);
Command command = Command::create(scheme, description, std::move(executor));
commands[command.getName()] = command;
}
+13 -13
View File
@@ -27,7 +27,7 @@ static debug::Logger logger("engine-control");
EngineController::EngineController(Engine* engine) : engine(engine) {
}
void EngineController::deleteWorld(std::string name) {
void EngineController::deleteWorld(const std::string& name) {
fs::path folder = engine->getPaths()->getWorldFolder(name);
guiutil::confirm(engine->getGUI(), langs::get(L"delete-confirm", L"world")+
L" ("+util::str2wstr_utf8(folder.u8string())+L")", [=]() {
@@ -38,10 +38,10 @@ void EngineController::deleteWorld(std::string name) {
std::shared_ptr<Task> create_converter(
Engine* engine,
fs::path folder,
const fs::path& folder,
const Content* content,
std::shared_ptr<ContentLUT> lut,
runnable postRunnable)
const std::shared_ptr<ContentLUT>& lut,
const runnable& postRunnable)
{
return WorldConverter::startTask(folder, content, lut, [=](){
auto menu = engine->getGUI()->getMenu();
@@ -56,9 +56,9 @@ std::shared_ptr<Task> create_converter(
void show_convert_request(
Engine* engine,
const Content* content,
std::shared_ptr<ContentLUT> lut,
fs::path folder,
runnable postRunnable
const std::shared_ptr<ContentLUT>& lut,
const fs::path& folder,
const runnable& postRunnable
) {
guiutil::confirm(engine->getGUI(), langs::get(L"world.convert-request"), [=]() {
auto converter = create_converter(engine, folder, content, lut, postRunnable);
@@ -68,7 +68,7 @@ void show_convert_request(
static void show_content_missing(
Engine* engine,
std::shared_ptr<ContentLUT> lut
const std::shared_ptr<ContentLUT>& lut
) {
using namespace dynamic;
auto root = create_map();
@@ -82,7 +82,7 @@ static void show_content_missing(
menus::show(engine, "reports/missing_content", {root});
}
static bool loadWorldContent(Engine* engine, fs::path folder) {
static bool loadWorldContent(Engine* engine, const fs::path& folder) {
try {
engine->loadWorldContent(folder);
return true;
@@ -104,7 +104,7 @@ static bool loadWorldContent(Engine* engine, fs::path folder) {
}
}
static void loadWorld(Engine* engine, fs::path folder) {
static void loadWorld(Engine* engine, const fs::path& folder) {
try {
auto content = engine->getContent();
auto& packs = engine->getContentPacks();
@@ -121,7 +121,7 @@ static void loadWorld(Engine* engine, fs::path folder) {
}
}
void EngineController::openWorld(std::string name, bool confirmConvert) {
void EngineController::openWorld(const std::string& name, bool confirmConvert) {
auto paths = engine->getPaths();
auto folder = paths->getWorldsFolder()/fs::u8path(name);
if (!loadWorldContent(engine, folder)) {
@@ -215,8 +215,8 @@ void EngineController::reopenWorld(World* world) {
void EngineController::reconfigPacks(
LevelController* controller,
std::vector<std::string> packsToAdd,
std::vector<std::string> packsToRemove
const std::vector<std::string>& packsToAdd,
const std::vector<std::string>& packsToRemove
) {
auto content = engine->getContent();
bool hasIndices = false;
+4 -4
View File
@@ -16,16 +16,16 @@ public:
/// @brief Load world, convert if required and set to LevelScreen.
/// @param name world name
/// @param confirmConvert automatically confirm convert if requested
void openWorld(std::string name, bool confirmConvert);
void openWorld(const std::string& name, bool confirmConvert);
/// @brief Show world removal confirmation dialog
/// @param name world name
void deleteWorld(std::string name);
void deleteWorld(const std::string& name);
void reconfigPacks(
LevelController* controller,
std::vector<std::string> packsToAdd,
std::vector<std::string> packsToRemove
const std::vector<std::string>& packsToAdd,
const std::vector<std::string>& packsToRemove
);
void createWorld(
+1 -1
View File
@@ -39,7 +39,7 @@ void LevelController::update(float delta, bool input, bool pause) {
if (!pause) {
// update all objects that needed
for (auto obj : level->objects) {
for (const auto& obj : level->objects) {
if (obj && obj->shouldUpdate) {
obj->update(delta);
}
+3 -3
View File
@@ -35,7 +35,7 @@ const float RUN_ZOOM = 1.1f;
const float C_ZOOM = 0.1f;
const float CROUCH_SHIFT_Y = -0.2f;
CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
CameraControl::CameraControl(const std::shared_ptr<Player>& player, const CameraSettings& settings)
: player(player),
camera(player->camera),
settings(settings),
@@ -186,7 +186,7 @@ void PlayerController::onBlockInteraction(
const Block* def,
BlockInteraction type
) {
for (auto callback : blockInteractionCallbacks) {
for (const auto& callback : blockInteractionCallbacks) {
callback(player.get(), pos, def, type);
}
}
@@ -456,6 +456,6 @@ Player* PlayerController::getPlayer() {
return player.get();
}
void PlayerController::listenBlockInteraction(on_block_interaction callback) {
void PlayerController::listenBlockInteraction(const on_block_interaction& callback) {
blockInteractionCallbacks.push_back(callback);
}
+2 -2
View File
@@ -37,7 +37,7 @@ class CameraControl {
/// @brief Switch active player camera
void switchCamera();
public:
CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings);
CameraControl(const std::shared_ptr<Player>& player, const CameraSettings& settings);
void updateMouse(PlayerInput& input);
void update(const PlayerInput& input, float delta, Chunks* chunks);
void refresh();
@@ -91,7 +91,7 @@ public:
Player* getPlayer();
void listenBlockInteraction(on_block_interaction callback);
void listenBlockInteraction(const on_block_interaction& callback);
};
#endif /* PLAYER_CONTROL_HPP_ */
+1 -1
View File
@@ -103,7 +103,7 @@ static int l_reconfig_packs(lua_State* L) {
static int l_get_setting(lua_State* L) {
auto name = lua_tostring(L, 1);
const auto value = scripting::engine->getSettingsHandler().getValue(name);
scripting::state->pushvalue(std::move(value));
scripting::state->pushvalue(value);
return 1;
}
+1 -1
View File
@@ -494,7 +494,7 @@ static void p_set_inventory(UINode* node, int idx) {
}
}
}
static void p_set_focused(std::shared_ptr<UINode> node, int idx) {
static void p_set_focused(const std::shared_ptr<UINode> &node, int idx) {
if (state->toboolean(idx) && !node->isFocused()) {
scripting::engine->getGUI()->setFocus(node);
} else if (node->isFocused()){
+6 -6
View File
@@ -37,7 +37,7 @@ const ContentIndices* scripting::indices = nullptr;
BlocksController* scripting::blocks = nullptr;
LevelController* scripting::controller = nullptr;
void load_script(fs::path name) {
void load_script(const fs::path& name) {
auto paths = scripting::engine->getPaths();
fs::path file = paths->getResources()/fs::path("scripts")/name;
@@ -72,7 +72,7 @@ scriptenv scripting::create_pack_environment(const ContentPack& pack) {
});
}
scriptenv scripting::create_doc_environment(scriptenv parent, const std::string& name) {
scriptenv scripting::create_doc_environment(const scriptenv& parent, const std::string& name) {
int id = state->createEnvironment(*parent);
state->pushenv(id);
state->pushvalue(-1);
@@ -279,7 +279,7 @@ bool scripting::register_event(int env, const std::string& name, const std::stri
return false;
}
void scripting::load_block_script(scriptenv senv, std::string prefix, fs::path file, block_funcs_set& funcsset) {
void scripting::load_block_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset) {
int env = *senv;
std::string src = files::read_string(file);
logger.info() << "script (block) " << file.u8string();
@@ -293,7 +293,7 @@ void scripting::load_block_script(scriptenv senv, std::string prefix, fs::path f
funcsset.onblockstick = register_event(env, "on_blocks_tick", prefix+".blockstick");
}
void scripting::load_item_script(scriptenv senv, std::string prefix, fs::path file, item_funcs_set& funcsset) {
void scripting::load_item_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, item_funcs_set& funcsset) {
int env = *senv;
std::string src = files::read_string(file);
logger.info() << "script (item) " << file.u8string();
@@ -305,7 +305,7 @@ void scripting::load_item_script(scriptenv senv, std::string prefix, fs::path fi
funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby");
}
void scripting::load_world_script(scriptenv senv, std::string prefix, fs::path file) {
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
int env = *senv;
std::string src = files::read_string(file);
@@ -321,7 +321,7 @@ void scripting::load_world_script(scriptenv senv, std::string prefix, fs::path f
register_event(env, "on_world_quit", prefix+".worldquit");
}
void scripting::load_layout_script(scriptenv senv, std::string prefix, fs::path file, uidocscript& script) {
void scripting::load_layout_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, uidocscript& script) {
int env = *senv;
std::string src = files::read_string(file);
+5 -5
View File
@@ -45,7 +45,7 @@ namespace scripting {
scriptenv get_root_environment();
scriptenv create_pack_environment(const ContentPack& pack);
scriptenv create_doc_environment(scriptenv parent, const std::string& name);
scriptenv create_doc_environment(const scriptenv& parent, const std::string& name);
void process_post_runnables();
@@ -88,27 +88,27 @@ namespace scripting {
/// @param prefix pack id
/// @param file item script file
/// @param funcsset block callbacks set
void load_block_script(scriptenv env, std::string prefix, fs::path file, block_funcs_set& funcsset);
void load_block_script(const scriptenv& env, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset);
/// @brief Load script associated with an Item
/// @param env environment
/// @param prefix pack id
/// @param file item script file
/// @param funcsset item callbacks set
void load_item_script(scriptenv env, std::string prefix, fs::path file, item_funcs_set& funcsset);
void load_item_script(const scriptenv& env, const std::string& prefix, const fs::path& file, item_funcs_set& funcsset);
/// @brief Load package-specific world script
/// @param env environment
/// @param packid content-pack id
/// @param file script file path
void load_world_script(scriptenv env, std::string packid, fs::path file);
void load_world_script(const scriptenv& env, const std::string& packid, const fs::path& file);
/// @brief Load script associated with an UiDocument
/// @param env environment
/// @param prefix pack id
/// @param file item script file
/// @param script document script info
void load_layout_script(scriptenv env, std::string prefix, fs::path file, uidocscript& script);
void load_layout_script(const scriptenv& env, const std::string& prefix, const fs::path& file, uidocscript& script);
/// @brief Finalize lua state. Using scripting after will lead to Lua panic
void close();
+1 -1
View File
@@ -40,7 +40,7 @@ void scripting::on_frontend_close() {
scripting::hud = nullptr;
}
void scripting::load_hud_script(scriptenv senv, std::string packid, fs::path file) {
void scripting::load_hud_script(const scriptenv& senv, const std::string& packid, const fs::path& file) {
int env = *senv;
std::string src = files::read_string(file);
logger.info() << "loading script " << file.u8string();
+2 -2
View File
@@ -17,12 +17,12 @@ namespace scripting {
void on_frontend_close();
/**
* Load package-specific hud script
* Load package-specific hud script
* @param env environment id
* @param packid content-pack id
* @param file script file path
*/
void load_hud_script(scriptenv env, std::string packid, fs::path file);
void load_hud_script(const scriptenv &env, const std::string &packid, const fs::path &file);
}
#endif // LOGIC_SCRIPTING_SCRIPTING_HUD_HPP_