From b5093e8122acd97d19267be1b6798c7232fae371 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Mon, 14 Jan 2019 17:09:19 -0600 Subject: [PATCH] PERF: Allow compiler to choose best way to construct a copy With move semantics added to the language and the standard library updated with move constructors added for many types it is now interesting to take an argument directly by value, instead of by const-reference, and then copy. This check allows the compiler to take care of choosing the best way to construct the copy. The transformation is usually beneficial when the calling code passes an rvalue and assumes the move construction is a cheap operation. This short example illustrates how the construction of the value happens: SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/ run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-pass-by-value -header-filter=.* -fix --- include/json/value.h | 2 +- include/json/writer.h | 4 ++-- src/lib_json/json_reader.cpp | 5 ++--- src/lib_json/json_value.cpp | 2 +- src/lib_json/json_writer.cpp | 26 +++++++++++++------------- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 3fdd884..71bc65c 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -54,7 +54,7 @@ namespace Json { */ class JSON_API Exception : public std::exception { public: - Exception(JSONCPP_STRING const& msg); + Exception(JSONCPP_STRING msg); ~Exception() JSONCPP_NOEXCEPT override; char const* what() const JSONCPP_NOEXCEPT override; diff --git a/include/json/writer.h b/include/json/writer.h index 7faaa02..34e71ed 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -300,8 +300,8 @@ public: /** * \param indentation Each level will be indented by this amount extra. */ - StyledStreamWriter(const JSONCPP_STRING& indentation = "\t"); - ~StyledStreamWriter() {} + StyledStreamWriter(JSONCPP_STRING indentation = "\t"); + ~StyledStreamWriter() = default; public: /** \brief Serialize a Value in JSON format. diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index ce4aac4..c5af014 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -88,9 +88,8 @@ bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) { // ////////////////////////////////////////////////////////////////// Reader::Reader() - : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(), - lastValue_(), commentsBefore_(), features_(Features::all()), - collectComments_() {} + : errors_(), document_(), commentsBefore_(), features_(Features::all()) + {} Reader::Reader(const Features& features) : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(), diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index ef95117..2445d31 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -213,7 +213,7 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); } namespace Json { -Exception::Exception(JSONCPP_STRING const& msg) : msg_(msg) {} +Exception::Exception(JSONCPP_STRING msg) : msg_(std::move(msg)) {} Exception::~Exception() JSONCPP_NOEXCEPT {} char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); } RuntimeError::RuntimeError(JSONCPP_STRING const& msg) : Exception(msg) {} diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 993001c..54fcb4f 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -642,8 +642,8 @@ bool StyledWriter::hasCommentForValue(const Value& value) { // Class StyledStreamWriter // ////////////////////////////////////////////////////////////////// -StyledStreamWriter::StyledStreamWriter(const JSONCPP_STRING& indentation) - : document_(nullptr), rightMargin_(74), indentation_(indentation), +StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation) + : document_(nullptr), indentation_(std::move(indentation)), addChildValues_(), indented_(false) {} void StyledStreamWriter::write(JSONCPP_OSTREAM& out, const Value& root) { @@ -872,11 +872,11 @@ struct CommentStyle { }; struct BuiltStyledStreamWriter : public StreamWriter { - BuiltStyledStreamWriter(JSONCPP_STRING const& indentation, + BuiltStyledStreamWriter(JSONCPP_STRING indentation, CommentStyle::Enum cs, - JSONCPP_STRING const& colonSymbol, - JSONCPP_STRING const& nullSymbol, - JSONCPP_STRING const& endingLineFeedSymbol, + JSONCPP_STRING colonSymbol, + JSONCPP_STRING nullSymbol, + JSONCPP_STRING endingLineFeedSymbol, bool useSpecialFloats, unsigned int precision, PrecisionType precisionType); @@ -912,17 +912,17 @@ private: PrecisionType precisionType_; }; BuiltStyledStreamWriter::BuiltStyledStreamWriter( - JSONCPP_STRING const& indentation, + JSONCPP_STRING indentation, CommentStyle::Enum cs, - JSONCPP_STRING const& colonSymbol, - JSONCPP_STRING const& nullSymbol, - JSONCPP_STRING const& endingLineFeedSymbol, + JSONCPP_STRING colonSymbol, + JSONCPP_STRING nullSymbol, + JSONCPP_STRING endingLineFeedSymbol, bool useSpecialFloats, unsigned int precision, PrecisionType precisionType) - : rightMargin_(74), indentation_(indentation), cs_(cs), - colonSymbol_(colonSymbol), nullSymbol_(nullSymbol), - endingLineFeedSymbol_(endingLineFeedSymbol), addChildValues_(false), + : rightMargin_(74), indentation_(std::move(indentation)), cs_(cs), + colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)), + endingLineFeedSymbol_(std::move(endingLineFeedSymbol)), addChildValues_(false), indented_(false), useSpecialFloats_(useSpecialFloats), precision_(precision), precisionType_(precisionType) {} int BuiltStyledStreamWriter::write(Value const& root, JSONCPP_OSTREAM* sout) {