Preallocate the node structure std::vector

PiperOrigin-RevId: 209830234
This commit is contained in:
Andrew Selle 2018-08-22 14:27:53 -07:00 committed by TensorFlower Gardener
parent 915fd68aa4
commit 091c9809b8
3 changed files with 13 additions and 0 deletions

View File

@ -476,6 +476,10 @@ TfLiteStatus Interpreter::ResetVariableTensorsToZero() {
return kTfLiteOk;
}
void Interpreter::ReserveNodes(int count) {
nodes_and_registration_.reserve(count);
}
TfLiteStatus Interpreter::AddNodeWithParameters(
const std::vector<int>& inputs, const std::vector<int>& outputs,
const char* init_data, size_t init_data_size, void* builtin_data,

View File

@ -136,6 +136,11 @@ class Interpreter {
// interpreter.
TfLiteStatus SetVariables(std::vector<int> variables);
// Ensure the internal node storage memory allocates at least `count`
// spots for node. NOTE, this doesn't actually add operators. This is an
// efficiency optimization that is subject to change.
void ReserveNodes(int count);
// Adds a node with the given parameters and returns the index of the new
// node in `node_index` (optionally). Interpreter will take ownership of
// `builtin_data` and destroy it with `free`. Ownership of 'init_data'

View File

@ -802,6 +802,10 @@ TfLiteStatus InterpreterBuilder::ParseNodes(
const flatbuffers::Vector<flatbuffers::Offset<Operator>>* operators,
Interpreter* interpreter) {
TfLiteStatus status = kTfLiteOk;
// Reduce the number of redundant allocations
interpreter->ReserveNodes(operators->Length());
for (int i = 0; i < operators->Length(); ++i) {
const auto* op = operators->Get(i);
int index = op->opcode_index();