Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fsaintjacques committed Dec 31, 2019
1 parent cd36e0e commit 546f94c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 32 deletions.
31 changes: 13 additions & 18 deletions include/jitmap/query/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ struct NamedData {
using NamedBitmap = NamedData<Bitmap*>;
using NamedDenseBitset = NamedData<DenseBitset>;

class QueryContext;

class Query : public std::enable_shared_from_this<Query> {
public:
// Create a new query object based on an expression.
Expand All @@ -45,30 +43,27 @@ class Query : public std::enable_shared_from_this<Query> {
// \param[in] expr, the expression of the query
//
// \return a new query object
static std::shared_ptr<Query> Make(std::string name, Expr* expr);
//
// \throws ParserException with a reason why the parsing failed.
static std::shared_ptr<Query> Make(std::string name, std::string query);

// DenseBitset Evaluate(const std::vector<NamedDenseBitset> bitmaps);
// Return the name of the query.
const std::string& name() const;

void Optimize();
void Compile();
// Return the expression of the query.
const Expr& expr() const;

// Return the names of the referenced bitmap (variables) in the expression.
const std::vector<std::string>& variables() const { return variables_; }
const std::vector<std::string>& variables() const;

// Return the name of the query.
const std::string& name() const { return name_; }

// Return the expression of the query.
const Expr& expr() const { return *query_; }
~Query();

private:
std::string name_;
std::optional<Expr*> optimized_query_;
Expr* query_;

std::vector<std::string> variables_;
class Impl;
std::unique_ptr<Impl> impl_;

Query(std::string name, Expr* expr);
Query(const Query&) = delete;
Query(std::string name, std::string query);
};

} // namespace query
Expand Down
44 changes: 40 additions & 4 deletions src/jitmap/query/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,53 @@
// limitations under the License.

#include "jitmap/query/query.h"

#include <memory>
#include <string>
#include <vector>

#include "jitmap/query/optimizer.h"
#include "jitmap/query/parser.h"

namespace jitmap {
namespace query {

Query::Query(std::string name, Expr* expr)
: name_(std::move(name)), query_(expr), variables_(query_->Variables()) {}
class Query::Impl {
public:
Impl(std::string name, std::string query)
: name_(std::move(name)),
query_(std::move(query)),
expr_(Parse(query_, &builder_)),
optimized_expr_(Optimizer(&builder_).Optimize(*expr_)),
variables_(expr_->Variables()) {}

// Accessors
const std::string& name() const { return name_; }
const std::string& query() const { return query_; }
const Expr& expr() const { return *expr_; }
const Expr& optimized_expr() const { return *optimized_expr_; }
const std::vector<std::string>& variables() const { return variables_; }

std::shared_ptr<Query> Query::Make(std::string name, Expr* expr) {
return std::shared_ptr<Query>(new Query(name, expr));
private:
std::string name_;
std::string query_;
ExprBuilder builder_;
Expr* expr_;
Expr* optimized_expr_;
std::vector<std::string> variables_;
};

Query::Query(std::string name, std::string query)
: impl_(std::make_unique<Impl>(std::move(name), std::move(query))) {}
Query::~Query() {}

std::shared_ptr<Query> Query::Make(std::string name, std::string query) {
return std::shared_ptr<Query>(new Query(std::move(name), std::move(query)));
}

const std::string& Query::name() const { return impl_->name(); }
const Expr& Query::expr() const { return impl_->expr(); }
const std::vector<std::string>& Query::variables() const { return impl_->variables(); }

} // namespace query
} // namespace jitmap
63 changes: 61 additions & 2 deletions tests/jitmap_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
// limitations under the License.

#include <benchmark/benchmark.h>

#include <bitset>
#include <vector>

#include <jitmap/jitmap.h>
#include <jitmap/query/compiler.h>
#include <jitmap/query/jit.h>
#include <jitmap/size.h>

namespace jitmap {

Expand All @@ -28,19 +33,66 @@ static void StaticIntersection2(benchmark::State& state) {
benchmark::DoNotOptimize(a & b);
}

state.SetBytesProcessed(((kBitsPerContainer / 8) * 2) * state.iterations());
state.SetBytesProcessed(kBytesPerContainer * 2 * state.iterations());
}

static void JitIntersection2(benchmark::State& state) {
std::vector<std::vector<BitsetWordType>> bitmaps;
std::vector<const BitsetWordType*> inputs;
for (size_t i = 0; i < 2; i++) {
bitmaps.emplace_back(kWordsPerContainers, 0UL);
inputs.emplace_back(bitmaps[i].data());
}
std::vector<BitsetWordType> output(kWordsPerContainers, 0UL);

query::JitEngine engine;
auto query = query::Query::Make("benchmark_query2", "a & b");
auto compiler = query::QueryIRCodeGen("benchmark_module");
compiler.Compile(query);
engine.Compile(std::move(compiler));
auto eval_fn = engine.LookupUserQuery("benchmark_query2");

for (auto _ : state) {
eval_fn(inputs.data(), output.data());
}

state.SetBytesProcessed(kBytesPerContainer * 2 * state.iterations());
}
static void StaticIntersection3(benchmark::State& state) {
DenseBitmap a, b, c;

for (auto _ : state) {
benchmark::DoNotOptimize(a & b & c);
}

state.SetBytesProcessed(((kBitsPerContainer / 8) * 3) * state.iterations());
state.SetBytesProcessed(kBytesPerContainer * 3 * state.iterations());
}

static void JitIntersection3(benchmark::State& state) {
std::vector<std::vector<BitsetWordType>> bitmaps;
std::vector<const BitsetWordType*> inputs;
for (size_t i = 0; i < 3; i++) {
bitmaps.emplace_back(kWordsPerContainers, 0UL);
inputs.emplace_back(bitmaps[i].data());
}
std::vector<BitsetWordType> output(kWordsPerContainers, 0UL);

query::JitEngine engine;
auto query = query::Query::Make("benchmark_query3", "a & b & c");
auto compiler = query::QueryIRCodeGen("benchmark_module");
compiler.Compile(query);
engine.Compile(std::move(compiler));
auto eval_fn = engine.LookupUserQuery("benchmark_query3");

for (auto _ : state) {
eval_fn(inputs.data(), output.data());
}

state.SetBytesProcessed(kBytesPerContainer * 3 * state.iterations());
}
/*
static void StaticIntersection4(benchmark::State& state) {
DenseBitmap a, b, c, d;
Expand All @@ -50,8 +102,15 @@ static void StaticIntersection4(benchmark::State& state) {
state.SetBytesProcessed(((kBitsPerContainer / 8) * 4) * state.iterations());
}
*/

BENCHMARK(StaticIntersection2);
BENCHMARK(JitIntersection2);
BENCHMARK(StaticIntersection3);
BENCHMARK(JitIntersection3);

/*
BENCHMARK(StaticIntersection3);
BENCHMARK(StaticIntersection4);
*/
} // namespace jitmap
2 changes: 1 addition & 1 deletion tests/query/compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace query {
class QueryIRCodeGenTest : public QueryTest {};

TEST_F(QueryIRCodeGenTest, Basic) {
auto query = Query::Make("not_a", Not(Var("a")));
auto query = Query::Make("not_a", "!a");
QueryIRCodeGen("jitmap").Compile(query);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/query/jit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class JitTest : public QueryTest {
EXPECT_THAT(output, testing::Each(output_word));
}

DenseEvalFn CompileAndLookup(std::string_view query_expr,
DenseEvalFn CompileAndLookup(const std::string& query_expr,
const std::string& query_name) {
auto query = Query::Make(query_name, Parse(query_expr));
auto query = Query::Make(query_name, query_expr);
auto compiler = QueryIRCodeGen("module_" + std::to_string(id++));
compiler.Compile(query);
engine_.Compile(std::move(compiler));
Expand Down
3 changes: 1 addition & 2 deletions tests/query/query_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ TEST_F(ExprTest, Equals) {
ExprEq(Not(&f), Not(Full()));
ExprNe(Not(&e), Not(&f));

ExprEq(And(Var("b"), Or(Var("a"), &f)),
And(Var("b"), Or(Var("a"), &f)));
ExprEq(And(Var("b"), Or(Var("a"), &f)), And(Var("b"), Or(Var("a"), &f)));
}

TEST_F(ExprTest, EqualsNotCommutative) {
Expand Down
4 changes: 1 addition & 3 deletions tools/jitmap_ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ int main(int argc, char** argv) {
}

try {
ExprBuilder builder;
auto expr = Parse(argv[1], &builder);
auto query = Query::Make("query", expr);
auto query = Query::Make("query", argv[1]);
auto compiler = QueryIRCodeGen("jitmap-ir-module", {});
compiler.Compile(*query);
auto [module, ctx] = std::move(compiler).Finish();
Expand Down

0 comments on commit 546f94c

Please sign in to comment.