mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 12:21:23 +00:00
[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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user