Commit 87704e98 authored by Peter Dillinger's avatar Peter Dillinger Committed by Wouter van Oortmerssen

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.
parent cb99116a
......@@ -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
......
......@@ -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";
......
......@@ -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();
}
......
......@@ -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();
}
......
......@@ -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();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment