Commit fe4f0d9f by Chris Olivier Committed by Tianqi Chen

Address execution hot spots (#154)

* Address execution hot spots

* extend the reserve logic a bit

* FIx more hot spots

* Performance: use resize(0) instead of clear() in order to avoid a free/malloc
parent 3055d5ed
......@@ -15,7 +15,8 @@ int NNListAllOpNames(nn_uint *out_size,
API_BEGIN();
NNAPIThreadLocalEntry *ret = NNAPIThreadLocalStore::Get();
ret->ret_vec_str = dmlc::Registry<Op>::ListAllNames();
ret->ret_vec_charp.clear();
ret->ret_vec_charp.resize(0);
ret->ret_vec_charp.reserve(ret->ret_vec_str.size());
for (size_t i = 0; i < ret->ret_vec_str.size(); ++i) {
ret->ret_vec_charp.push_back(ret->ret_vec_str[i].c_str());
}
......@@ -64,7 +65,8 @@ int NNGetOpInfo(OpHandle handle,
*description = op->description.c_str();
*num_doc_args = static_cast<nn_uint>(op->arguments.size());
if (return_type) *return_type = nullptr;
ret->ret_vec_charp.clear();
ret->ret_vec_charp.resize(0);
ret->ret_vec_charp.reserve(op->arguments.size() * 3);
for (size_t i = 0; i < op->arguments.size(); ++i) {
ret->ret_vec_charp.push_back(op->arguments[i].name.c_str());
}
......@@ -207,13 +209,15 @@ int NNSymbolListAttrs(SymbolHandle symbol,
s->ListAttrs(static_cast<Symbol::ListAttrOption>(option)); // NOLINT(*)
std::vector<std::string>& attr_list = ret->ret_vec_str;
attr_list.clear();
attr_list.resize(0);
attr_list.reserve(attr.size());
for (const auto& kv : attr) {
attr_list.push_back(kv.first);
attr_list.push_back(kv.second);
}
*out_size = attr.size();
ret->ret_vec_charp.clear();
ret->ret_vec_charp.reserve(ret->ret_vec_str.size());
for (size_t i = 0; i < ret->ret_vec_str.size(); ++i) {
ret->ret_vec_charp.push_back(ret->ret_vec_str[i].c_str());
}
......@@ -229,7 +233,8 @@ int NNSymbolListInputVariables(SymbolHandle symbol,
NNAPIThreadLocalEntry *ret = NNAPIThreadLocalStore::Get();
API_BEGIN();
std::vector<NodePtr> vs = s->ListInputs(Symbol::ListInputOption(option));
ret->ret_handles.clear();
ret->ret_handles.resize(0);
ret->ret_handles.reserve(vs.size());
for (size_t i = 0; i < vs.size(); ++i) {
nnvm::Symbol* rs = new nnvm::Symbol();
rs->outputs.push_back(NodeEntry{vs[i], 0, 0});
......@@ -249,7 +254,8 @@ int NNSymbolListInputNames(SymbolHandle symbol,
API_BEGIN();
ret->ret_vec_str =
s->ListInputNames(Symbol::ListInputOption(option));
ret->ret_vec_charp.clear();
ret->ret_vec_charp.resize(0);
ret->ret_vec_charp.reserve(ret->ret_vec_str.size());
for (size_t i = 0; i < ret->ret_vec_str.size(); ++i) {
ret->ret_vec_charp.push_back(ret->ret_vec_str[i].c_str());
}
......@@ -265,7 +271,8 @@ int NNSymbolListOutputNames(SymbolHandle symbol,
NNAPIThreadLocalEntry *ret = NNAPIThreadLocalStore::Get();
API_BEGIN();
ret->ret_vec_str = s->ListOutputNames();
ret->ret_vec_charp.clear();
ret->ret_vec_charp.resize(0);
ret->ret_vec_charp.reserve(ret->ret_vec_str.size());
for (size_t i = 0; i < ret->ret_vec_str.size(); ++i) {
ret->ret_vec_charp.push_back(ret->ret_vec_str[i].c_str());
}
......
......@@ -189,6 +189,7 @@ Symbol Symbol::operator[] (size_t index) const {
std::vector<NodePtr> Symbol::ListInputs(ListInputOption option) const {
std::vector<NodePtr> ret;
if (option == kAll) {
ret.reserve(this->outputs.size());
DFSVisit(this->outputs, [&ret](const NodePtr &node) {
if (node->is_variable()) {
ret.push_back(node);
......@@ -197,6 +198,7 @@ std::vector<NodePtr> Symbol::ListInputs(ListInputOption option) const {
} else {
std::unordered_set<Node*> mutable_set;
std::vector<NodePtr> vlist;
vlist.reserve(this->outputs.size());
static auto& fmutate_inputs = Op::GetAttr<FMutateInputs>("FMutateInputs");
DFSVisit(this->outputs, [&ret, &mutable_set, &vlist](const NodePtr &node) {
if (node->is_variable()) {
......@@ -207,6 +209,7 @@ std::vector<NodePtr> Symbol::ListInputs(ListInputOption option) const {
}
}
});
ret.reserve(vlist.size());
for (const NodePtr& node : vlist) {
if ((option == kReadOnlyArgs && mutable_set.count(node.get()) == 0) ||
(option == kAuxiliaryStates && mutable_set.count(node.get()) != 0)) {
......@@ -230,6 +233,7 @@ std::vector<std::string> Symbol::ListOutputNames() const {
static auto& flist_ouputs = Op::GetAttr<FListOutputNames>("FListOutputNames");
std::vector<std::string> ret;
ret.reserve(outputs.size());
for (auto &head : outputs) {
if (head.node->is_variable()) {
ret.push_back(head.node->attrs.name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment