Skip to content

Commit

Permalink
opt1: flatten_trivial_operations (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-cert authored Oct 1, 2024
1 parent 8467f9f commit b5d3f0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions libursa/QueryOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@ Query simplify_subqueries(Query &&q) {
return std::move(Query(q.get_type(), std::move(newqueries)));
}

// This optimization simplifies trivial (one operant) operations:
// AND(x) --> x
// OR(x) --> x
Query flatten_trivial_operations(Query &&q, bool *changed) {
if (q.get_type() == QueryType::AND && q.as_queries().size() == 1) {
*changed = true;
return std::move(q.as_queries()[0]);
}
if (q.get_type() == QueryType::OR && q.as_queries().size() == 1) {
*changed = true;
return std::move(q.as_queries()[0]);
}
return std::move(q);
}

Query q_optimize(Query &&q) {
if (q.get_type() == QueryType::PRIMITIVE) {
// Nothing to improve here.
return std::move(q);
}

q = simplify_subqueries(std::move(q));

// Optimization passes will be added here later.
bool changed = true;
while (changed) {
changed = false;
q = flatten_trivial_operations(std::move(q), &changed);
}

return std::move(q);
}
2 changes: 1 addition & 1 deletion libursa/Version.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ constexpr std::string_view ursadb_format_version = "1.5.0";
// Project version.
// Consider updating the version tag when doing PRs.
// clang-format off
constexpr std::string_view ursadb_version_string = "@PROJECT_VERSION@+opt0";
constexpr std::string_view ursadb_version_string = "@PROJECT_VERSION@+opt1";
// clang-format on

0 comments on commit b5d3f0f

Please sign in to comment.