Skip to content

Commit

Permalink
format on "[PyTorch] Avoid move-constructing a List in listConstruct"
Browse files Browse the repository at this point in the history
List's move ctor is a little bit more expensive than you might expect, but we can easily avoid it.

Differential Revision: [D25542190](https://our.internmc.facebook.com/intern/diff/D25542190/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25542190/)!

[ghstack-poisoned]
  • Loading branch information
swolchok committed Dec 14, 2020
1 parent 368a6cd commit 454082a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions torch/csrc/jit/runtime/vararg_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@ void listConstruct(
// Structuring the implementation this way allows NRVO to avoid
// move-constructing vals on its way onto the stack. Moving a List
// isn't free.
auto makeList = [](Stack& stack, const at::ListTypePtr& type, size_t num_inputs) {
c10::List<IValue> vals(type->getElementType());
vals.reserve(num_inputs);
for (size_t i = stack.size() - num_inputs; i < stack.size(); ++i) {
vals.emplace_back(std::move(stack[i]));
}
drop(stack, num_inputs);
return vals;
};
auto makeList =
[](Stack& stack, const at::ListTypePtr& type, size_t num_inputs) {
c10::List<IValue> vals(type->getElementType());
vals.reserve(num_inputs);
for (size_t i = stack.size() - num_inputs; i < stack.size(); ++i) {
vals.emplace_back(std::move(stack[i]));
}
drop(stack, num_inputs);
return vals;
};
stack.push_back(makeList(stack, type, num_inputs));
}

Expand Down

0 comments on commit 454082a

Please sign in to comment.