// nanobench comparison of the literal translation and the summarised // implementation on representative readable instances. #define ANKERL_NANOBENCH_IMPLEMENT #include "nanobench.h" #include #include #include #include #include #include "../src/fast.h" #include "../src/literal.h" #include "readable_gen.h" namespace { using namespace cr; struct Case { gen::Instance inst; RangeRead r; Result res; const char* name = ""; // Build the borrowed views once the case sits at its final address. void prepare() { inst.finish(); r = inst.read(); res = inst.result(); } }; std::vector makeDb(std::size_t n, std::size_t len) { std::vector db; db.reserve(n); for (std::size_t i = 0; i < n; ++i) { Key k; k.reserve(len); for (std::size_t j = 0; j < len; ++j) k.push_back(static_cast((i >> (8 * j)) & 0xff)); // Ensure strict ordering is preserved for the multi-byte encoding. if (!db.empty() && !keyLt(db.back(), k)) k.push_back(static_cast(i)); db.push_back(std::move(k)); } std::sort(db.begin(), db.end()); db.erase(std::unique(db.begin(), db.end()), db.end()); return db; } Case makeCase(const std::vector& db, OptKey beginKey, Int beginOff, OptKey endKey, Int endOff, bool reverse, bool truncate, const char* name) { gen::Instance inst; inst.beginKey = std::move(beginKey); inst.beginOffset = beginOff; inst.endKey = std::move(endKey); inst.endOffset = endOff; inst.reverse = reverse; OptKey lo = gen::resolve(db, Selector{optView(inst.beginKey), beginOff}); OptKey hi = gen::resolve(db, Selector{optView(inst.endKey), endOff}); std::vector complete = gen::inRange(db, lo, hi); if (reverse) std::reverse(complete.begin(), complete.end()); if (truncate && complete.size() > 1) { inst.keys.assign( complete.begin(), complete.begin() + static_cast(complete.size() / 2)); inst.more = true; } else { inst.keys = std::move(complete); inst.more = false; } Case c; c.inst = std::move(inst); c.name = name; return c; } void check(const Case& c) { ConflictRange a = literal::closedForm(c.r, c.res); ConflictRange b = fast::closedForm(c.r, c.res); if (!gen::equivalent(a, b)) { std::fprintf(stderr, "benchmark case %s disagrees between implementations\n", c.name); std::exit(1); } } } // namespace int main() { using ankerl::nanobench::Bench; const std::vector smallDb = makeDb(8, 1); const std::vector mediumDb = makeDb(48, 2); const std::vector longDb = makeDb(512, 3); const OptKey bot = bottomKey(); const OptKey top = std::nullopt; // A finite key above every key in `smallDb` and `longDb`, so the complete // reads below take the scanning branch (`residualT`, `allBelowEnd`, ...) // instead of short-circuiting on `⊤`. Key aboveKey; aboveKey.push_back(static_cast(0xff)); aboveKey.push_back(static_cast(0xff)); const OptKey above = OptKey{aboveKey}; std::vector cases; // A short complete read. An end offset of 2 (rather than 1) skips the // `end.offset <= 1` shortcut, so the literal translation actually rescans the // reported keys and the summary has something to summarise. cases.push_back(makeCase(smallDb, bot, 1, above, 2, false, false, "small complete")); // A short truncated read (cross-free). cases.push_back(makeCase(smallDb, smallDb[1], 1, top, 1, false, true, "small truncated fwd")); // A crossed read: the end selector resolves before the begin selector. cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, false, true, "crossed truncated fwd")); cases.push_back(makeCase(mediumDb, mediumDb[40], 1, mediumDb[3], 1, true, true, "crossed truncated rev")); // Long complete / truncated reads. cases.push_back( makeCase(longDb, bot, 1, above, 2, false, false, "long complete fwd")); cases.push_back(makeCase(longDb, bot, 1, top, 1, false, true, "long truncated fwd")); cases.push_back(makeCase(longDb, bot, 1, top, 1, true, true, "long truncated rev")); // The case vector is final now, so the borrowed views can point at it. for (Case& c : cases) c.prepare(); for (const Case& c : cases) { check(c); Bench bench; bench.title(c.name).relative(true).warmup(20).minEpochIterations(200); bench.run("literal", [&] { ankerl::nanobench::doNotOptimizeAway( literal::closedForm(c.r, c.res)); }); bench.run("fast", [&] { ankerl::nanobench::doNotOptimizeAway(fast::closedForm(c.r, c.res)); }); } return 0; }