Performance improvement to generated+supporting PHP (#5080)

By avoiding redundant alignment+alloc checks (startVector does prep) and virtual calls
(by making the class final) in inner loops.
This commit is contained in:
Peter Dillinger 2018-12-20 16:10:20 -08:00 committed by Wouter van Oortmerssen
parent cb99116aca
commit 87704e987e
5 changed files with 35 additions and 28 deletions

View File

@ -21,7 +21,7 @@
namespace Google\FlatBuffers;
class FlatbufferBuilder
final class FlatbufferBuilder
{
/**
* Internal ByteBuffer for the FlatBuffer data.
@ -278,6 +278,15 @@ class FlatbufferBuilder
{
$this->bb->putDouble($this->space -= 8, $x);
}
/**
* @param $off
*/
public function putOffset($off)
{
$new_off = $this->offset() - $off + Constants::SIZEOF_INT;
$this->putInt($new_off);
}
/// @endcond
/**
@ -562,9 +571,7 @@ class FlatbufferBuilder
if ($off > $this->offset()) {
throw new \Exception("");
}
$off = $this->offset() - $off + Constants::SIZEOF_INT;
$this->putInt($off);
$this->putOffset($off);
}
/// @cond FLATBUFFERS_INTERNAL

View File

@ -602,12 +602,12 @@ class PhpGenerator : public BaseGenerator {
code += "for ($i = count($data) - 1; $i >= 0; $i--) {\n";
if (IsScalar(field.value.type.VectorType().base_type)) {
code += Indent + Indent + Indent;
code += "$builder->add";
code += "$builder->put";
code += MakeCamel(GenTypeBasic(field.value.type.VectorType()));
code += "($data[$i]);\n";
} else {
code += Indent + Indent + Indent;
code += "$builder->addOffset($data[$i]);\n";
code += "$builder->putOffset($data[$i]);\n";
}
code += Indent + Indent + "}\n";
code += Indent + Indent + "return $builder->endVector();\n";

View File

@ -793,7 +793,7 @@ class Monster extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addByte($data[$i]);
$builder->putByte($data[$i]);
}
return $builder->endVector();
}
@ -852,7 +852,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 2);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -886,7 +886,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -920,7 +920,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -964,7 +964,7 @@ class Monster extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addByte($data[$i]);
$builder->putByte($data[$i]);
}
return $builder->endVector();
}
@ -1098,7 +1098,7 @@ class Monster extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addBool($data[$i]);
$builder->putBool($data[$i]);
}
return $builder->endVector();
}
@ -1162,7 +1162,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -1196,7 +1196,7 @@ class Monster extends Table
{
$builder->startVector(8, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -1230,7 +1230,7 @@ class Monster extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addByte($data[$i]);
$builder->putByte($data[$i]);
}
return $builder->endVector();
}
@ -1264,7 +1264,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 2);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -1298,7 +1298,7 @@ class Monster extends Table
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addLong($data[$i]);
$builder->putLong($data[$i]);
}
return $builder->endVector();
}
@ -1332,7 +1332,7 @@ class Monster extends Table
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addDouble($data[$i]);
$builder->putDouble($data[$i]);
}
return $builder->endVector();
}
@ -1376,7 +1376,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -1420,7 +1420,7 @@ class Monster extends Table
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addUlong($data[$i]);
$builder->putUlong($data[$i]);
}
return $builder->endVector();
}
@ -1454,7 +1454,7 @@ class Monster extends Table
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}
@ -1498,7 +1498,7 @@ class Monster extends Table
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addUlong($data[$i]);
$builder->putUlong($data[$i]);
}
return $builder->endVector();
}
@ -1542,7 +1542,7 @@ class Monster extends Table
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addUlong($data[$i]);
$builder->putUlong($data[$i]);
}
return $builder->endVector();
}
@ -1606,7 +1606,7 @@ class Monster extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addSbyte($data[$i]);
$builder->putSbyte($data[$i]);
}
return $builder->endVector();
}

View File

@ -326,7 +326,7 @@ class TypeAliases extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addSbyte($data[$i]);
$builder->putSbyte($data[$i]);
}
return $builder->endVector();
}
@ -360,7 +360,7 @@ class TypeAliases extends Table
{
$builder->startVector(8, count($data), 8);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addDouble($data[$i]);
$builder->putDouble($data[$i]);
}
return $builder->endVector();
}

View File

@ -154,7 +154,7 @@ class Movie extends Table
{
$builder->startVector(1, count($data), 1);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addByte($data[$i]);
$builder->putByte($data[$i]);
}
return $builder->endVector();
}
@ -188,7 +188,7 @@ class Movie extends Table
{
$builder->startVector(4, count($data), 4);
for ($i = count($data) - 1; $i >= 0; $i--) {
$builder->addOffset($data[$i]);
$builder->putOffset($data[$i]);
}
return $builder->endVector();
}