[C++] Use UnPackTo instead of UnPack if pointer exists (#6725)

Causes generated code to check for existing pointers in an object (when using the object based api) and use UnPackTo(existingPointer) rather than just using UnPack() to replace the pointer. This is a performance issue when unpacking to existing objects when using large buffer fields (e.g. image frames)

Co-authored-by: RobWauson <robwauson@gmail.com>
This commit is contained in:
RWauson
2021-08-05 12:40:03 -06:00
committed by GitHub
parent 35e2cac6eb
commit 909ce970ae
5 changed files with 38 additions and 19 deletions

View File

@@ -2767,9 +2767,19 @@ class CppGenerator : public BaseGenerator {
code += "/* else do nothing */";
}
} else {
const bool is_pointer =
field.value.type.VectorType().base_type == BASE_TYPE_STRUCT &&
!IsStruct(field.value.type.VectorType());
if (is_pointer) {
code += "if(_o->" + name + "[_i]" + ") { ";
code += indexing + "->UnPackTo(_o->" + name +
"[_i].get(), _resolver);";
code += " } else { ";
}
code += "_o->" + name + "[_i]" + access + " = ";
code += GenUnpackVal(field.value.type.VectorType(), indexing, true,
field);
if (is_pointer) { code += "; }"; }
}
code += "; } }";
}
@@ -2816,8 +2826,17 @@ class CppGenerator : public BaseGenerator {
} else {
// Generate code for assigning the value, of the form:
// _o->field = value;
const bool is_pointer =
field.value.type.base_type == BASE_TYPE_STRUCT &&
!IsStruct(field.value.type);
if (is_pointer) {
code += "{ if(_o->" + Name(field) + ") { ";
code += "_e->UnPackTo(_o->" + Name(field) + ".get(), _resolver);";
code += " } else { ";
}
code += "_o->" + Name(field) + " = ";
code += GenUnpackVal(field.value.type, "_e", false, field) + ";";
if (is_pointer) { code += " } }"; }
}
break;
}