From e064a5c371333d19a5e19aa26f91f99af8954be9 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Thu, 15 Sep 2022 21:41:13 +0200 Subject: [PATCH] Avoid unnecessary copies of parser result The 'result' class has unwrap() and unwrap_err() member functions overloaded for const lvalue and rvalue *this to avoid an unnecessarily copying the to-be unwrapped object of its containing object is going to be discarded anyway. Alas, the parse() function toml/parser.hpp file stored the parse result in a local `const` variable so, although the unwrap call would have been the last use of the object in each case, the unnecessary copy would still be made. This patch removes the `const` and adds a std::move() to actually benefit from the already implemented optimization. --- toml/parser.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index 4d59f0a..590cb2a 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2414,12 +2414,14 @@ parse(std::vector& letters, const std::string& fname) } } - const auto data = detail::parse_toml_file(loc); - if(!data) + if (auto data = detail::parse_toml_file(loc)) { - throw syntax_error(data.unwrap_err(), source_location(loc)); + return std::move(data).unwrap(); + } + else + { + throw syntax_error(std::move(data).unwrap_err(), source_location(loc)); } - return data.unwrap(); } } // detail