Skip to content

Commit

Permalink
avoid calls to getArguments() in interpreter (#2473)
Browse files Browse the repository at this point in the history
Avoid calling costly `getArguments()` when the interpreter is only
interested by the number of arguments.
  • Loading branch information
quentin authored Mar 5, 2024
1 parent dc8ecf8 commit 7826a26
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/interpreter/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
{ \
auto result = EVAL_CHILD(RamDomain, 0); \
auto* result_val = &getSymbolTable().decode(result); \
for (std::size_t i = 1; i < args.size(); i++) { \
for (std::size_t i = 1; i < numArgs; i++) { \
auto alt = EVAL_CHILD(RamDomain, i); \
if (alt == result) continue; \
\
Expand All @@ -629,7 +629,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
#define MINMAX_OP(ty, op) \
{ \
auto result = EVAL_CHILD(ty, 0); \
for (std::size_t i = 1; i < args.size(); i++) { \
for (std::size_t i = 1; i < numArgs; i++) { \
result = op(result, EVAL_CHILD(ty, i)); \
} \
return ramBitCast(result); \
Expand All @@ -651,7 +651,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
getSymbolTable().decode(EVAL_CHILD(RamDomain, 0))));
// clang-format on

const auto& args = cur.getArguments();
const auto numArgs = cur.getNumArgs();
switch (cur.getOperator()) {
/** Unary Functor Operators */
case FunctorOp::ORD: return execute(shadow.getChild(0), ctxt);
Expand Down Expand Up @@ -759,7 +759,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {

case FunctorOp::CAT: {
std::stringstream ss;
for (std::size_t i = 0; i < args.size(); i++) {
for (std::size_t i = 0; i < numArgs; i++) {
ss << getSymbolTable().decode(execute(shadow.getChild(i), ctxt));
}
return getSymbolTable().encode(ss.str());
Expand Down Expand Up @@ -810,8 +810,8 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
ESAC(IntrinsicOperator)

CASE(NestedIntrinsicOperator)
auto numArgs = cur.getArguments().size();
auto runNested = [&](auto&& tuple) {
const auto numArgs = cur.getNumArgs();
const auto runNested = [&](auto&& tuple) {
ctxt[cur.getTupleId()] = tuple.data();
execute(shadow.getChild(numArgs), ctxt);
};
Expand All @@ -837,7 +837,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {

auto userFunctor = reinterpret_cast<void (*)()>(shadow.getFunctionPointer());
if (userFunctor == nullptr) fatal("cannot find user-defined operator `%s`", name);
std::size_t arity = cur.getArguments().size();
std::size_t arity = cur.getNumArgs();

if (cur.isStateful()) {
auto exec = std::bind(&Engine::execute, this, std::placeholders::_1, std::placeholders::_2);
Expand Down Expand Up @@ -957,8 +957,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
ESAC(UserDefinedOperator)

CASE(PackRecord)
auto values = cur.getArguments();
std::size_t arity = values.size();
const std::size_t arity = cur.getNumArgs();
std::unique_ptr<RamDomain[]> data = std::make_unique<RamDomain[]>(arity);
for (std::size_t i = 0; i < arity; ++i) {
data[i] = execute(shadow.getChild(i), ctxt);
Expand Down Expand Up @@ -1312,7 +1311,7 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
#undef ERASE

CASE(SubroutineReturn)
for (std::size_t i = 0; i < cur.getValues().size(); ++i) {
for (std::size_t i = 0; i < cur.getNumValues(); ++i) {
if (shadow.getChild(i) == nullptr) {
ctxt.addReturnValue(0);
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/ram/AbstractOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class AbstractOperator : public Expression {
return toPtrVector(arguments);
}

std::size_t getNumArgs() const {
return arguments.size();
}

void apply(const NodeMapper& map) override {
for (auto& arg : arguments) {
arg = map(std::move(arg));
Expand Down
4 changes: 4 additions & 0 deletions src/ram/NestedIntrinsicOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class NestedIntrinsicOperator : public TupleOperation {
return toPtrVector(args);
}

std::size_t getNumArgs() const {
return args.size();
}

NestedIntrinsicOperator* cloning() const override {
return new NestedIntrinsicOperator(op, clone(args), clone(getOperation()), getTupleId());
}
Expand Down
4 changes: 4 additions & 0 deletions src/ram/PackRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class PackRecord : public Expression {
return toPtrVector(arguments);
}

std::size_t getNumArgs() const {
return arguments.size();
}

PackRecord* cloning() const override {
auto* res = new PackRecord({});
for (auto& cur : arguments) {
Expand Down
4 changes: 4 additions & 0 deletions src/ram/SubroutineReturn.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class SubroutineReturn : public Operation {
return toPtrVector(expressions);
}

std::size_t getNumValues() const {
return expressions.size();
}

SubroutineReturn* cloning() const override {
VecOwn<Expression> newValues;
for (auto& expr : expressions) {
Expand Down
4 changes: 4 additions & 0 deletions src/ram/UserDefinedOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class UserDefinedOperator : public AbstractOperator {
return argsTypes;
}

std::size_t getNumArgs() const {
return argsTypes.size();
}

/** @brief Get return type */
TypeAttribute getReturnType() const {
return returnType;
Expand Down

0 comments on commit 7826a26

Please sign in to comment.