You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by ha...@apache.org on 2018/10/03 18:42:11 UTC

[incubator-mxnet] branch master updated: [MXNET-860] Use emplace where helpful (#12694)

This is an automated email from the ASF dual-hosted git repository.

haibin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new bcd24f8  [MXNET-860] Use emplace where helpful (#12694)
bcd24f8 is described below

commit bcd24f85457821f9c0ce17d60e545a252a87a5ae
Author: Kellen Sunderland <ke...@gmail.com>
AuthorDate: Wed Oct 3 11:41:45 2018 -0700

    [MXNET-860] Use emplace where helpful (#12694)
    
    * [MXNET-860] Use emplace where helpful
    
    * [MXNET-860] Add emplace as an error in clang-tidy
---
 .clang-tidy                          |  3 ++-
 src/c_api/c_api_executor.cc          |  2 +-
 src/c_api/c_api_ndarray.cc           |  2 +-
 src/c_api/c_predict_api.cc           |  4 ++--
 src/io/iter_mnist.cc                 |  6 +++---
 src/kvstore/comm_tree.h              |  8 ++++----
 src/nnvm/legacy_json_util.cc         |  2 +-
 src/nnvm/legacy_op_util.cc           |  3 +--
 src/operator/control_flow.cc         | 12 ++++++------
 src/operator/custom/custom.cc        |  2 +-
 src/operator/custom/native_op-inl.h  |  4 ++--
 src/operator/custom/ndarray_op-inl.h |  4 ++--
 src/operator/optimizer_op.cc         |  2 +-
 src/operator/rnn-inl.h               |  4 ++--
 14 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/.clang-tidy b/.clang-tidy
index af99026..0080662 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -54,7 +54,8 @@ Checks: >
 
 # In order to trigger an error, you must have a rule defined both in checks and in this section.
 WarningsAsErrors: >
-    cppcoreguidelines-no-malloc, modernize-use-nullptr, performance-unnecessary-copy-initialization
+    cppcoreguidelines-no-malloc, modernize-use-nullptr, performance-unnecessary-copy-initialization,
+    modernize-use-emplace
 
 # Todo: define a better regex match that includes most project headers, but excludes third party
 # code.
diff --git a/src/c_api/c_api_executor.cc b/src/c_api/c_api_executor.cc
index c3a6473..1f936b1 100644
--- a/src/c_api/c_api_executor.cc
+++ b/src/c_api/c_api_executor.cc
@@ -168,7 +168,7 @@ int MXExecutorBindEX(SymbolHandle symbol_handle,
   for (mx_uint i = 0; i < len; ++i) {
     in_args_vec.push_back(*(in_args_ptr[i]));
     if (arg_grad_ptr[i] == nullptr) {
-      arg_grad_vec.push_back(NDArray());
+      arg_grad_vec.emplace_back();
       grad_req_vec.push_back(kNullOp);
     } else {
       arg_grad_vec.push_back(*(arg_grad_ptr[i]));
diff --git a/src/c_api/c_api_ndarray.cc b/src/c_api/c_api_ndarray.cc
index 34bd4b2..18f6c41 100644
--- a/src/c_api/c_api_ndarray.cc
+++ b/src/c_api/c_api_ndarray.cc
@@ -176,7 +176,7 @@ int MXCreateCachedOpEx(SymbolHandle handle,
   API_BEGIN();
   std::vector<std::pair<std::string, std::string> > flags;
   for (int i = 0; i < num_flags; ++i) {
-    flags.push_back({keys[i], vals[i]});
+    flags.emplace_back(keys[i], vals[i]);
   }
   *out = new CachedOpPtr(new CachedOp(*sym, flags));
   API_END();
diff --git a/src/c_api/c_predict_api.cc b/src/c_api/c_predict_api.cc
index 24358e4..cae5c68 100644
--- a/src/c_api/c_predict_api.cc
+++ b/src/c_api/c_predict_api.cc
@@ -191,7 +191,7 @@ int _CreatePartialOut(const char* symbol_json_str,
       if (known_shape.count(key) != 0) {
         in_shapes.push_back(known_shape[key]);
       } else {
-        in_shapes.push_back(TShape());
+        in_shapes.emplace_back();
       }
     }
     nnvm::Graph g; g.outputs = sym.outputs;
@@ -369,7 +369,7 @@ int MXPredReshape(mx_uint num_input_nodes,
       if (new_shape.count(key) != 0) {
         in_shapes.push_back(new_shape[key]);
       } else {
-        in_shapes.push_back(TShape());
+        in_shapes.emplace_back();
       }
     }
     nnvm::Graph g; g.outputs = ret->sym.outputs;
diff --git a/src/io/iter_mnist.cc b/src/io/iter_mnist.cc
index 4022347..139cf47 100644
--- a/src/io/iter_mnist.cc
+++ b/src/io/iter_mnist.cc
@@ -124,11 +124,11 @@ class MNISTIter: public IIterator<TBlobBatch> {
       batch_label_.dptr_ = &labels_[loc_];
       out_.data.clear();
       if (param_.flat) {
-          out_.data.push_back(TBlob(batch_data_.FlatTo2D()));
+          out_.data.emplace_back(batch_data_.FlatTo2D());
       } else {
-          out_.data.push_back(TBlob(batch_data_));
+          out_.data.emplace_back(batch_data_);
       }
-      out_.data.push_back(TBlob(batch_label_));
+      out_.data.emplace_back(batch_label_);
       loc_ += param_.batch_size;
       return true;
     } else {
diff --git a/src/kvstore/comm_tree.h b/src/kvstore/comm_tree.h
index 1ebfcdc..e857f33 100644
--- a/src/kvstore/comm_tree.h
+++ b/src/kvstore/comm_tree.h
@@ -395,7 +395,7 @@ class CommDeviceTree : public CommDevice {
     // 2) Force copy_buf to be of kRecvBufferSize
     // 3) Do not use greedy assignment; all keys are assigned to each GPU
     for (unsigned i = 0; i < devs_.size(); ++i)
-      tree_merge_buf_.push_back(std::unordered_map<int, TreeBufferEntry>());
+      tree_merge_buf_.emplace_back();
 
     bool delay_alloc = true;
     std::map<int, int> key_dist;
@@ -457,7 +457,7 @@ class CommDeviceTree : public CommDevice {
               if (row == devs_.size()-1)
                 shape_copy[0] = last_slice;
               buf.merged[row] = NDArray(shape_copy, ctx, delay_alloc, type);
-              buf.copy_buf.push_back(std::vector<NDArray>());
+              buf.copy_buf.emplace_back();
               if (buf.copy_buf[row].empty()) {
                 buf.copy_buf[row].resize(kBranch-1);
                 for (size_t col = 0; col < buf.copy_buf[0].size(); ++col) {
@@ -469,9 +469,9 @@ class CommDeviceTree : public CommDevice {
               }
             }
           } else {
-            buf.merged.push_back(NDArray(shape, ctx, false, type));
+            buf.merged.emplace_back(shape, ctx, false, type);
             if (buf.copy_buf.empty()) {
-              buf.copy_buf.push_back(std::vector<NDArray>());
+              buf.copy_buf.emplace_back();
               buf.copy_buf[0].resize(kBranch-1);
               for (size_t col = 0; col < buf.copy_buf[0].size(); ++col) {
                 buf.copy_buf[0][col] = NDArray(buf.merged[0].shape(),
diff --git a/src/nnvm/legacy_json_util.cc b/src/nnvm/legacy_json_util.cc
index 935a64c..0697aeb 100644
--- a/src/nnvm/legacy_json_util.cc
+++ b/src/nnvm/legacy_json_util.cc
@@ -60,7 +60,7 @@ Graph UpgradeJSON_FixParsing(Graph g) {
         for (const auto key : kHiddenKeys) {
           size_t pos = it->first.rfind(key);
           if (pos == 0 || (pos != std::string::npos && pos == it->first.length() - key.length())) {
-            hidden_keys.push_back(*it);
+            hidden_keys.emplace_back(*it);
             erase = true;
             break;
           }
diff --git a/src/nnvm/legacy_op_util.cc b/src/nnvm/legacy_op_util.cc
index 4260e68..63be619 100644
--- a/src/nnvm/legacy_op_util.cc
+++ b/src/nnvm/legacy_op_util.cc
@@ -288,8 +288,7 @@ std::vector<std::pair<int, int> > OpPropInplaceOption(const NodeAttrs& attrs) {
   }
   std::vector<std::pair<int, int> > forward_inplace;
   for (auto& kv : prop.ptr->ForwardInplaceOption(in_data, out_addr)) {
-    forward_inplace.push_back(
-        std::make_pair(kv.first, *static_cast<int*>(kv.second)));
+    forward_inplace.emplace_back(kv.first, *static_cast<int*>(kv.second));
   }
   return forward_inplace;
 }
diff --git a/src/operator/control_flow.cc b/src/operator/control_flow.cc
index ba7f5c0..25c8f70 100644
--- a/src/operator/control_flow.cc
+++ b/src/operator/control_flow.cc
@@ -1268,7 +1268,7 @@ NNVM_REGISTER_OP(_foreach)
     [](const NodeAttrs& attrs) {
   const ForeachParam& params = nnvm::get<ForeachParam>(attrs.parsed);
   std::vector<std::string> names;
-  names.push_back("fn");
+  names.emplace_back("fn");
   for (int i = 0; i < params.num_args - 1; i++)
     names.push_back("data" + std::to_string(i));
   return names;
@@ -1330,8 +1330,8 @@ NNVM_REGISTER_OP(_while_loop)
   const WhileLoopParam& params = nnvm::get<WhileLoopParam>(attrs.parsed);
   std::vector<std::string> names;
   names.reserve(params.num_args);
-  names.push_back("cond");
-  names.push_back("func");
+  names.emplace_back("cond");
+  names.emplace_back("func");
   for (int i = 2; i < params.num_args; i++)
     names.push_back("data" + std::to_string(i - 2));
   return names;
@@ -1392,9 +1392,9 @@ NNVM_REGISTER_OP(_cond)
   const CondParam& params = nnvm::get<CondParam>(attrs.parsed);
   std::vector<std::string> names;
   names.reserve(params.num_args);
-  names.push_back("cond");
-  names.push_back("then_branch");
-  names.push_back("else_branch");
+  names.emplace_back("cond");
+  names.emplace_back("then_branch");
+  names.emplace_back("else_branch");
   for (int i = 3; i < params.num_args; ++i)
     names.push_back("data" + std::to_string(i - 3));
   return names;
diff --git a/src/operator/custom/custom.cc b/src/operator/custom/custom.cc
index 4cda137..2061324 100644
--- a/src/operator/custom/custom.cc
+++ b/src/operator/custom/custom.cc
@@ -81,7 +81,7 @@ std::vector<std::string> List(const NodeAttrs& attrs) {
       &args, params.info->contexts[Type]));
   std::vector<std::string> ret;
   for (int i = 0; args[i] != nullptr; ++i) {
-    ret.push_back(args[i]);
+    ret.emplace_back(args[i]);
   }
   return ret;
 }
diff --git a/src/operator/custom/native_op-inl.h b/src/operator/custom/native_op-inl.h
index d2fb114..3c22207 100644
--- a/src/operator/custom/native_op-inl.h
+++ b/src/operator/custom/native_op-inl.h
@@ -185,7 +185,7 @@ class NativeOpProp : public OperatorProperty {
     param_.pinfo->list_arguments(&args, param_.pinfo->p_list_arguments);
     std::vector<std::string> ret;
     for (int i = 0; args[i] != NULL; ++i) {
-      ret.push_back(args[i]);
+      ret.emplace_back(args[i]);
     }
     return ret;
   }
@@ -195,7 +195,7 @@ class NativeOpProp : public OperatorProperty {
     param_.pinfo->list_outputs(&args, param_.pinfo->p_list_outputs);
     std::vector<std::string> ret;
     for (int i = 0; args[i] != NULL; ++i) {
-      ret.push_back(args[i]);
+      ret.emplace_back(args[i]);
     }
     return ret;
   }
diff --git a/src/operator/custom/ndarray_op-inl.h b/src/operator/custom/ndarray_op-inl.h
index 20624d2..02c5630 100644
--- a/src/operator/custom/ndarray_op-inl.h
+++ b/src/operator/custom/ndarray_op-inl.h
@@ -87,7 +87,7 @@ class NDArrayOpProp : public OperatorProperty {
     CHECK(param_.pinfo->list_arguments(&args, param_.pinfo->p_list_arguments));
     std::vector<std::string> ret;
     for (int i = 0; args[i] != NULL; ++i) {
-      ret.push_back(args[i]);
+      ret.emplace_back(args[i]);
     }
     return ret;
   }
@@ -97,7 +97,7 @@ class NDArrayOpProp : public OperatorProperty {
     CHECK(param_.pinfo->list_outputs(&args, param_.pinfo->p_list_outputs));
     std::vector<std::string> ret;
     for (int i = 0; args[i] != NULL; ++i) {
-      ret.push_back(args[i]);
+      ret.emplace_back(args[i]);
     }
     return ret;
   }
diff --git a/src/operator/optimizer_op.cc b/src/operator/optimizer_op.cc
index cf126ed..6c44f99 100644
--- a/src/operator/optimizer_op.cc
+++ b/src/operator/optimizer_op.cc
@@ -384,7 +384,7 @@ only the row slices whose indices appear in grad.indices are updated (for both w
   [](const NodeAttrs& attrs, const int dev_mask, const DispatchMode dispatch_mode) {
     std::vector<ResourceRequest> request;
     if (dispatch_mode == DispatchMode::kFComputeEx) {
-      request.push_back(ResourceRequest::kTempSpace);
+      request.emplace_back(ResourceRequest::kTempSpace);
     }
     return request;
   })
diff --git a/src/operator/rnn-inl.h b/src/operator/rnn-inl.h
index 9211f6a..3901a80 100644
--- a/src/operator/rnn-inl.h
+++ b/src/operator/rnn-inl.h
@@ -608,9 +608,9 @@ class RNNProp : public OperatorProperty {
     if (!param_.state_outputs)
       return outputs;
     else
-      outputs.push_back("state");
+      outputs.emplace_back("state");
     if (param_.mode == rnn_enum::kLstm)
-      outputs.push_back("state_cell");
+      outputs.emplace_back("state_cell");
     return outputs;
   }