Skip to content

Commit b5093e8

Browse files
committed
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
1 parent 1fc3de7 commit b5093e8

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace Json {
5454
*/
5555
class JSON_API Exception : public std::exception {
5656
public:
57-
Exception(JSONCPP_STRING const& msg);
57+
Exception(JSONCPP_STRING msg);
5858
~Exception() JSONCPP_NOEXCEPT override;
5959
char const* what() const JSONCPP_NOEXCEPT override;
6060

include/json/writer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
300300
/**
301301
* \param indentation Each level will be indented by this amount extra.
302302
*/
303-
StyledStreamWriter(const JSONCPP_STRING& indentation = "\t");
304-
~StyledStreamWriter() {}
303+
StyledStreamWriter(JSONCPP_STRING indentation = "\t");
304+
~StyledStreamWriter() = default;
305305

306306
public:
307307
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.

src/lib_json/json_reader.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
8888
// //////////////////////////////////////////////////////////////////
8989

9090
Reader::Reader()
91-
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
92-
lastValue_(), commentsBefore_(), features_(Features::all()),
93-
collectComments_() {}
91+
: errors_(), document_(), commentsBefore_(), features_(Features::all())
92+
{}
9493

9594
Reader::Reader(const Features& features)
9695
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),

src/lib_json/json_value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static inline void releaseStringValue(char* value, unsigned) { free(value); }
213213

214214
namespace Json {
215215

216-
Exception::Exception(JSONCPP_STRING const& msg) : msg_(msg) {}
216+
Exception::Exception(JSONCPP_STRING msg) : msg_(std::move(msg)) {}
217217
Exception::~Exception() JSONCPP_NOEXCEPT {}
218218
char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); }
219219
RuntimeError::RuntimeError(JSONCPP_STRING const& msg) : Exception(msg) {}

src/lib_json/json_writer.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,8 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
642642
// Class StyledStreamWriter
643643
// //////////////////////////////////////////////////////////////////
644644

645-
StyledStreamWriter::StyledStreamWriter(const JSONCPP_STRING& indentation)
646-
: document_(nullptr), rightMargin_(74), indentation_(indentation),
645+
StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation)
646+
: document_(nullptr), indentation_(std::move(indentation)),
647647
addChildValues_(), indented_(false) {}
648648

649649
void StyledStreamWriter::write(JSONCPP_OSTREAM& out, const Value& root) {
@@ -872,11 +872,11 @@ struct CommentStyle {
872872
};
873873

874874
struct BuiltStyledStreamWriter : public StreamWriter {
875-
BuiltStyledStreamWriter(JSONCPP_STRING const& indentation,
875+
BuiltStyledStreamWriter(JSONCPP_STRING indentation,
876876
CommentStyle::Enum cs,
877-
JSONCPP_STRING const& colonSymbol,
878-
JSONCPP_STRING const& nullSymbol,
879-
JSONCPP_STRING const& endingLineFeedSymbol,
877+
JSONCPP_STRING colonSymbol,
878+
JSONCPP_STRING nullSymbol,
879+
JSONCPP_STRING endingLineFeedSymbol,
880880
bool useSpecialFloats,
881881
unsigned int precision,
882882
PrecisionType precisionType);
@@ -912,17 +912,17 @@ struct BuiltStyledStreamWriter : public StreamWriter {
912912
PrecisionType precisionType_;
913913
};
914914
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
915-
JSONCPP_STRING const& indentation,
915+
JSONCPP_STRING indentation,
916916
CommentStyle::Enum cs,
917-
JSONCPP_STRING const& colonSymbol,
918-
JSONCPP_STRING const& nullSymbol,
919-
JSONCPP_STRING const& endingLineFeedSymbol,
917+
JSONCPP_STRING colonSymbol,
918+
JSONCPP_STRING nullSymbol,
919+
JSONCPP_STRING endingLineFeedSymbol,
920920
bool useSpecialFloats,
921921
unsigned int precision,
922922
PrecisionType precisionType)
923-
: rightMargin_(74), indentation_(indentation), cs_(cs),
924-
colonSymbol_(colonSymbol), nullSymbol_(nullSymbol),
925-
endingLineFeedSymbol_(endingLineFeedSymbol), addChildValues_(false),
923+
: rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
924+
colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
925+
endingLineFeedSymbol_(std::move(endingLineFeedSymbol)), addChildValues_(false),
926926
indented_(false), useSpecialFloats_(useSpecialFloats),
927927
precision_(precision), precisionType_(precisionType) {}
928928
int BuiltStyledStreamWriter::write(Value const& root, JSONCPP_OSTREAM* sout) {

0 commit comments

Comments
 (0)