Add benchmark
All checks were successful
Tests / Release [gcc] total: 363, passed: 363
GNU C Compiler (gcc) |Total|New|Outstanding|Fixed|Trend
|:-:|:-:|:-:|:-:|:-:
|0|0|0|0|:clap:
Reference build: <a href="https://jenkins.weaselab.dev/job/weaselab/job/conflict-set/job/main/12//gcc">weaselab » conflict-set » main #12</a>
Tests / Coverage total: 361, passed: 361
weaselab/conflict-set/pipeline/head This commit looks good
All checks were successful
Tests / Release [gcc] total: 363, passed: 363
GNU C Compiler (gcc) |Total|New|Outstanding|Fixed|Trend
|:-:|:-:|:-:|:-:|:-:
|0|0|0|0|:clap:
Reference build: <a href="https://jenkins.weaselab.dev/job/weaselab/job/conflict-set/job/main/12//gcc">weaselab » conflict-set » main #12</a>
Tests / Coverage total: 361, passed: 361
weaselab/conflict-set/pipeline/head This commit looks good
This commit is contained in:
201
Bench.cpp
201
Bench.cpp
@@ -1,5 +1,7 @@
|
||||
#include "ConflictSet.h"
|
||||
#include "Internal.h"
|
||||
#include <byteswap.h>
|
||||
#include <cstdint>
|
||||
|
||||
#define ANKERL_NANOBENCH_IMPLEMENT
|
||||
#include "third_party/nanobench.h"
|
||||
@@ -15,94 +17,165 @@ std::string toKey(int n) {
|
||||
}
|
||||
|
||||
constexpr int kNumKeys = 100000;
|
||||
// A range read, a point read, and a point write. Range writes can erase
|
||||
// keys, and we don't want to change the number of keys stored in the
|
||||
// conflict set.
|
||||
constexpr int kOpsPerTx = 100;
|
||||
|
||||
void benchPointRead() {
|
||||
constexpr int kPrefixLen = 0;
|
||||
|
||||
std::span<const uint8_t> makeKey(Arena &arena, int index) {
|
||||
auto result = std::span<uint8_t>{new (arena) uint8_t[4], 4};
|
||||
index = __builtin_bswap32(index);
|
||||
memcpy(result.data(), &index, 4);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class ConflictSet_> void benchConflictSet() {
|
||||
ankerl::nanobench::Bench bench;
|
||||
ConflictSet_ cs{0};
|
||||
|
||||
int readKey = kNumKeys / 2;
|
||||
bench.batch(kOpsPerTx);
|
||||
|
||||
ConflictSet cs(0);
|
||||
int64_t version = 0;
|
||||
|
||||
// Populate conflict set
|
||||
Arena arena;
|
||||
{
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
writes.reserve(kNumKeys);
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
if (i != readKey) {
|
||||
auto key = makeKey(arena, i);
|
||||
ConflictSet::WriteRange conflict;
|
||||
conflict.begin.p = key.data();
|
||||
conflict.begin.len = key.size();
|
||||
conflict.end.len = 0;
|
||||
conflict.writeVersion = version + 1;
|
||||
writes.push_back(conflict);
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
++version;
|
||||
}
|
||||
|
||||
// I don't know why std::less didn't work /shrug
|
||||
struct Less {
|
||||
bool operator()(const std::span<const uint8_t> &lhs,
|
||||
const std::span<const uint8_t> &rhs) const {
|
||||
return lhs < rhs;
|
||||
}
|
||||
};
|
||||
auto points = set<std::span<const uint8_t>, Less>(arena);
|
||||
|
||||
// Two points for each range read, one for each point read, and one for each
|
||||
// point write
|
||||
while (points.size() < kOpsPerTx * 2 + 1) {
|
||||
// TODO don't use rand?
|
||||
points.insert(makeKey(arena, rand() % kNumKeys));
|
||||
}
|
||||
|
||||
// Make short-circuiting non-trivial
|
||||
{
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
writes.reserve(kNumKeys);
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
auto key = makeKey(arena, i);
|
||||
if (points.find(key) != points.end()) {
|
||||
continue;
|
||||
}
|
||||
auto key = toKey(i);
|
||||
ConflictSet::WriteRange w;
|
||||
w.begin.p = (const uint8_t *)key.data();
|
||||
w.begin.len = key.size();
|
||||
w.end.len = 0;
|
||||
w.writeVersion = 1;
|
||||
cs.addWrites(&w, 1);
|
||||
ConflictSet::WriteRange conflict;
|
||||
conflict.begin.p = key.data();
|
||||
conflict.begin.len = key.size();
|
||||
conflict.end.len = 0;
|
||||
conflict.writeVersion = version + 1;
|
||||
writes.push_back(conflict);
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
++version;
|
||||
}
|
||||
|
||||
auto key = toKey(readKey);
|
||||
{
|
||||
std::vector<ConflictSet::ReadRange> reads;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx; ++i) {
|
||||
ConflictSet::ReadRange r;
|
||||
r.begin.p = (const uint8_t *)key.data();
|
||||
r.begin.len = key.size();
|
||||
r.begin.p = iter->data();
|
||||
r.begin.len = iter->size();
|
||||
r.end.len = 0;
|
||||
r.readVersion = 0;
|
||||
ConflictSet::Result result;
|
||||
bench.run("point read", [&]() { cs.check(&r, &result, 1); });
|
||||
}
|
||||
|
||||
void benchPointerSet() {
|
||||
ankerl::nanobench::Bench bench;
|
||||
|
||||
constexpr int kNumPointers = 32;
|
||||
bench.batch(kNumPointers);
|
||||
|
||||
std::vector<void *> pointers;
|
||||
for (int i = 0; i < kNumPointers; ++i) {
|
||||
pointers.push_back(malloc(1));
|
||||
r.readVersion = version - 1;
|
||||
reads.push_back(r);
|
||||
++iter;
|
||||
}
|
||||
|
||||
bench.run("Create and destroy Arena hashset", [&]() {
|
||||
Arena arena;
|
||||
auto h = hashSet<void *>(arena);
|
||||
for (auto p : pointers) {
|
||||
h.insert(p);
|
||||
}
|
||||
});
|
||||
auto *results = new (arena) ConflictSet::Result[kOpsPerTx];
|
||||
|
||||
bench.run("Create and destroy malloc hashset", [&]() {
|
||||
std::unordered_set<void *, MyHash<void *>> h;
|
||||
for (auto p : pointers) {
|
||||
h.insert(p);
|
||||
bench.run("radix tree (point reads)",
|
||||
[&]() { cs.check(reads.data(), results, kOpsPerTx); });
|
||||
}
|
||||
});
|
||||
|
||||
bench.run("Create and destroy Arena vector", [&]() {
|
||||
Arena arena;
|
||||
auto h = vector<void *>(arena);
|
||||
for (auto p : pointers) {
|
||||
h.push_back(p);
|
||||
}
|
||||
});
|
||||
|
||||
{
|
||||
Arena arena;
|
||||
auto h = hashSet<void *>(arena);
|
||||
bench.run("Find hashset", [&]() {
|
||||
for (auto p : pointers) {
|
||||
bench.doNotOptimizeAway(h.find(p));
|
||||
std::vector<ConflictSet::ReadRange> reads;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx; ++i) {
|
||||
auto begin = *iter++;
|
||||
auto end = *iter++;
|
||||
ConflictSet::ReadRange r;
|
||||
r.begin.p = begin.data();
|
||||
r.begin.len = begin.size();
|
||||
r.end.p = end.data();
|
||||
r.end.len = end.size();
|
||||
r.readVersion = version - 1;
|
||||
reads.push_back(r);
|
||||
}
|
||||
|
||||
auto *results = new (arena) ConflictSet::Result[kOpsPerTx];
|
||||
|
||||
bench.run("radix tree (range reads)",
|
||||
[&]() { cs.check(reads.data(), results, kOpsPerTx); });
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx; ++i) {
|
||||
ConflictSet::WriteRange w;
|
||||
w.begin.p = iter->data();
|
||||
w.begin.len = iter->size();
|
||||
w.end.len = 0;
|
||||
writes.push_back(w);
|
||||
++iter;
|
||||
}
|
||||
|
||||
bench.run("radix tree (point writes)", [&]() {
|
||||
auto v = ++version;
|
||||
for (auto &w : writes) {
|
||||
w.writeVersion = v;
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
bench.run("Find vector", [&]() {
|
||||
for (auto p : pointers) {
|
||||
bench.doNotOptimizeAway(std::find(pointers.begin(), pointers.end(), p));
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx - 1; ++i) {
|
||||
auto begin = *iter++;
|
||||
auto end = *iter++;
|
||||
ConflictSet::WriteRange w;
|
||||
w.begin.p = begin.data();
|
||||
w.begin.len = begin.size();
|
||||
w.end.p = end.data();
|
||||
w.end.len = end.size();
|
||||
writes.push_back(w);
|
||||
}
|
||||
|
||||
bench.run("radix tree (range writes)", [&]() {
|
||||
auto v = ++version;
|
||||
for (auto &w : writes) {
|
||||
w.writeVersion = v;
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
});
|
||||
}
|
||||
|
||||
for (auto p : pointers) {
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// benchPointRead();
|
||||
benchPointerSet();
|
||||
}
|
||||
int main(void) { benchConflictSet<ConflictSet>(); }
|
@@ -4,6 +4,7 @@
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <compare>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <inttypes.h>
|
||||
@@ -21,15 +22,6 @@
|
||||
|
||||
// ==================== BEGIN IMPLEMENTATION ====================
|
||||
|
||||
int compare(std::span<const uint8_t> lhs, std::span<const uint8_t> rhs) {
|
||||
int cl = std::min<int>(lhs.size(), rhs.size());
|
||||
int c = cl == 0 ? 0 : memcmp(lhs.data(), rhs.data(), cl);
|
||||
if (c != 0) {
|
||||
return c;
|
||||
}
|
||||
return int(lhs.size()) - int(rhs.size());
|
||||
}
|
||||
|
||||
std::span<uint8_t> strincMutate(std::span<uint8_t> str, bool &ok) {
|
||||
int index;
|
||||
for (index = str.size() - 1; index >= 0; index--)
|
||||
@@ -793,7 +785,7 @@ bool checkRangeRead(Node *n, const std::span<const uint8_t> begin,
|
||||
}
|
||||
|
||||
bool first = true;
|
||||
for (auto *iter = left.n; iter != nullptr && compare(searchPath, end) < 0;
|
||||
for (auto *iter = left.n; iter != nullptr && searchPath < end;
|
||||
first = false) {
|
||||
if (iter->entryPresent) {
|
||||
if (!first && iter->entry.rangeVersion > readVersion) {
|
||||
@@ -811,11 +803,11 @@ bool checkRangeRead(Node *n, const std::span<const uint8_t> begin,
|
||||
#endif
|
||||
bool ok = true;
|
||||
auto rangeEnd = strincMutate(searchPath, ok);
|
||||
int c;
|
||||
auto c = std::strong_ordering::equal;
|
||||
if (!ok) {
|
||||
goto iterate;
|
||||
}
|
||||
c = compare(rangeEnd, end);
|
||||
c = rangeEnd <=> end;
|
||||
--rangeEnd.back();
|
||||
|
||||
if (c == 0) {
|
||||
|
19
Internal.h
19
Internal.h
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
@@ -19,6 +20,18 @@
|
||||
|
||||
#define DEBUG_VERBOSE 0
|
||||
|
||||
[[nodiscard]] inline auto
|
||||
operator<=>(const std::span<const uint8_t> &lhs,
|
||||
const std::span<const uint8_t> &rhs) noexcept {
|
||||
int cl = std::min<int>(lhs.size(), rhs.size());
|
||||
if (cl > 0) {
|
||||
if (auto c = memcmp(lhs.data(), rhs.data(), cl) <=> 0; c != 0) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return lhs.size() <=> rhs.size();
|
||||
}
|
||||
|
||||
// This header contains code that we want to reuse outside of ConflictSet.cpp or
|
||||
// want to exclude from coverage since it's only testing related.
|
||||
|
||||
@@ -181,9 +194,9 @@ template <class T> using Vector = std::vector<T, ArenaAlloc<T>>;
|
||||
template <class T> auto vector(Arena &arena) {
|
||||
return Vector<T>(ArenaAlloc<T>(&arena));
|
||||
}
|
||||
template <class T> using Set = std::set<T, std::less<T>, ArenaAlloc<T>>;
|
||||
template <class T> auto set(Arena &arena) {
|
||||
return Set<T>(ArenaAlloc<T>(&arena));
|
||||
template <class T, class C> using Set = std::set<T, C, ArenaAlloc<T>>;
|
||||
template <class T, class C = std::less<T>> auto set(Arena &arena) {
|
||||
return Set<T, C>(ArenaAlloc<T>(&arena));
|
||||
}
|
||||
|
||||
template <class T> struct MyHash;
|
||||
|
Reference in New Issue
Block a user