Optimize parameter passing to avoid unnecessary copying
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@@ -22,7 +23,7 @@ class ConverterWorker : public util::Worker<convert_task, int> {
|
||||
std::shared_ptr<WorldConverter> converter;
|
||||
public:
|
||||
ConverterWorker(std::shared_ptr<WorldConverter> converter)
|
||||
: converter(converter) {}
|
||||
: converter(std::move(converter)) {}
|
||||
|
||||
int operator()(const std::shared_ptr<convert_task>& task) override {
|
||||
converter->convert(*task);
|
||||
@@ -31,11 +32,11 @@ public:
|
||||
};
|
||||
|
||||
WorldConverter::WorldConverter(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
const Content* content,
|
||||
std::shared_ptr<ContentLUT> lut
|
||||
) : wfile(std::make_unique<WorldFiles>(folder)),
|
||||
lut(lut),
|
||||
lut(std::move(lut)),
|
||||
content(content)
|
||||
{
|
||||
fs::path regionsFolder = wfile->getRegions().getRegionsFolder(REGION_LAYER_VOXELS);
|
||||
@@ -44,7 +45,7 @@ WorldConverter::WorldConverter(
|
||||
return;
|
||||
}
|
||||
tasks.push(convert_task {convert_task_type::player, wfile->getPlayerFile()});
|
||||
for (auto file : fs::directory_iterator(regionsFolder)) {
|
||||
for (const auto& file : fs::directory_iterator(regionsFolder)) {
|
||||
tasks.push(convert_task {convert_task_type::region, file.path()});
|
||||
}
|
||||
}
|
||||
@@ -53,10 +54,10 @@ WorldConverter::~WorldConverter() {
|
||||
}
|
||||
|
||||
std::shared_ptr<Task> WorldConverter::startTask(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
const Content* content,
|
||||
std::shared_ptr<ContentLUT> lut,
|
||||
runnable onDone,
|
||||
const std::shared_ptr<ContentLUT>& lut,
|
||||
const runnable& onDone,
|
||||
bool multithreading
|
||||
) {
|
||||
auto converter = std::make_shared<WorldConverter>(folder, content, lut);
|
||||
@@ -85,7 +86,7 @@ std::shared_ptr<Task> WorldConverter::startTask(
|
||||
return pool;
|
||||
}
|
||||
|
||||
void WorldConverter::convertRegion(fs::path file) const {
|
||||
void WorldConverter::convertRegion(const fs::path& file) const {
|
||||
int x, z;
|
||||
std::string name = file.stem().string();
|
||||
if (!WorldRegions::parseRegionFilename(name, x, z)) {
|
||||
@@ -101,14 +102,14 @@ void WorldConverter::convertRegion(fs::path file) const {
|
||||
});
|
||||
}
|
||||
|
||||
void WorldConverter::convertPlayer(fs::path file) const {
|
||||
void WorldConverter::convertPlayer(const fs::path& file) const {
|
||||
logger.info() << "converting player " << file.u8string();
|
||||
auto map = files::read_json(file);
|
||||
Player::convert(map.get(), lut.get());
|
||||
files::write_json(file, map.get());
|
||||
}
|
||||
|
||||
void WorldConverter::convert(convert_task task) const {
|
||||
void WorldConverter::convert(const convert_task& task) const {
|
||||
if (!fs::is_regular_file(task.file))
|
||||
return;
|
||||
|
||||
@@ -134,7 +135,7 @@ void WorldConverter::convertNext() {
|
||||
}
|
||||
|
||||
void WorldConverter::setOnComplete(runnable callback) {
|
||||
this->onComplete = callback;
|
||||
this->onComplete = std::move(callback);
|
||||
}
|
||||
|
||||
void WorldConverter::update() {
|
||||
|
||||
@@ -32,17 +32,17 @@ class WorldConverter : public Task {
|
||||
runnable onComplete;
|
||||
uint tasksDone = 0;
|
||||
|
||||
void convertPlayer(fs::path file) const;
|
||||
void convertRegion(fs::path file) const;
|
||||
void convertPlayer(const fs::path& file) const;
|
||||
void convertRegion(const fs::path& file) const;
|
||||
public:
|
||||
WorldConverter(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
const Content* content,
|
||||
std::shared_ptr<ContentLUT> lut
|
||||
);
|
||||
~WorldConverter();
|
||||
|
||||
void convert(convert_task task) const;
|
||||
void convert(const convert_task& task) const;
|
||||
void convertNext();
|
||||
void setOnComplete(runnable callback);
|
||||
void write();
|
||||
@@ -55,10 +55,10 @@ public:
|
||||
uint getWorkDone() const override;
|
||||
|
||||
static std::shared_ptr<Task> startTask(
|
||||
fs::path folder,
|
||||
const fs::path& folder,
|
||||
const Content* content,
|
||||
std::shared_ptr<ContentLUT> lut,
|
||||
runnable onDone,
|
||||
const std::shared_ptr<ContentLUT>& lut,
|
||||
const runnable& onDone,
|
||||
bool multithreading
|
||||
);
|
||||
};
|
||||
|
||||
@@ -27,14 +27,15 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
#define WORLD_FORMAT_MAGIC ".VOXWLD"
|
||||
|
||||
WorldFiles::WorldFiles(fs::path directory) : directory(directory), regions(directory) {
|
||||
WorldFiles::WorldFiles(const fs::path& directory) : directory(directory), regions(directory) {
|
||||
}
|
||||
|
||||
WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
|
||||
: WorldFiles(directory)
|
||||
WorldFiles::WorldFiles(const fs::path& directory, const DebugSettings& settings)
|
||||
: WorldFiles(directory)
|
||||
{
|
||||
generatorTestMode = settings.generatorTestMode.get();
|
||||
doWriteLights = settings.doWriteLights.get();
|
||||
|
||||
@@ -41,8 +41,8 @@ class WorldFiles {
|
||||
void writeWorldInfo(const World* world);
|
||||
void writeIndices(const ContentIndices* indices);
|
||||
public:
|
||||
WorldFiles(fs::path directory);
|
||||
WorldFiles(fs::path directory, const DebugSettings& settings);
|
||||
WorldFiles(const fs::path &directory);
|
||||
WorldFiles(const fs::path &directory, const DebugSettings& settings);
|
||||
~WorldFiles();
|
||||
|
||||
fs::path getPlayerFile() const;
|
||||
|
||||
@@ -8,10 +8,11 @@
|
||||
#include "../util/data_io.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
#define REGION_FORMAT_MAGIC ".VOXREG"
|
||||
|
||||
regfile::regfile(fs::path filename) : file(filename) {
|
||||
regfile::regfile(fs::path filename) : file(std::move(filename)) {
|
||||
if (file.length() < REGION_HEADER_SIZE)
|
||||
throw std::runtime_error("incomplete region file header");
|
||||
char header[REGION_HEADER_SIZE];
|
||||
@@ -92,7 +93,7 @@ uint WorldRegion::getChunkDataSize(uint x, uint z) {
|
||||
return sizes[z * REGION_SIZE + x];
|
||||
}
|
||||
|
||||
WorldRegions::WorldRegions(fs::path directory) : directory(directory) {
|
||||
WorldRegions::WorldRegions(const fs::path& directory) : directory(directory) {
|
||||
for (uint i = 0; i < sizeof(layers)/sizeof(RegionsLayer); i++) {
|
||||
layers[i].layer = i;
|
||||
}
|
||||
@@ -427,7 +428,7 @@ chunk_inventories_map WorldRegions::fetchInventories(int x, int z) {
|
||||
return inventories;
|
||||
}
|
||||
|
||||
void WorldRegions::processRegionVoxels(int x, int z, regionproc func) {
|
||||
void WorldRegions::processRegionVoxels(int x, int z, const regionproc& func) {
|
||||
if (getRegion(x, z, REGION_LAYER_VOXELS)) {
|
||||
throw std::runtime_error("not implemented for in-memory regions");
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ public:
|
||||
bool generatorTestMode = false;
|
||||
bool doWriteLights = true;
|
||||
|
||||
WorldRegions(fs::path directory);
|
||||
WorldRegions(const fs::path &directory);
|
||||
WorldRegions(const WorldRegions&) = delete;
|
||||
~WorldRegions();
|
||||
|
||||
@@ -148,7 +148,7 @@ public:
|
||||
std::unique_ptr<light_t[]> getLights(int x, int z);
|
||||
chunk_inventories_map fetchInventories(int x, int z);
|
||||
|
||||
void processRegionVoxels(int x, int z, regionproc func);
|
||||
void processRegionVoxels(int x, int z, const regionproc& func);
|
||||
|
||||
fs::path getRegionsFolder(int layer) const;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#include "../util/stringutil.hpp"
|
||||
#include "../typedefs.hpp"
|
||||
@@ -21,7 +22,7 @@ fs::path EnginePaths::getResources() const {
|
||||
return resources;
|
||||
}
|
||||
|
||||
fs::path EnginePaths::getScreenshotFile(std::string ext) {
|
||||
fs::path EnginePaths::getScreenshotFile(const std::string& ext) {
|
||||
fs::path folder = userfiles/fs::path(SCREENSHOTS_FOLDER);
|
||||
if (!fs::is_directory(folder)) {
|
||||
fs::create_directory(folder);
|
||||
@@ -75,11 +76,11 @@ std::vector<fs::path> EnginePaths::scanForWorlds() {
|
||||
if (!fs::is_directory(folder))
|
||||
return folders;
|
||||
|
||||
for (auto entry : fs::directory_iterator(folder)) {
|
||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||
if (!entry.is_directory()) {
|
||||
continue;
|
||||
}
|
||||
fs::path worldFolder = entry.path();
|
||||
const fs::path& worldFolder = entry.path();
|
||||
fs::path worldFile = worldFolder/fs::u8path(WorldFiles::WORLD_FILE);
|
||||
if (!fs::is_regular_file(worldFile)) {
|
||||
continue;
|
||||
@@ -94,20 +95,20 @@ std::vector<fs::path> EnginePaths::scanForWorlds() {
|
||||
return folders;
|
||||
}
|
||||
|
||||
bool EnginePaths::isWorldNameUsed(std::string name) {
|
||||
bool EnginePaths::isWorldNameUsed(const std::string& name) {
|
||||
return fs::exists(EnginePaths::getWorldsFolder()/fs::u8path(name));
|
||||
}
|
||||
|
||||
void EnginePaths::setUserfiles(fs::path folder) {
|
||||
this->userfiles = folder;
|
||||
this->userfiles = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setResources(fs::path folder) {
|
||||
this->resources = folder;
|
||||
this->resources = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setWorldFolder(fs::path folder) {
|
||||
this->worldFolder = folder;
|
||||
this->worldFolder = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
||||
@@ -139,7 +140,7 @@ static fs::path toCanonic(fs::path path) {
|
||||
return path;
|
||||
}
|
||||
|
||||
fs::path EnginePaths::resolve(std::string path, bool throwErr) {
|
||||
fs::path EnginePaths::resolve(const std::string& path, bool throwErr) {
|
||||
size_t separator = path.find(':');
|
||||
if (separator == std::string::npos) {
|
||||
throw files_access_error("no entry point specified");
|
||||
@@ -172,7 +173,7 @@ fs::path EnginePaths::resolve(std::string path, bool throwErr) {
|
||||
}
|
||||
|
||||
ResPaths::ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots)
|
||||
: mainRoot(mainRoot), roots(roots) {
|
||||
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
|
||||
}
|
||||
|
||||
fs::path ResPaths::find(const std::string& filename) const {
|
||||
|
||||
@@ -24,14 +24,14 @@ public:
|
||||
fs::path getUserfiles() const;
|
||||
fs::path getResources() const;
|
||||
|
||||
fs::path getScreenshotFile(std::string ext);
|
||||
fs::path getScreenshotFile(const std::string& ext);
|
||||
fs::path getWorldsFolder();
|
||||
fs::path getWorldFolder();
|
||||
fs::path getWorldFolder(const std::string& name);
|
||||
fs::path getControlsFile();
|
||||
fs::path getControlsFileOld(); // TODO: remove in 0.22
|
||||
fs::path getSettingsFile();
|
||||
bool isWorldNameUsed(std::string name);
|
||||
bool isWorldNameUsed(const std::string& name);
|
||||
|
||||
void setUserfiles(fs::path folder);
|
||||
void setResources(fs::path folder);
|
||||
@@ -40,7 +40,7 @@ public:
|
||||
|
||||
std::vector<fs::path> scanForWorlds();
|
||||
|
||||
fs::path resolve(std::string path, bool throwErr=true);
|
||||
fs::path resolve(const std::string& path, bool throwErr=true);
|
||||
};
|
||||
|
||||
struct PathsRoot {
|
||||
|
||||
+13
-13
@@ -15,7 +15,7 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
files::rafile::rafile(fs::path filename)
|
||||
files::rafile::rafile(const fs::path& filename)
|
||||
: file(filename, std::ios::binary | std::ios::ate) {
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file "+filename.string());
|
||||
@@ -36,7 +36,7 @@ void files::rafile::read(char* buffer, std::streamsize size) {
|
||||
file.read(buffer, size);
|
||||
}
|
||||
|
||||
bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
bool files::write_bytes(const fs::path& filename, const ubyte* data, size_t size) {
|
||||
std::ofstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
@@ -45,7 +45,7 @@ bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
uint files::append_bytes(const fs::path& filename, const ubyte* data, size_t size) {
|
||||
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
||||
if (!output.is_open())
|
||||
return 0;
|
||||
@@ -55,7 +55,7 @@ uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||
return position;
|
||||
}
|
||||
|
||||
bool files::read(fs::path filename, char* data, size_t size) {
|
||||
bool files::read(const fs::path& filename, char* data, size_t size) {
|
||||
std::ifstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
@@ -64,7 +64,7 @@ bool files::read(fs::path filename, char* data, size_t size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<ubyte[]> files::read_bytes(fs::path filename, size_t& length) {
|
||||
std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& length) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open())
|
||||
return nullptr;
|
||||
@@ -78,7 +78,7 @@ std::unique_ptr<ubyte[]> files::read_bytes(fs::path filename, size_t& length) {
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string files::read_string(fs::path filename) {
|
||||
std::string files::read_string(const fs::path& filename) {
|
||||
size_t size;
|
||||
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
|
||||
if (bytes == nullptr) {
|
||||
@@ -88,7 +88,7 @@ std::string files::read_string(fs::path filename) {
|
||||
return std::string((const char*)bytes.get(), size);
|
||||
}
|
||||
|
||||
bool files::write_string(fs::path filename, const std::string content) {
|
||||
bool files::write_string(const fs::path& filename, const std::string content) {
|
||||
std::ofstream file(filename);
|
||||
if (!file) {
|
||||
return false;
|
||||
@@ -97,16 +97,16 @@ bool files::write_string(fs::path filename, const std::string content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||
bool files::write_json(const fs::path& filename, const dynamic::Map* obj, bool nice) {
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool compression) {
|
||||
bool files::write_binary_json(const fs::path& filename, const dynamic::Map* obj, bool compression) {
|
||||
auto bytes = json::to_binary(obj, compression);
|
||||
return files::write_bytes(filename, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||
std::shared_ptr<dynamic::Map> files::read_json(const fs::path& filename) {
|
||||
std::string text = files::read_string(filename);
|
||||
try {
|
||||
return json::parse(filename.string(), text);;
|
||||
@@ -116,17 +116,17 @@ std::shared_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
||||
std::shared_ptr<dynamic::Map> files::read_binary_json(const fs::path& file) {
|
||||
size_t size;
|
||||
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
|
||||
return json::from_binary(bytes.get(), size);
|
||||
}
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_toml(fs::path file) {
|
||||
std::shared_ptr<dynamic::Map> files::read_toml(const fs::path& file) {
|
||||
return toml::parse(file.u8string(), files::read_string(file));
|
||||
}
|
||||
|
||||
std::vector<std::string> files::read_list(fs::path filename) {
|
||||
std::vector<std::string> files::read_list(const fs::path& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file "+filename.u8string());
|
||||
|
||||
+13
-13
@@ -20,7 +20,7 @@ namespace files {
|
||||
std::ifstream file;
|
||||
size_t filelength;
|
||||
public:
|
||||
rafile(fs::path filename);
|
||||
rafile(const fs::path& filename);
|
||||
|
||||
void seekg(std::streampos pos);
|
||||
void read(char* buffer, std::streamsize size);
|
||||
@@ -31,40 +31,40 @@ namespace files {
|
||||
/// @param file target file
|
||||
/// @param data data bytes array
|
||||
/// @param size size of data bytes array
|
||||
bool write_bytes(fs::path file, const ubyte* data, size_t size);
|
||||
bool write_bytes(const fs::path& file, const ubyte* data, size_t size);
|
||||
|
||||
/// @brief Append bytes array to the file without any extra data
|
||||
/// @param file target file
|
||||
/// @param data data bytes array
|
||||
/// @param size size of data bytes array
|
||||
uint append_bytes(fs::path file, const ubyte* data, size_t size);
|
||||
uint append_bytes(const fs::path& file, const ubyte* data, size_t size);
|
||||
|
||||
/// @brief Write string to the file
|
||||
bool write_string(fs::path filename, const std::string content);
|
||||
bool write_string(const fs::path& filename, const std::string content);
|
||||
|
||||
/// @brief Write dynamic data to the JSON file
|
||||
/// @param nice if true, human readable format will be used, otherwise minimal
|
||||
bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
|
||||
bool write_json(const fs::path& filename, const dynamic::Map* obj, bool nice=true);
|
||||
|
||||
/// @brief Write dynamic data to the binary JSON file
|
||||
/// (see src/coders/binary_json_spec.md)
|
||||
/// @param compressed use gzip compression
|
||||
bool write_binary_json(
|
||||
fs::path filename,
|
||||
const fs::path& filename,
|
||||
const dynamic::Map* obj,
|
||||
bool compressed=false
|
||||
);
|
||||
|
||||
bool read(fs::path, char* data, size_t size);
|
||||
std::unique_ptr<ubyte[]> read_bytes(fs::path, size_t& length);
|
||||
std::string read_string(fs::path filename);
|
||||
bool read(const fs::path&, char* data, size_t size);
|
||||
std::unique_ptr<ubyte[]> read_bytes(const fs::path&, size_t& length);
|
||||
std::string read_string(const fs::path& filename);
|
||||
|
||||
/// @brief Read JSON or BJSON file
|
||||
/// @param file *.json or *.bjson file
|
||||
std::shared_ptr<dynamic::Map> read_json(fs::path file);
|
||||
std::shared_ptr<dynamic::Map> read_binary_json(fs::path file);
|
||||
std::shared_ptr<dynamic::Map> read_toml(fs::path file);
|
||||
std::vector<std::string> read_list(fs::path file);
|
||||
std::shared_ptr<dynamic::Map> read_json(const fs::path& file);
|
||||
std::shared_ptr<dynamic::Map> read_binary_json(const fs::path& file);
|
||||
std::shared_ptr<dynamic::Map> read_toml(const fs::path& file);
|
||||
std::vector<std::string> read_list(const fs::path& file);
|
||||
}
|
||||
|
||||
#endif /* FILES_FILES_HPP_ */
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "../settings.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
static debug::Logger logger("settings_io");
|
||||
|
||||
@@ -22,10 +23,10 @@ struct SectionsBuilder {
|
||||
}
|
||||
|
||||
void section(std::string name) {
|
||||
sections.push_back(Section {name, {}});
|
||||
sections.push_back(Section {std::move(name), {}});
|
||||
}
|
||||
|
||||
void add(std::string name, Setting* setting, bool writeable=true) {
|
||||
void add(const std::string& name, Setting* setting, bool writeable=true) {
|
||||
Section& section = sections.at(sections.size()-1);
|
||||
map[section.name+"."+name] = setting;
|
||||
section.keys.push_back(name);
|
||||
|
||||
Reference in New Issue
Block a user