Skip to content

Commit

Permalink
PERF: Allow compiler to choose best way to construct a copy
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hjmjohnson committed Jan 16, 2019
1 parent 1fc3de7 commit b5093e8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
/**
* \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 <a HREF="http://www.json.org">JSON</a> format.
Expand Down
5 changes: 2 additions & 3 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
26 changes: 13 additions & 13 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -912,17 +912,17 @@ struct BuiltStyledStreamWriter : public StreamWriter {
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) {
Expand Down

0 comments on commit b5093e8

Please sign in to comment.