diff --git a/libursa/QueryOptimizer.cpp b/libursa/QueryOptimizer.cpp index f8198ce..726f416 100644 --- a/libursa/QueryOptimizer.cpp +++ b/libursa/QueryOptimizer.cpp @@ -17,6 +17,21 @@ 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. @@ -24,8 +39,11 @@ Query q_optimize(Query &&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); } diff --git a/libursa/Version.h.in b/libursa/Version.h.in index dbec52a..f8bc058 100644 --- a/libursa/Version.h.in +++ b/libursa/Version.h.in @@ -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