minimize extra dv::value copies

This commit is contained in:
MihailRis 2024-09-19 13:45:32 +03:00
parent bd176f24e1
commit 3953583605
14 changed files with 29 additions and 26 deletions

View File

@ -225,7 +225,7 @@ static void read_anim_file(
if (auto found = root.at("frames")) { if (auto found = root.at("frames")) {
auto& frameArr = *found; auto& frameArr = *found;
for (size_t i = 0; i < frameArr.size(); i++) { for (size_t i = 0; i < frameArr.size(); i++) {
auto currentFrame = frameArr[i]; const auto& currentFrame = frameArr[i];
frameName = currentFrame[0].asString(); frameName = currentFrame[0].asString();
if (currentFrame.size() > 1) { if (currentFrame.size() > 1) {

View File

@ -114,7 +114,7 @@ static dv::value value_from_binary(ByteReader& reader) {
case BJSON_TYPE_STRING: case BJSON_TYPE_STRING:
return reader.getString(); return reader.getString();
case BJSON_TYPE_NULL: case BJSON_TYPE_NULL:
return dv::none; return nullptr;
case BJSON_TYPE_BYTES: { case BJSON_TYPE_BYTES: {
int32_t size = reader.getInt32(); int32_t size = reader.getInt32();
if (size < 0) { if (size < 0) {

View File

@ -102,10 +102,10 @@ public:
dv::value read() { dv::value read() {
skipWhitespace(); skipWhitespace();
if (!hasNext()) { if (!hasNext()) {
return root; return std::move(root);
} }
readSection("", root); readSection("", root);
return root; return std::move(root);
} }
}; };

View File

@ -148,7 +148,7 @@ public:
return MISSING; return MISSING;
} }
dv::value getSavedData(size_t index) const { const dv::value& getSavedData(size_t index) const {
return savedData->at(index); return savedData->at(index);
} }

View File

@ -504,8 +504,6 @@ namespace dv {
} }
}; };
inline value none = value();
inline bool is_numeric(const value& val) { inline bool is_numeric(const value& val) {
return val.isInteger() && val.isNumber(); return val.isInteger() && val.isNumber();
} }

View File

@ -477,11 +477,11 @@ dv::value WorldRegions::fetchEntities(int x, int z) {
uint32_t bytesSize; uint32_t bytesSize;
const ubyte* data = getData(x, z, REGION_LAYER_ENTITIES, bytesSize); const ubyte* data = getData(x, z, REGION_LAYER_ENTITIES, bytesSize);
if (data == nullptr) { if (data == nullptr) {
return dv::none; return nullptr;
} }
auto map = json::from_binary(data, bytesSize); auto map = json::from_binary(data, bytesSize);
if (map.size() == 0) { if (map.size() == 0) {
return dv::none; return nullptr;
} }
return map; return map;
} }

View File

@ -136,7 +136,13 @@ public:
break; break;
} }
} }
return Argument {name, type, optional, def, origin, enumname}; return Argument {
std::move(name),
type,
optional,
std::move(def),
std::move(origin),
std::move(enumname)};
} }
Command parseScheme(executor_func executor, std::string_view description) { Command parseScheme(executor_func executor, std::string_view description) {
@ -257,9 +263,9 @@ public:
CommandsInterpreter* interpreter, Argument* arg CommandsInterpreter* interpreter, Argument* arg
) { ) {
if (dv::is_numeric(arg->origin)) { if (dv::is_numeric(arg->origin)) {
return arg->origin; return dv::value(arg->origin);
} else if (arg->origin.getType() == dv::value_type::string) { } else if (arg->origin.getType() == dv::value_type::string) {
return (*interpreter)[arg->origin.asString()]; return dv::value((*interpreter)[arg->origin.asString()]);
} }
return nullptr; return nullptr;
} }
@ -296,7 +302,7 @@ public:
if (origin == nullptr) { if (origin == nullptr) {
return value; return value;
} }
return applyRelative(arg, value, origin); return applyRelative(arg, std::move(value), origin);
} }
inline dv::value performKeywordArg( inline dv::value performKeywordArg(
@ -378,9 +384,9 @@ public:
if (relative) { if (relative) {
value = value =
applyRelative(arg, value, fetchOrigin(interpreter, arg)); applyRelative(arg, std::move(value), fetchOrigin(interpreter, arg));
} }
args.add(value); args.add(std::move(value));
} }
while (auto arg = command->getArgument(arg_index++)) { while (auto arg = command->getArgument(arg_index++)) {
@ -397,7 +403,7 @@ public:
args.add(arg->def); args.add(arg->def);
} }
} }
return Prompt {command, args, kwargs}; return Prompt {command, std::move(args), std::move(kwargs)};
} }
}; };

View File

@ -46,7 +46,7 @@ namespace cmd {
}; };
using executor_func = std::function<dv::value( using executor_func = std::function<dv::value(
CommandsInterpreter*, dv::value args, dv::value kwargs CommandsInterpreter*, const dv::value& args, const dv::value& kwargs
)>; )>;
class Command { class Command {

View File

@ -96,7 +96,7 @@ static void show_content_missing(
contentEntry["type"] = contentName; contentEntry["type"] = contentName;
contentEntry["name"] = entry.name; contentEntry["name"] = entry.name;
} }
menus::show(engine, "reports/missing_content", {root}); menus::show(engine, "reports/missing_content", {std::move(root)});
} }
static bool loadWorldContent(Engine* engine, const fs::path& folder) { static bool loadWorldContent(Engine* engine, const fs::path& folder) {

View File

@ -54,7 +54,7 @@ static int l_spawn(lua::State* L) {
if (lua::gettop(L) > 2) { if (lua::gettop(L) > 2) {
args = lua::tovalue(L, 3); args = lua::tovalue(L, 3);
} }
level->entities->spawn(def, pos, args); level->entities->spawn(def, pos, std::move(args));
return 1; return 1;
} }

View File

@ -340,8 +340,8 @@ void scripting::on_entity_spawn(
const EntityDef&, const EntityDef&,
entityid_t eid, entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components, const std::vector<std::unique_ptr<UserComponent>>& components,
dv::value args, const dv::value& args,
dv::value saved const dv::value& saved
) { ) {
auto L = lua::get_main_thread(); auto L = lua::get_main_thread();
lua::requireglobal(L, STDCOMP); lua::requireglobal(L, STDCOMP);

View File

@ -94,8 +94,8 @@ namespace scripting {
const EntityDef& def, const EntityDef& def,
entityid_t eid, entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components, const std::vector<std::unique_ptr<UserComponent>>& components,
dv::value args, const dv::value& args,
dv::value saved const dv::value& saved
); );
void on_entity_despawn(const Entity& entity); void on_entity_despawn(const Entity& entity);
void on_entity_grounded(const Entity& entity, float force); void on_entity_grounded(const Entity& entity, float force);

View File

@ -173,8 +173,7 @@ entityid_t Entities::spawn(
} }
body.hitbox.position = tsf.pos; body.hitbox.position = tsf.pos;
scripting::on_entity_spawn( scripting::on_entity_spawn(
def, id, scripting.components, std::move(args), std::move(componentsMap) def, id, scripting.components, args, componentsMap);
);
return id; return id;
} }

View File

@ -167,7 +167,7 @@ std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
std::string_view src, std::string_view file, std::string_view name std::string_view src, std::string_view file, std::string_view name
) { ) {
auto root = json::parse(file, src); auto root = json::parse(file, src);
auto rootNodeMap = root["root"]; const auto& rootNodeMap = root["root"];
auto [count, rootNode] = read_node(rootNodeMap, 0); auto [count, rootNode] = read_node(rootNodeMap, 0);
return std::make_unique<SkeletonConfig>( return std::make_unique<SkeletonConfig>(
std::string(name), std::move(rootNode), count std::string(name), std::move(rootNode), count