- Correct stale references and headline-path wording in READMEs and Lean docstrings - Document build steps, comparator requirements, sweep scope, and coverage - Unify Apache headers across Lean and C++ sources - Add a bwrap-free axiom audit for the headline theorems - Add a Gitea workflow running lake build, the axiom audit, C++ tests, and coverage - Match LLVM coverage tool versions to the compiler and recreate stale build dirs
155 lines
5.0 KiB
C++
155 lines
5.0 KiB
C++
// Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||
// Authors: FormalConflictRange contributors
|
||
|
||
// nanobench comparison of the literal translation and the summarised
|
||
// implementation on representative readable instances.
|
||
#define ANKERL_NANOBENCH_IMPLEMENT
|
||
#include "nanobench.h"
|
||
|
||
#include <algorithm>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
#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 in place. `Case` is move-only (through
|
||
// `Instance`), and the views must be rebuilt after any move.
|
||
void prepare() {
|
||
inst.finish();
|
||
r = inst.read();
|
||
res = inst.result();
|
||
}
|
||
};
|
||
|
||
std::vector<Key> makeDb(std::size_t n, std::size_t len) {
|
||
std::vector<Key> 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<char>(static_cast<Byte>((i >> (8 * j)) & 0xff)));
|
||
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<Key>& 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<Key> 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<std::ptrdiff_t>(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<Key> smallDb = makeDb(8, 1);
|
||
const std::vector<Key> mediumDb = makeDb(48, 2);
|
||
const std::vector<Key> 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<char>(0xff));
|
||
aboveKey.push_back(static_cast<char>(0xff));
|
||
const OptKey above = OptKey{aboveKey};
|
||
|
||
std::vector<Case> 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 *key* is below the begin key, but the offsets move
|
||
// the resolved anchors back into order, so the range is nonempty and the
|
||
// dispatch takes the crossed branch.
|
||
cases.push_back(makeCase(mediumDb, mediumDb[40], -30, mediumDb[3], 40, false,
|
||
true, "crossed truncated fwd"));
|
||
cases.push_back(makeCase(mediumDb, mediumDb[40], -30, mediumDb[3], 40, 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"));
|
||
|
||
// Prepare each case in place (its views borrow from the instance) and check
|
||
// that both implementations agree before timing them.
|
||
for (Case& c : cases) {
|
||
c.prepare();
|
||
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;
|
||
}
|