From 371ab598d993a7ba99e36e7a0b2b550983b87d1c Mon Sep 17 00:00:00 2001 From: Brody Franks <25767784+brodyf@users.noreply.github.com> Date: Mon, 11 Feb 2019 16:16:15 +1100 Subject: [PATCH 1/2] Refactor LevelConditionsTransformer --- src/RamTransforms.cpp | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/RamTransforms.cpp b/src/RamTransforms.cpp index eb4e03361de..394dcdb5103 100644 --- a/src/RamTransforms.cpp +++ b/src/RamTransforms.cpp @@ -64,20 +64,16 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { } } - using RamNodeMapper::operator(); - std::unique_ptr operator()(std::unique_ptr node) const override { - if (RamNestedOperation* nested = dynamic_cast(node.get())) { - if (const RamFilter* filter = dynamic_cast(&nested->getOperation())) { - const RamCondition& condition = filter->getCondition(); + if (RamFilter* filter = dynamic_cast(node.get())) { + const RamCondition& condition = filter->getCondition(); - if (context->rcla->getLevel(&condition) == identifier) { - addCondition(std::unique_ptr(condition.clone())); + if (context->rcla->getLevel(&condition) == identifier) { + addCondition(std::unique_ptr(condition.clone())); - // skip this filter - nested->setOperation(std::unique_ptr(filter->getOperation().clone())); - return (*this)(std::move(node)); - } + // skip this filter + node->apply(*this); + return std::unique_ptr(filter->getOperation().clone()); } } @@ -86,6 +82,21 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { } }; + class RamFilterInsert : public RamNodeMapper { + std::unique_ptr condition; + + public: + RamFilterInsert(std::unique_ptr c) : condition(std::move(c)) {} + + std::unique_ptr operator()(std::unique_ptr node) const override { + if (nullptr != dynamic_cast(node.get())) { + return std::make_unique(std::unique_ptr(condition->clone()), + std::unique_ptr(dynamic_cast(node.release()))); + } + return node; + } + }; + // Node-mapper that searches for and updates RAM scans nested in RAM inserts class RamScanCapturer : public RamNodeMapper { mutable bool modified = false; @@ -101,16 +112,14 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { std::unique_ptr operator()(std::unique_ptr node) const override { if (RamScan* scan = dynamic_cast(node.get())) { RamFilterCapturer filterUpdate(context, scan->getIdentifier()); - std::unique_ptr newScan = filterUpdate(std::unique_ptr(scan->clone())); + node->apply(filterUpdate); // If a condition applies to this scan level, filter the scan based on the condition if (std::unique_ptr condition = filterUpdate.getCondition()) { - newScan->setOperation(std::make_unique(std::move(condition), - std::unique_ptr(newScan->getOperation().clone()))); + RamFilterInsert filterInsert(std::move(condition)); + node->apply(filterInsert); modified = true; } - - node = std::move(newScan); } node->apply(*this); From d2cf7055d66c9623828392bbc0b9368e47d19071 Mon Sep 17 00:00:00 2001 From: Brody Franks <25767784+brodyf@users.noreply.github.com> Date: Mon, 11 Feb 2019 21:40:57 +1100 Subject: [PATCH 2/2] Refactor ConvertExistenceChecksTransformer --- src/RamOperation.h | 5 ----- src/RamTransforms.cpp | 50 ++++++++++++++++--------------------------- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/RamOperation.h b/src/RamOperation.h index 91eaff5e67b..32a3cd693ef 100644 --- a/src/RamOperation.h +++ b/src/RamOperation.h @@ -80,11 +80,6 @@ class RamNestedOperation : public RamOperation { return *nestedOperation; } - /** Set nested operation */ - void setOperation(std::unique_ptr nested) { - nestedOperation = std::move(nested); - } - /** Print */ void print(std::ostream& os, int tabpos) const override { nestedOperation->print(os, tabpos + 1); diff --git a/src/RamTransforms.cpp b/src/RamTransforms.cpp index 394dcdb5103..d2d782bb9d1 100644 --- a/src/RamTransforms.cpp +++ b/src/RamTransforms.cpp @@ -24,7 +24,7 @@ namespace { std::vector getConditions(const RamCondition* condition) { std::vector conditions; while (condition != nullptr) { - if (const RamAnd* ramAnd = dynamic_cast(condition)) { + if (const auto* ramAnd = dynamic_cast(condition)) { conditions.push_back(ramAnd->getRHS().clone()); condition = &ramAnd->getLHS(); } else { @@ -65,7 +65,7 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { } std::unique_ptr operator()(std::unique_ptr node) const override { - if (RamFilter* filter = dynamic_cast(node.get())) { + if (auto* filter = dynamic_cast(node.get())) { const RamCondition& condition = filter->getCondition(); if (context->rcla->getLevel(&condition) == identifier) { @@ -110,7 +110,7 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { } std::unique_ptr operator()(std::unique_ptr node) const override { - if (RamScan* scan = dynamic_cast(node.get())) { + if (auto* scan = dynamic_cast(node.get())) { RamFilterCapturer filterUpdate(context, scan->getIdentifier()); node->apply(filterUpdate); @@ -141,7 +141,7 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { std::unique_ptr operator()(std::unique_ptr node) const override { // get all RAM inserts - if (RamInsert* insert = dynamic_cast(node.get())) { + if (auto* insert = dynamic_cast(node.get())) { RamScanCapturer scanUpdate(context); insert->apply(scanUpdate); @@ -167,7 +167,7 @@ bool LevelConditionsTransformer::levelConditions(RamProgram& program) { /** Get indexable element */ std::unique_ptr CreateIndicesTransformer::getIndexElement( RamCondition* c, size_t& element, size_t identifier) { - if (RamBinaryRelation* binRelOp = dynamic_cast(c)) { + if (auto* binRelOp = dynamic_cast(c)) { if (binRelOp->getOperator() == BinaryConstraintOp::EQ) { if (auto* lhs = dynamic_cast(binRelOp->getLHS())) { RamValue* rhs = binRelOp->getRHS(); @@ -191,7 +191,7 @@ std::unique_ptr CreateIndicesTransformer::getIndexElement( } std::unique_ptr CreateIndicesTransformer::rewriteScan(const RamScan* scan) { - if (const RamFilter* filter = dynamic_cast(&scan->getOperation())) { + if (const auto* filter = dynamic_cast(&scan->getOperation())) { const RamRelationReference& rel = scan->getRelation(); const size_t identifier = scan->getIdentifier(); @@ -249,19 +249,17 @@ bool CreateIndicesTransformer::createIndices(RamProgram& program) { CreateIndicesTransformer* context; public: - RamScanCapturer(CreateIndicesTransformer* c) : modified(false), context(c) {} + RamScanCapturer(CreateIndicesTransformer* c) : context(c) {} bool getModified() const { return modified; } std::unique_ptr operator()(std::unique_ptr node) const override { - if (RamNestedOperation* nested = dynamic_cast(node.get())) { - if (const RamScan* scan = dynamic_cast(&nested->getOperation())) { - if (std::unique_ptr op = context->rewriteScan(scan)) { - modified = true; - nested->setOperation(std::move(op)); - } + if (auto* scan = dynamic_cast(node.get())) { + if (std::unique_ptr op = context->rewriteScan(scan)) { + modified = true; + node = std::move(op); } } node->apply(*this); @@ -283,14 +281,7 @@ bool CreateIndicesTransformer::createIndices(RamProgram& program) { std::unique_ptr operator()(std::unique_ptr node) const override { // get all RAM inserts - if (RamInsert* insert = dynamic_cast(node.get())) { - // TODO: better way to modify the child of a RAM insert - if (const RamScan* scan = dynamic_cast(&insert->getOperation())) { - if (std::unique_ptr op = context->rewriteScan(scan)) { - modified = true; - insert->setOperation(std::move(op)); - } - } + if (auto* insert = dynamic_cast(node.get())) { RamScanCapturer scanUpdate(context); insert->apply(scanUpdate); if (!modified && scanUpdate.getModified()) { @@ -306,7 +297,6 @@ bool CreateIndicesTransformer::createIndices(RamProgram& program) { // level all RAM inserts RamInsertCapturer insertUpdate(this); - program.getMain()->apply(insertUpdate); return insertUpdate.getModified(); @@ -332,17 +322,15 @@ bool ConvertExistenceChecksTransformer::convertExistenceChecks(RamProgram& progr while (!queue.empty()) { const RamValue* val = queue.back(); queue.pop_back(); - if (const RamElementAccess* elemAccess = dynamic_cast(val)) { + if (const auto* elemAccess = dynamic_cast(val)) { if (context->rvla->getLevel(elemAccess) == identifier) { return true; } - } else if (const RamIntrinsicOperator* intrinsicOp = - dynamic_cast(val)) { + } else if (const auto* intrinsicOp = dynamic_cast(val)) { for (const RamValue* arg : intrinsicOp->getArguments()) { queue.push_back(arg); } - } else if (const RamUserDefinedOperator* userDefinedOp = - dynamic_cast(val)) { + } else if (const auto* userDefinedOp = dynamic_cast(val)) { for (const RamValue* arg : userDefinedOp->getArguments()) { queue.push_back(arg); } @@ -352,14 +340,14 @@ bool ConvertExistenceChecksTransformer::convertExistenceChecks(RamProgram& progr } bool dependsOn(const RamCondition* condition, const size_t identifier) const { - if (const RamBinaryRelation* binRel = dynamic_cast(condition)) { + if (const auto* binRel = dynamic_cast(condition)) { return dependsOn(binRel->getLHS(), identifier) || dependsOn(binRel->getRHS(), identifier); } return false; } std::unique_ptr operator()(std::unique_ptr node) const override { - if (RamRelationSearch* scan = dynamic_cast(node.get())) { + if (auto* scan = dynamic_cast(node.get())) { const size_t identifier = scan->getIdentifier(); bool isExistCheck = true; visitDepthFirst(scan->getOperation(), [&](const RamFilter& filter) { @@ -397,7 +385,7 @@ bool ConvertExistenceChecksTransformer::convertExistenceChecks(RamProgram& progr const RamValue* value = values.back(); values.pop_back(); - if (const RamPack* pack = dynamic_cast(value)) { + if (const auto* pack = dynamic_cast(value)) { const std::vector args = pack->getArguments(); values.insert(values.end(), args.begin(), args.end()); } else if (const auto* intrinsicOp = @@ -457,7 +445,7 @@ bool ConvertExistenceChecksTransformer::convertExistenceChecks(RamProgram& progr std::unique_ptr operator()(std::unique_ptr node) const override { // get all RAM inserts - if (RamInsert* insert = dynamic_cast(node.get())) { + if (auto* insert = dynamic_cast(node.get())) { RamScanCapturer scanUpdate(context); insert->apply(scanUpdate);