Files
conflict-range/cpp/tests/bench.cpp
T
andrew e32e42d933 Add a C++ translation of the closed form
- Literal transliteration of `Statement.closedForm` plus an equivalent
  summarised implementation
- Borrowed input keys (`std::string_view` / `std::span`) and owned result
  keys (`std::string`)
- Golden-vector test, differential/safety fuzzers, and a nanobench
  comparison

The literal and summarised implementations agree on 40,000 Lean golden
vectors, 18,971 sorted corpus cases, and the readable fuzzer; the raw fuzzer
runs both implementations under ASan/UBSan.  `build/` and the generated
`oracle/golden.txt` are ignored.
2026-11-17 16:08:07 -05:00

150 lines
4.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 once the case sits at its final address.
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<Byte>((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<Byte>(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<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 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;
}