Skip to content

Commit

Permalink
Remove unnecessary copies of value parameters.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 155511618
  • Loading branch information
hawkinsp authored and tensorflower-gardener committed May 10, 2017
1 parent b9845c6 commit 1d0b8c0
Show file tree
Hide file tree
Showing 72 changed files with 225 additions and 165 deletions.
3 changes: 2 additions & 1 deletion tensorflow/cc/client/client_session.cc
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
#include "tensorflow/cc/client/client_session.h"

#include <unordered_map>
#include <utility>
#include <vector>

#include "tensorflow/core/platform/env.h"
Expand All @@ -31,7 +32,7 @@ class ClientSession::Impl {
friend class ClientSession;

Impl(Session* session, std::shared_ptr<Graph> graph)
: session_(session), graph_(graph) {}
: session_(session), graph_(std::move(graph)) {}

static SessionOptions MakeDefaultSessionOptions(const string& target);
Status MaybeExtendGraph() const;
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/cc/framework/cc_op_gen.cc
Expand Up @@ -198,7 +198,7 @@ string PrintTensorProto(const TensorProto& proto) {
").AsTensorProto()");
}

string PrintAttrValue(string op, const AttrValue& attr_value) {
string PrintAttrValue(const string& op, const AttrValue& attr_value) {
switch (attr_value.value_case()) {
case AttrValue::kS:
return PrintString(attr_value.s());
Expand Down
3 changes: 2 additions & 1 deletion tensorflow/cc/framework/cc_ops_test.cc
Expand Up @@ -32,7 +32,8 @@ Output Linear(const Scope& scope, Input x, Input w, Input b) {
return BiasAdd(cop_scopes.last, m, b);
}

void GetColocationConstraints(Output tensor, std::vector<string>* constraints) {
void GetColocationConstraints(const Output& tensor,
std::vector<string>* constraints) {
constraints->clear();
TF_EXPECT_OK(
GetNodeAttr(tensor.op().node()->def(), kColocationAttrName, constraints));
Expand Down
6 changes: 3 additions & 3 deletions tensorflow/cc/gradients/math_grad_test.cc
Expand Up @@ -56,9 +56,9 @@ class CWiseUnaryGradTest : public ::testing::Test {
ATAN
};

void TestCWiseGrad(UnaryOpType op_type, std::function<float(int)> x_fn,
std::function<float(float)> dy_fn,
std::function<float(float, float)> dx_fn) {
void TestCWiseGrad(UnaryOpType op_type, const std::function<float(int)>& x_fn,
const std::function<float(float)>& dy_fn,
const std::function<float(float, float)>& dx_fn) {
Tensor x(DT_FLOAT, {2, 3, 2});
auto x_flat = x.flat<float>();
for (int i = 0; i < x_flat.size(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/compiler/aot/tfcompile_util_test.cc
Expand Up @@ -24,7 +24,7 @@ namespace tensorflow {
namespace tfcompile {
namespace {

void ExpectErrorContains(Status status, StringPiece str) {
void ExpectErrorContains(const Status& status, StringPiece str) {
EXPECT_NE(Status::OK(), status);
EXPECT_TRUE(StringPiece(status.error_message()).contains(str))
<< "expected error: " << status.error_message() << " to contain: " << str;
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/compiler/jit/encapsulate_subgraphs_pass_test.cc
Expand Up @@ -109,7 +109,7 @@ Node* Binary(ops::NodeOut a, ops::NodeOut b,
return ops::BinaryOp("BinaryTest", a, b, opts);
}

Node* AddNLike(std::vector<ops::NodeOut> inputs,
Node* AddNLike(const std::vector<ops::NodeOut>& inputs,
const GraphDefBuilder::Options& opts) {
if (opts.HaveError()) return nullptr;
NodeBuilder node_builder(opts.GetNameForOp("AddN"), "AddNLikeTest",
Expand Down
15 changes: 9 additions & 6 deletions tensorflow/compiler/jit/mark_for_compilation_pass.cc
Expand Up @@ -52,14 +52,16 @@ bool HasXLAKernel(const Node& node, const DeviceType& jit_device_type) {
// Make sure we don't recurse infinitely on recursive functions.
const int kMaxRecursionDepth = 10;

bool IsCompilableCall(const NodeDef& call_def, DeviceType jit_device_type,
int depth, FunctionLibraryRuntime* lib_runtime);
bool IsCompilableCall(const NodeDef& call_def,
const DeviceType& jit_device_type, int depth,
FunctionLibraryRuntime* lib_runtime);

// Tests whether 'while_def' is a completely compilable loop.
// Every operator in the condition and body functions must be compilable for a
// while loop to be compilable.
bool IsCompilableWhile(const NodeDef& while_def, DeviceType jit_device_type,
int depth, FunctionLibraryRuntime* lib_runtime) {
bool IsCompilableWhile(const NodeDef& while_def,
const DeviceType& jit_device_type, int depth,
FunctionLibraryRuntime* lib_runtime) {
VLOG(2) << "Loop marking: " << while_def.op();

const NameAttrList* name_attr;
Expand Down Expand Up @@ -98,8 +100,9 @@ bool IsCompilableWhile(const NodeDef& while_def, DeviceType jit_device_type,
// Tests whether 'call_def' is a call to a completely compilable function.
// Every operator in the function must be compilable for a function to be
// compilable.
bool IsCompilableCall(const NodeDef& call_def, DeviceType jit_device_type,
int depth, FunctionLibraryRuntime* lib_runtime) {
bool IsCompilableCall(const NodeDef& call_def,
const DeviceType& jit_device_type, int depth,
FunctionLibraryRuntime* lib_runtime) {
VLOG(2) << "Function marking: " << call_def.op();

if (depth > kMaxRecursionDepth) {
Expand Down
18 changes: 9 additions & 9 deletions tensorflow/compiler/tests/randomized_tests.cc
Expand Up @@ -94,7 +94,7 @@ class OpTestBuilder {
explicit OpTestBuilder(const string& op_name);

// Adds an input 'tensor'.
OpTestBuilder& Input(Tensor tensor);
OpTestBuilder& Input(const Tensor& tensor);

// Sets an attribute.
template <class T>
Expand All @@ -111,8 +111,8 @@ class OpTestBuilder {
// sets it to the NodeDef of the operator under test. Fills 'inputs' and
// 'outputs' with the names of the input placeholder nodes and the output
// identity nodes, respectively.
Status BuildGraph(string name_prefix, string device, bool use_jit,
GraphDef* graphdef, NodeDef** test_node_def,
Status BuildGraph(const string& name_prefix, const string& device,
bool use_jit, GraphDef* graphdef, NodeDef** test_node_def,
std::vector<string>* inputs,
std::vector<string>* outputs) const;

Expand All @@ -127,7 +127,7 @@ OpTestBuilder::OpTestBuilder(const string& op_name) {
node_def_.set_op(op_name);
}

OpTestBuilder& OpTestBuilder::Input(Tensor tensor) {
OpTestBuilder& OpTestBuilder::Input(const Tensor& tensor) {
VLOG(1) << "Adding input: " << tensor.DebugString();
inputs_.push_back(tensor);
return *this;
Expand All @@ -146,9 +146,9 @@ OpTestBuilder& OpTestBuilder::Attr(StringPiece attr_name,
return *this;
}

Status OpTestBuilder::BuildGraph(string name_prefix, string device,
bool use_jit, GraphDef* graphdef,
NodeDef** test_node_def,
Status OpTestBuilder::BuildGraph(const string& name_prefix,
const string& device, bool use_jit,
GraphDef* graphdef, NodeDef** test_node_def,
std::vector<string>* inputs,
std::vector<string>* outputs) const {
OpRegistryInterface* op_registry = OpRegistry::Global();
Expand Down Expand Up @@ -209,7 +209,7 @@ class OpTest : public ::testing::Test {

// Runs 'fn' up to --tf_xla_test_repetitions times, or until a failure occurs;
// whichever happens first.
void Repeatedly(std::function<void(void)> fn);
void Repeatedly(const std::function<void(void)>& fn);

// Select a random element from 'candidates'.
template <typename T>
Expand Down Expand Up @@ -315,7 +315,7 @@ OpTest::OpTest() {
TF_CHECK_OK(session_->Create(def));
}

void OpTest::Repeatedly(std::function<void(void)> fn) {
void OpTest::Repeatedly(const std::function<void(void)>& fn) {
int const max_repetitions = tf_xla_test_repetitions;
for (int i = 0; !HasFailure() && i < max_repetitions; ++i) {
fn();
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/compiler/xla/service/hlo_instruction.cc
Expand Up @@ -1083,7 +1083,7 @@ bool HloInstruction::Identical(
// general, there is no need to check shape because shape is inferred from the
// shape of the operands.
if (opcode() != other.opcode() ||
!ContainersEqual(operands(), other.operands(), eq_operands)) {
!ContainersEqual(operands(), other.operands(), std::move(eq_operands))) {
return false;
}

Expand Down
9 changes: 5 additions & 4 deletions tensorflow/compiler/xla/shape_util.cc
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <vector>

#include "tensorflow/compiler/xla/index_util.h"
Expand Down Expand Up @@ -675,7 +676,7 @@ namespace {
// Helper for ForEachSubshape which visits the subshapes of the given shape in
// DFS pre-order starting with the index.
Status ForEachSubshapeHelper(const Shape& shape,
const ShapeUtil::VisitorFunction func,
const ShapeUtil::VisitorFunction& func,
ShapeIndex* index) {
TF_RETURN_IF_ERROR(func(shape, *index));
if (ShapeUtil::IsTuple(shape)) {
Expand All @@ -692,7 +693,7 @@ Status ForEachSubshapeHelper(const Shape& shape,
// Helper for ForEachMutableSubshape which visits the subshapes of the given
// shape in DFS pre-order starting with the index.
Status ForEachMutableSubshapeHelper(
Shape* shape, const ShapeUtil::MutatingVisitorFunction func,
Shape* shape, const ShapeUtil::MutatingVisitorFunction& func,
ShapeIndex* index) {
TF_RETURN_IF_ERROR(func(shape, *index));
if (ShapeUtil::IsTuple(*shape)) {
Expand All @@ -709,13 +710,13 @@ Status ForEachMutableSubshapeHelper(
} // namespace

/* static */ Status ShapeUtil::ForEachSubshape(const Shape& shape,
VisitorFunction func) {
const VisitorFunction& func) {
ShapeIndex index;
return ForEachSubshapeHelper(shape, func, &index);
}

/* static */ Status ShapeUtil::ForEachMutableSubshape(
Shape* shape, MutatingVisitorFunction func) {
Shape* shape, const MutatingVisitorFunction& func) {
ShapeIndex index;
return ForEachMutableSubshapeHelper(shape, func, &index);
}
Expand Down
5 changes: 3 additions & 2 deletions tensorflow/compiler/xla/shape_util.h
Expand Up @@ -299,13 +299,14 @@ class ShapeUtil {
// pre-order starting with the entire shape (index {}).
using VisitorFunction = std::function<Status(const Shape& /*subshape*/,
const ShapeIndex& /*index*/)>;
static Status ForEachSubshape(const Shape& shape, VisitorFunction func);
static Status ForEachSubshape(const Shape& shape,
const VisitorFunction& func);

// Mutating variant of ForEachSubshape.
using MutatingVisitorFunction =
std::function<Status(Shape* /*subshape*/, const ShapeIndex& /*index*/)>;
static Status ForEachMutableSubshape(Shape* shape,
MutatingVisitorFunction func);
const MutatingVisitorFunction& func);

// Removes all degenerate dimensions (size one) from the given shape. The
// stripped minor_to_major preserves the relative ordering of non-degenerate
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/compiler/xla/status_macros_test.cc
Expand Up @@ -73,7 +73,7 @@ Status ReturnStatusError() { return (tensorflow::errors::Internal("foobar")); }

using StatusReturningFunction = std::function<Status()>;

StatusOr<int> CallStatusReturningFunction(StatusReturningFunction func) {
StatusOr<int> CallStatusReturningFunction(const StatusReturningFunction& func) {
TF_RETURN_IF_ERROR(func());
return 42;
}
Expand Down
6 changes: 3 additions & 3 deletions tensorflow/compiler/xla/tests/dynamic_ops_test.cc
Expand Up @@ -109,7 +109,7 @@ class DynamicSliceTest : public ClientLibraryTestBase {
template <typename IndexT>
void RunR1(const std::vector<float>& input_values,
const std::vector<IndexT> slice_starts,
const std::vector<int64> slice_sizes,
const std::vector<int64>& slice_sizes,
const std::vector<float>& expected_values) {
ComputationBuilder builder(client_, TestName());
// Initialize and transfer dynamic slice start indices parameter.
Expand All @@ -127,7 +127,7 @@ class DynamicSliceTest : public ClientLibraryTestBase {
template <typename IndexT>
void RunR2(const Array2D<float>& input_values,
const std::vector<IndexT> slice_starts,
const std::vector<int64> slice_sizes,
const std::vector<int64>& slice_sizes,
const Array2D<float>& expected_values) {
ComputationBuilder builder(client_, TestName());
// Initialize and transfer dynamic slice start indices parameter.
Expand All @@ -145,7 +145,7 @@ class DynamicSliceTest : public ClientLibraryTestBase {
template <typename IndexT>
void RunR3(const Array3D<float>& input_values,
const std::vector<IndexT> slice_starts,
const std::vector<int64> slice_sizes,
const std::vector<int64>& slice_sizes,
const Array3D<float>& expected_values) {
ComputationBuilder builder(client_, TestName());
// Initialize and transfer dynamic slice start indices parameter.
Expand Down
6 changes: 3 additions & 3 deletions tensorflow/compiler/xla/tests/reduce_window_test.cc
Expand Up @@ -43,7 +43,7 @@ class ReduceWindowTest : public ClientLibraryTestBase {
public:
ReduceWindowTest() : builder_(client_, TestName()) {}

void ReduceWindowAdd(ComputationDataHandle input,
void ReduceWindowAdd(const ComputationDataHandle& input,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
Padding padding) {
Expand All @@ -52,7 +52,7 @@ class ReduceWindowTest : public ClientLibraryTestBase {
window_dimensions, window_strides, padding);
}

void ReduceWindowMax(ComputationDataHandle input,
void ReduceWindowMax(const ComputationDataHandle& input,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
Padding padding) {
Expand All @@ -61,7 +61,7 @@ class ReduceWindowTest : public ClientLibraryTestBase {
CreateScalarMax(), window_dimensions, window_strides, padding);
}

void ReduceWindowMin(ComputationDataHandle input,
void ReduceWindowMin(const ComputationDataHandle& input,
tensorflow::gtl::ArraySlice<int64> window_dimensions,
tensorflow::gtl::ArraySlice<int64> window_strides,
Padding padding) {
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/compiler/xla/util.cc
Expand Up @@ -33,7 +33,7 @@ namespace {
// Adds a backtrace to the provided status iff the xla_status_add_backtrace flag
// is set. This is useful for quickly tracing status errors observed coming out
// of the service.
Status MaybeAddBacktrace(Status prior) {
Status MaybeAddBacktrace(const Status& prior) {
DCHECK(!prior.ok());
if (legacy_flags::GetUtilFlags()->xla_status_add_backtrace) {
return Status{prior.code(),
Expand Down
6 changes: 4 additions & 2 deletions tensorflow/contrib/batching/kernels/batch_kernels.cc
Expand Up @@ -817,7 +817,8 @@ class UnbatchGradResource : public ResourceBase {

// Flushes the information for one batch, given its context and done
// callback. Clears all information about it from the available_tensors_.
Status OutputBatch(OpKernelContext* context, AsyncOpKernel::DoneCallback done)
Status OutputBatch(OpKernelContext* context,
const AsyncOpKernel::DoneCallback& done)
EXCLUSIVE_LOCKS_REQUIRED(mu_) {
const Tensor& batch_index_t = context->input(1);
auto batch_index =
Expand Down Expand Up @@ -848,7 +849,8 @@ class UnbatchGradResource : public ResourceBase {
}

// Ingests data from one invocation of the op.
Status Compute(OpKernelContext* context, AsyncOpKernel::DoneCallback done) {
Status Compute(OpKernelContext* context,
const AsyncOpKernel::DoneCallback& done) {
const Tensor& data_t = context->input(0);
const Tensor& batch_index_t = context->input(1);
const Tensor& grad_t = context->input(2);
Expand Down
6 changes: 4 additions & 2 deletions tensorflow/contrib/nccl/kernels/nccl_manager.cc
Expand Up @@ -14,6 +14,8 @@ limitations under the License.
==============================================================================*/
#include "tensorflow/contrib/nccl/kernels/nccl_manager.h"

#include <utility>

#ifdef GOOGLE_CUDA

#include "tensorflow/core/lib/core/threadpool.h"
Expand Down Expand Up @@ -287,7 +289,7 @@ void NcclManager::AddBroadcastSend(
const Tensor* in_t, DoneCallback done_callback) {
std::unique_ptr<Participant> participant(
new Participant(in_t, nullptr /* out_t */, event_mgr, tensor_stream,
executor, gpu_device_id, done_callback));
executor, gpu_device_id, std::move(done_callback)));
participant->root = true;
AddParticipant(num_devices, key, std::move(participant), in_t->dtype(),
kBroadcast, ncclSum /* unused */);
Expand All @@ -300,7 +302,7 @@ void NcclManager::AddBroadcastRecv(
Tensor* out_t, DoneCallback done_callback) {
std::unique_ptr<Participant> participant(
new Participant(nullptr /* in_t */, out_t, event_mgr, tensor_stream,
executor, gpu_device_id, done_callback));
executor, gpu_device_id, std::move(done_callback)));
AddParticipant(num_devices, key, std::move(participant), out_t->dtype(),
kBroadcast, ncclSum /* unused */);
}
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/contrib/rnn/ops/lstm_ops_test.cc
Expand Up @@ -37,7 +37,7 @@ class LSTMOpsTest : public ::testing::Test {
}
};

static string JoinedCopies(string s, int copies) {
static string JoinedCopies(const string& s, int copies) {
string res;
for (int i = 0; i < copies; ++i) {
strings::StrAppend(&res, i > 0 ? ";" : "", s);
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/contrib/session_bundle/session_bundle_test.cc
Expand Up @@ -275,7 +275,7 @@ class SessionBundleTest : public ::testing::Test {
}
// SetupExport that allows for the variables and meta_graph_def filenames
// to be overridden.
string SetupExport(MetaGraphDefTwiddler twiddler,
string SetupExport(const MetaGraphDefTwiddler& twiddler,
const string& variables_filename,
const string& meta_graph_def_filename) {
// Construct a unique path name based on the test name.
Expand Down
4 changes: 2 additions & 2 deletions tensorflow/contrib/tensor_forest/kernels/tree_utils.cc
Expand Up @@ -43,8 +43,8 @@ DataColumnTypes FindSparseFeatureSpec(
return static_cast<DataColumnTypes>(spec.sparse(column_num).original_type());
}

void GetTwoBest(int max, std::function<float(int)> score_fn, float* best_score,
int* best_index, float* second_best_score,
void GetTwoBest(int max, const std::function<float(int)>& score_fn,
float* best_score, int* best_index, float* second_best_score,
int* second_best_index) {
*best_index = -1;
*second_best_index = -1;
Expand Down
3 changes: 2 additions & 1 deletion tensorflow/core/common_runtime/executor.cc
Expand Up @@ -1213,7 +1213,8 @@ class ExecutorState {
GUARDED_BY(mu_);

// The unique name of a frame.
inline string MakeFrameName(FrameState* frame, int64 iter_id, string name) {
inline string MakeFrameName(FrameState* frame, int64 iter_id,
const string& name) {
return strings::StrCat(frame->frame_name, ";", iter_id, ";", name);
}

Expand Down
2 changes: 1 addition & 1 deletion tensorflow/core/common_runtime/gpu/gpu_tracer_test.cc
Expand Up @@ -82,7 +82,7 @@ class GPUTracerTest : public ::testing::Test {
}

protected:
void ExpectFailure(Status status, error::Code code) {
void ExpectFailure(const Status& status, error::Code code) {
EXPECT_FALSE(status.ok());
if (!status.ok()) {
LOG(INFO) << "Status message: " << status.error_message();
Expand Down

0 comments on commit 1d0b8c0

Please sign in to comment.