Fix some unnecessary string copies when reading from or writing to a Tensor.
PiperOrigin-RevId: 262433288
This commit is contained in:
parent
bb2d5fb6aa
commit
f485b95b18
@ -264,11 +264,11 @@ class SequenceFileDatasetOp : public DatasetOpKernel {
|
||||
TF_RETURN_IF_ERROR(status);
|
||||
|
||||
Tensor key_tensor(ctx->allocator({}), DT_STRING, {});
|
||||
key_tensor.scalar<tstring>()() = key;
|
||||
key_tensor.scalar<tstring>()() = std::move(key);
|
||||
out_tensors->emplace_back(std::move(key_tensor));
|
||||
|
||||
Tensor value_tensor(ctx->allocator({}), DT_STRING, {});
|
||||
value_tensor.scalar<tstring>()() = value;
|
||||
value_tensor.scalar<tstring>()() = std::move(value);
|
||||
out_tensors->emplace_back(std::move(value_tensor));
|
||||
|
||||
*end_of_sequence = false;
|
||||
|
@ -128,9 +128,9 @@ class KafkaDatasetOp : public DatasetOpKernel {
|
||||
if (message->err() == RdKafka::ERR_NO_ERROR) {
|
||||
// Produce the line as output.
|
||||
Tensor line_tensor(cpu_allocator(), DT_STRING, {});
|
||||
line_tensor.scalar<tstring>()() =
|
||||
std::string(static_cast<const char*>(message->payload()),
|
||||
message->len());
|
||||
line_tensor.scalar<tstring>()().assign(
|
||||
static_cast<const char*>(message->payload()),
|
||||
message->len());
|
||||
out_tensors->emplace_back(std::move(line_tensor));
|
||||
*end_of_sequence = false;
|
||||
// Sync offset
|
||||
|
@ -130,7 +130,7 @@ static void ExtractExtraProperties(
|
||||
if (tensor.NumElements() != 1) {
|
||||
continue;
|
||||
}
|
||||
const string filename = tensor.scalar<tstring>()();
|
||||
const string& filename = tensor.scalar<tstring>()();
|
||||
|
||||
Env* env = Env::Default();
|
||||
FileStatistics stat;
|
||||
|
@ -109,7 +109,7 @@ class DecodeCompressedOp : public OpKernel {
|
||||
string output_string;
|
||||
Status s = zlib_stream->ReadNBytes(INT_MAX, &output_string);
|
||||
OP_REQUIRES(context, (s.ok() || errors::IsOutOfRange(s)), s);
|
||||
output_flat(i) = output_string;
|
||||
output_flat(i) = std::move(output_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -184,7 +184,7 @@ class DecodeCSVOp : public OpKernel {
|
||||
output[f]->flat<tstring>()(i) =
|
||||
record_defaults[f].flat<tstring>()(0);
|
||||
} else {
|
||||
output[f]->flat<tstring>()(i) = fields[f];
|
||||
output[f]->flat<tstring>()(i) = std::move(fields[f]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ class DecodeWavOp : public OpKernel {
|
||||
OP_REQUIRES(context, TensorShapeUtils::IsScalar(contents.shape()),
|
||||
errors::InvalidArgument("contents must be scalar, got shape ",
|
||||
contents.shape().DebugString()));
|
||||
const string wav_string = contents.scalar<tstring>()();
|
||||
const string& wav_string = contents.scalar<tstring>()();
|
||||
OP_REQUIRES(context, wav_string.size() <= std::numeric_limits<int>::max(),
|
||||
errors::InvalidArgument("WAV contents are too large for int: ",
|
||||
wav_string.size()));
|
||||
|
@ -123,12 +123,11 @@ class LoadAndRemapMatrixOp : public OpKernel {
|
||||
// Processes the checkpoint source and the provided Tensor name.
|
||||
const Tensor* ckpt_path_t;
|
||||
OP_REQUIRES_OK(context, context->input("ckpt_path", &ckpt_path_t));
|
||||
const string ckpt_path = *(ckpt_path_t->scalar<tstring>().data());
|
||||
const string& ckpt_path = ckpt_path_t->scalar<tstring>()();
|
||||
const Tensor* old_tensor_name_t;
|
||||
OP_REQUIRES_OK(context,
|
||||
context->input("old_tensor_name", &old_tensor_name_t));
|
||||
const string old_tensor_name =
|
||||
*(old_tensor_name_t->scalar<tstring>().data());
|
||||
const string& old_tensor_name = old_tensor_name_t->scalar<tstring>()();
|
||||
|
||||
LOG(INFO) << "Processing checkpoint : " << ckpt_path;
|
||||
BundleReader reader(context->env(), ckpt_path);
|
||||
|
@ -130,7 +130,7 @@ class InitializeTableFromTextFileOp : public OpKernel {
|
||||
errors::InvalidArgument("filename should be a single string, but got ",
|
||||
vocab_filename_tensor.shape().DebugString()));
|
||||
|
||||
string vocab_filename = vocab_filename_tensor.scalar<tstring>()();
|
||||
const string& vocab_filename = vocab_filename_tensor.scalar<tstring>()();
|
||||
OP_REQUIRES(ctx, !vocab_filename.empty(),
|
||||
errors::InvalidArgument("filename cannot be empty."));
|
||||
|
||||
|
@ -70,7 +70,7 @@ class RegexReplaceOp : public OpKernel {
|
||||
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(pattern_tensor->shape()),
|
||||
errors::InvalidArgument("Pattern must be scalar, but received ",
|
||||
pattern_tensor->shape().DebugString()));
|
||||
const string pattern = pattern_tensor->flat<tstring>()(0);
|
||||
const string& pattern = pattern_tensor->scalar<tstring>()();
|
||||
const RE2 match(pattern);
|
||||
OP_REQUIRES(ctx, match.ok(),
|
||||
errors::InvalidArgument("Invalid pattern: ", pattern,
|
||||
@ -81,7 +81,7 @@ class RegexReplaceOp : public OpKernel {
|
||||
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(rewrite_tensor->shape()),
|
||||
errors::InvalidArgument("Rewrite must be scalar, but received ",
|
||||
rewrite_tensor->shape().DebugString()));
|
||||
const string rewrite = rewrite_tensor->flat<tstring>()(0);
|
||||
const string& rewrite = rewrite_tensor->scalar<tstring>()();
|
||||
OP_REQUIRES_OK(ctx, InternalCompute(match, rewrite, replace_global_, ctx));
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ class StringFormatOp : public OpKernel {
|
||||
strings::StrAppend(&msg, split_template_[i + 1].c_str());
|
||||
}
|
||||
|
||||
formatted_string->scalar<tstring>()() = msg;
|
||||
formatted_string->scalar<tstring>()() = std::move(msg);
|
||||
}
|
||||
|
||||
private:
|
||||
|
Loading…
x
Reference in New Issue
Block a user