Commit 50c229bb authored by Kenton Varda's avatar Kenton Varda

Improve eval benchmark message structure to waste fewer objects (for both…

Improve eval benchmark message structure to waste fewer objects (for both protobuf and capnproto, but this turns out to help capnproto more than protobufs).
parent cb655884
...@@ -119,48 +119,68 @@ inline int32_t mod(int32_t a, int32_t b) { ...@@ -119,48 +119,68 @@ inline int32_t mod(int32_t a, int32_t b) {
} }
int32_t makeExpression(Expression::Builder exp, int depth) { int32_t makeExpression(Expression::Builder exp, int depth) {
// TODO: Operation_MAX or something.
exp.setOp((Operation)(rand() % (int)Operation::MODULUS + 1));
int left, right;
if (rand() % 8 < depth) { if (rand() % 8 < depth) {
exp.setOp(Operation::VALUE); exp.setLeftIsValue(true);
exp.setValue(rand() % 128 + 1); left = rand() % 128 + 1;
return exp.getValue(); exp.setLeftValue(left);
} else { } else {
// TODO: Operation_MAX or something. left = makeExpression(exp.initLeftExpression(), depth + 1);
exp.setOp((Operation)(rand() % (int)Operation::MODULUS + 1)); }
int32_t left = makeExpression(exp.initLeft(), depth + 1);
int32_t right = makeExpression(exp.initRight(), depth + 1); if (rand() % 8 < depth) {
switch (exp.getOp()) { exp.setRightIsValue(true);
case Operation::ADD: right = rand() % 128 + 1;
return left + right; exp.setRightValue(right);
case Operation::SUBTRACT: } else {
return left - right; right = makeExpression(exp.initRightExpression(), depth + 1);
case Operation::MULTIPLY: }
return left * right;
case Operation::DIVIDE: switch (exp.getOp()) {
return div(left, right); case Operation::ADD:
case Operation::MODULUS: return left + right;
return mod(left, right); case Operation::SUBTRACT:
case Operation::VALUE: return left - right;
break; case Operation::MULTIPLY:
} return left * right;
throw std::logic_error("Can't get here."); case Operation::DIVIDE:
return div(left, right);
case Operation::MODULUS:
return mod(left, right);
} }
throw std::logic_error("Can't get here.");
} }
int32_t evaluateExpression(Expression::Reader exp) { int32_t evaluateExpression(Expression::Reader exp) {
int left, right;
if (exp.getLeftIsValue()) {
left = exp.getLeftValue();
} else {
left = evaluateExpression(exp.getLeftExpression());
}
if (exp.getRightIsValue()) {
right = exp.getRightValue();
} else {
right = evaluateExpression(exp.getRightExpression());
}
switch (exp.getOp()) { switch (exp.getOp()) {
case Operation::VALUE:
return exp.getValue();
case Operation::ADD: case Operation::ADD:
return evaluateExpression(exp.getLeft()) + evaluateExpression(exp.getRight()); return left + right;
case Operation::SUBTRACT: case Operation::SUBTRACT:
return evaluateExpression(exp.getLeft()) - evaluateExpression(exp.getRight()); return left - right;
case Operation::MULTIPLY: case Operation::MULTIPLY:
return evaluateExpression(exp.getLeft()) * evaluateExpression(exp.getRight()); return left * right;
case Operation::DIVIDE: case Operation::DIVIDE:
return div(evaluateExpression(exp.getLeft()), evaluateExpression(exp.getRight())); return div(left, right);
case Operation::MODULUS: { case Operation::MODULUS:
return mod(evaluateExpression(exp.getLeft()), evaluateExpression(exp.getRight())); return mod(left, right);
}
} }
throw std::logic_error("Can't get here."); throw std::logic_error("Can't get here.");
} }
......
...@@ -119,47 +119,65 @@ inline int32_t mod(int32_t a, int32_t b) { ...@@ -119,47 +119,65 @@ inline int32_t mod(int32_t a, int32_t b) {
} }
int32_t makeExpression(Expression* exp, int depth) { int32_t makeExpression(Expression* exp, int depth) {
exp->set_op((Operation)(rand() % Operation_MAX + 1));
int left, right;
if (rand() % 8 < depth) { if (rand() % 8 < depth) {
exp->set_op(Operation::VALUE); left = rand() % 128 + 1;
exp->set_value(rand() % 128 + 1); exp->set_left_value(left);
return exp->value();
} else { } else {
exp->set_op((Operation)(rand() % Operation_MAX + 1)); left = makeExpression(exp->mutable_left_expression(), depth + 1);
int32_t left = makeExpression(exp->mutable_left(), depth + 1); }
int32_t right = makeExpression(exp->mutable_right(), depth + 1);
switch (exp->op()) { if (rand() % 8 < depth) {
case Operation::ADD: right = rand() % 128 + 1;
return left + right; exp->set_right_value(right);
case Operation::SUBTRACT: } else {
return left - right; right = makeExpression(exp->mutable_right_expression(), depth + 1);
case Operation::MULTIPLY:
return left * right;
case Operation::DIVIDE:
return div(left, right);
case Operation::MODULUS:
return mod(left, right);
case Operation::VALUE:
break;
}
throw std::logic_error("Can't get here.");
} }
switch (exp->op()) {
case Operation::ADD:
return left + right;
case Operation::SUBTRACT:
return left - right;
case Operation::MULTIPLY:
return left * right;
case Operation::DIVIDE:
return div(left, right);
case Operation::MODULUS:
return mod(left, right);
}
throw std::logic_error("Can't get here.");
} }
int32_t evaluateExpression(const Expression& exp) { int32_t evaluateExpression(const Expression& exp) {
int left, right;
if (exp.has_left_value()) {
left = exp.left_value();
} else {
left = evaluateExpression(exp.left_expression());
}
if (exp.has_right_value()) {
right = exp.right_value();
} else {
right = evaluateExpression(exp.right_expression());
}
switch (exp.op()) { switch (exp.op()) {
case Operation::VALUE:
return exp.value();
case Operation::ADD: case Operation::ADD:
return evaluateExpression(exp.left()) + evaluateExpression(exp.right()); return left + right;
case Operation::SUBTRACT: case Operation::SUBTRACT:
return evaluateExpression(exp.left()) - evaluateExpression(exp.right()); return left - right;
case Operation::MULTIPLY: case Operation::MULTIPLY:
return evaluateExpression(exp.left()) * evaluateExpression(exp.right()); return left * right;
case Operation::DIVIDE: case Operation::DIVIDE:
return div(evaluateExpression(exp.left()), evaluateExpression(exp.right())); return div(left, right);
case Operation::MODULUS: { case Operation::MODULUS:
return mod(evaluateExpression(exp.left()), evaluateExpression(exp.right())); return mod(left, right);
}
} }
throw std::logic_error("Can't get here."); throw std::logic_error("Can't get here.");
} }
......
...@@ -22,19 +22,25 @@ ...@@ -22,19 +22,25 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
enum Operation { enum Operation {
value = 0; add = 0;
add = 1; subtract = 1;
subtract = 2; multiply = 2;
multiply = 3; divide = 3;
divide = 4; modulus = 4;
modulus = 5;
} }
struct Expression { struct Expression {
op@0: Operation; op@0: Operation;
value@1: Int32;
left@2: Expression; # TODO: Use unions once fully-implemented.
right@3: Expression;
leftIsValue@1: Bool;
leftValue@2: Int32;
leftExpression@3: Expression;
rightIsValue@4: Bool;
rightValue@5: Int32;
rightExpression@6: Expression;
} }
struct EvaluationResult { struct EvaluationResult {
......
...@@ -24,19 +24,21 @@ ...@@ -24,19 +24,21 @@
package capnproto.benchmark.protobuf; package capnproto.benchmark.protobuf;
enum Operation { enum Operation {
VALUE = 0; ADD = 0;
ADD = 1; SUBTRACT = 1;
SUBTRACT = 2; MULTIPLY = 2;
MULTIPLY = 3; DIVIDE = 3;
DIVIDE = 4; MODULUS = 4;
MODULUS = 5;
} }
message Expression { message Expression {
required Operation op = 1; required Operation op = 1;
optional int32 value = 2;
optional Expression left = 3; optional int32 left_value = 2;
optional Expression right = 4; optional Expression left_expression = 3;
optional int32 right_value = 4;
optional Expression right_expression = 5;
} }
message EvaluationResult { message EvaluationResult {
......
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