From dff976ec0af2684b821d72157da16fa1c86414e4 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Thu, 18 Jan 2024 16:23:37 -0800 Subject: [PATCH] Shuffle keys before inserting --- ConflictSet.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ConflictSet.cpp b/ConflictSet.cpp index 23eb785..fb967bd 100644 --- a/ConflictSet.cpp +++ b/ConflictSet.cpp @@ -3,13 +3,14 @@ #include #include #include +#include #include #include #include #include -#define SHOW_PRIORITY 1 -#define DEBUG 1 +#define SHOW_PRIORITY 0 +#define DEBUG 0 using Key = ConflictSet::Key; @@ -527,6 +528,13 @@ struct ConflictSet::Impl { // TODO Descend until queries for front and back diverge + // Mitigate potential n^2 behavior of insertion by shuffling the insertion + // order. Not sure how this interacts with interleaved insertion but it's + // probably fine. + // TODO better/faster RNG? + std::mt19937 g(fastRand()); + std::shuffle(stepwiseInserts.get(), stepwiseInserts.get() + count, g); + runInterleaved(std::span(stepwiseInserts.get(), count)); std::vector workList; @@ -538,6 +546,10 @@ struct ConflictSet::Impl { while (!workList.empty()) { Node *n = workList.back(); workList.pop_back(); +#if DEBUG + fprintf(stderr, "\tcheck heap invariant %.*s\n", n->len, + (const char *)(n + 1)); +#endif if (n->parent == nullptr) { continue; } @@ -561,8 +573,6 @@ struct ConflictSet::Impl { if (lr != nullptr) { workList.push_back(lr); } - } else { - continue; } } } @@ -621,7 +631,7 @@ ConflictSet &ConflictSet::operator=(ConflictSet &&other) noexcept { int main(void) { int64_t writeVersion = 0; ConflictSet::Impl cs{writeVersion}; - constexpr int kNumKeys = 3; + constexpr int kNumKeys = 5; ConflictSet::WriteRange write[kNumKeys]; for (int i = 0; i < kNumKeys; ++i) { write[i].begin = toKey(i);