From 87704e987eb51dbd9852d7cae4e8013d47ad6eef Mon Sep 17 00:00:00 2001 From: Peter Dillinger Date: Thu, 20 Dec 2018 16:10:20 -0800 Subject: [PATCH] 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. --- php/FlatbufferBuilder.php | 15 ++++++++---- src/idl_gen_php.cpp | 4 ++-- tests/MyGame/Example/Monster.php | 36 ++++++++++++++-------------- tests/MyGame/Example/TypeAliases.php | 4 ++-- tests/union_vector/Movie.php | 4 ++-- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/php/FlatbufferBuilder.php b/php/FlatbufferBuilder.php index 925b4387d..4fbc2bc50 100644 --- a/php/FlatbufferBuilder.php +++ b/php/FlatbufferBuilder.php @@ -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 diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 91ce01367..ae5675483 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -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"; diff --git a/tests/MyGame/Example/Monster.php b/tests/MyGame/Example/Monster.php index 72ba76a01..311f0174f 100644 --- a/tests/MyGame/Example/Monster.php +++ b/tests/MyGame/Example/Monster.php @@ -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(); } diff --git a/tests/MyGame/Example/TypeAliases.php b/tests/MyGame/Example/TypeAliases.php index ecf804dfd..7629897fc 100644 --- a/tests/MyGame/Example/TypeAliases.php +++ b/tests/MyGame/Example/TypeAliases.php @@ -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(); } diff --git a/tests/union_vector/Movie.php b/tests/union_vector/Movie.php index 9baad5a5a..216cd288b 100644 --- a/tests/union_vector/Movie.php +++ b/tests/union_vector/Movie.php @@ -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(); }