Compare commits
22
Commits
84bf5707f9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aeafce1bc8 | ||
|
|
cce3e1651f | ||
|
|
d7b4b66dd5 | ||
|
|
3d997d5c57 | ||
|
|
0ca1d3c546 | ||
|
|
b671f93e5a | ||
|
|
e23018c952 | ||
|
|
b1c1f8f2ee | ||
|
|
4bec2cf4eb | ||
|
|
fa1701479c | ||
|
|
20e2e332e4 | ||
|
|
23d092ab69 | ||
|
|
c94e9aaee6 | ||
|
|
a792352387 | ||
|
|
56e0673fc7 | ||
|
|
80f084cc43 | ||
|
|
c0bd10effa | ||
|
|
df986810ff | ||
|
|
8b6389236a | ||
|
|
b22d5f4816 | ||
|
|
bba328f79f | ||
|
|
3d3184ef5f |
No files matched your search
+23
-2
@@ -182,6 +182,8 @@ endif()
|
|||||||
|
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
|
||||||
|
option(TEST_SKIP_LIST "Run the slow skip list cross-check tests" OFF)
|
||||||
|
|
||||||
# disable tests if this is being used through e.g. FetchContent
|
# disable tests if this is being used through e.g. FetchContent
|
||||||
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND BUILD_TESTING)
|
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND BUILD_TESTING)
|
||||||
|
|
||||||
@@ -224,8 +226,9 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND BUILD_TESTING)
|
|||||||
target_compile_options(driver_skip_list PRIVATE ${TEST_FLAGS})
|
target_compile_options(driver_skip_list PRIVATE ${TEST_FLAGS})
|
||||||
target_link_libraries(driver_skip_list PRIVATE skip_list)
|
target_link_libraries(driver_skip_list PRIVATE skip_list)
|
||||||
|
|
||||||
# enable to test skip list
|
# Optional cross-check of SkipList.cpp against ReferenceImpl. Opt-in via
|
||||||
if(0)
|
# -DTEST_SKIP_LIST=ON, since these are slow.
|
||||||
|
if(TEST_SKIP_LIST)
|
||||||
foreach(TEST ${CORPUS_TESTS})
|
foreach(TEST ${CORPUS_TESTS})
|
||||||
get_filename_component(hash ${TEST} NAME)
|
get_filename_component(hash ${TEST} NAME)
|
||||||
add_test(NAME skip_list_${hash} COMMAND driver_skip_list ${TEST})
|
add_test(NAME skip_list_${hash} COMMAND driver_skip_list ${TEST})
|
||||||
@@ -390,6 +393,24 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND BUILD_TESTING)
|
|||||||
PROPERTIES CXX_STANDARD_REQUIRED ON)
|
PROPERTIES CXX_STANDARD_REQUIRED ON)
|
||||||
add_test(NAME conflict_set_cxx_api_test COMMAND conflict_set_cxx_api_test)
|
add_test(NAME conflict_set_cxx_api_test COMMAND conflict_set_cxx_api_test)
|
||||||
|
|
||||||
|
# SHOW_MEMORY instrumentation, which the rest of the test suite doesn't
|
||||||
|
# compile with. It assumes a single live ConflictSet (see Internal.h).
|
||||||
|
add_executable(conflict_set_show_memory_test ConflictSet.cpp
|
||||||
|
ShowMemoryTest.cpp)
|
||||||
|
target_compile_options(conflict_set_show_memory_test PRIVATE ${TEST_FLAGS})
|
||||||
|
target_compile_definitions(conflict_set_show_memory_test
|
||||||
|
PRIVATE SHOW_MEMORY=1)
|
||||||
|
target_include_directories(conflict_set_show_memory_test
|
||||||
|
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
if(NOT CMAKE_CROSSCOMPILING)
|
||||||
|
target_compile_options(conflict_set_show_memory_test
|
||||||
|
PRIVATE -fsanitize=address,undefined)
|
||||||
|
target_link_options(conflict_set_show_memory_test PRIVATE
|
||||||
|
-fsanitize=address,undefined)
|
||||||
|
endif()
|
||||||
|
add_test(NAME conflict_set_show_memory_test
|
||||||
|
COMMAND conflict_set_show_memory_test)
|
||||||
|
|
||||||
# symbol visibility tests
|
# symbol visibility tests
|
||||||
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
|
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
|
|||||||
+127
-19
@@ -80,17 +80,19 @@ using namespace weaselab;
|
|||||||
#define assume assert
|
#define assume assert
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if SHOW_MEMORY
|
// The SHOW_MEMORY instrumentation. Declared unconditionally so that each of
|
||||||
|
// these has a single definition in every configuration (see the definitions at
|
||||||
|
// the end of this file), rather than a no-op stub standing in for the real
|
||||||
|
// thing.
|
||||||
void addNode(struct Node *);
|
void addNode(struct Node *);
|
||||||
void removeNode(struct Node *);
|
void removeNode(struct Node *);
|
||||||
void addKey(struct Node *);
|
void addKey(struct Node *);
|
||||||
void removeKey(struct Node *);
|
void removeKey(struct Node *);
|
||||||
#else
|
// Like `removeKey`, but with a caller-provided search path length. Used when
|
||||||
constexpr void addNode(struct Node *) {}
|
// the node's ancestors may already have been released, which makes walking
|
||||||
constexpr void removeNode(struct Node *) {}
|
// `parent` pointers invalid.
|
||||||
constexpr void addKey(struct Node *) {}
|
void removeKey(struct Node *, int64_t searchPathLength);
|
||||||
constexpr void removeKey(struct Node *) {}
|
int64_t getSearchPathLength(struct Node *);
|
||||||
#endif
|
|
||||||
|
|
||||||
// ==================== BEGIN IMPLEMENTATION ====================
|
// ==================== BEGIN IMPLEMENTATION ====================
|
||||||
|
|
||||||
@@ -334,7 +336,9 @@ struct Node {
|
|||||||
uint8_t *partialKey();
|
uint8_t *partialKey();
|
||||||
Type getType() const { return type; }
|
Type getType() const { return type; }
|
||||||
int32_t getCapacity() const {
|
int32_t getCapacity() const {
|
||||||
assert(!releaseDeferred);
|
// `partialKeyCapacity` is not repurposed by `deferRelease`, so this is also
|
||||||
|
// valid for nodes with `releaseDeferred` set. It's needed to account for
|
||||||
|
// the capacity of a deferred node when it's finally released.
|
||||||
return partialKeyCapacity;
|
return partialKeyCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2782,10 +2786,39 @@ checkMaxBetweenExclusiveImpl<true>(Node256 *n, int begin, int end,
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SHOW_MEMORY
|
||||||
|
// Length of the key leading to `child`, given the length of the key leading to
|
||||||
|
// its parent. Consistent with `getSearchPathLength`.
|
||||||
|
int64_t childSearchPathLength(int64_t parentSearchPathLength, Node *child) {
|
||||||
|
return parentSearchPathLength + 1 + child->partialKeyLen;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void eraseTree(Node *root, WriteContext *writeContext) {
|
void eraseTree(Node *root, WriteContext *writeContext) {
|
||||||
Arena arena;
|
Arena arena;
|
||||||
auto toFree = vector<Node *>(arena);
|
auto toFree = vector<Node *>(arena);
|
||||||
toFree.push_back(root);
|
toFree.push_back(root);
|
||||||
|
// Length of the key leading to each node in `toFree`, on a parallel stack.
|
||||||
|
// A node is released before its children are visited, so the `parent`
|
||||||
|
// pointers that `getSearchPathLength` walks are dangling by the time a queued
|
||||||
|
// node is popped. Carry the lengths along instead. Only the SHOW_MEMORY
|
||||||
|
// accounting needs them.
|
||||||
|
#if SHOW_MEMORY
|
||||||
|
auto searchPathLengths = vector<int64_t>(arena);
|
||||||
|
searchPathLengths.push_back(getSearchPathLength(root));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Queue `child` for release, where `parentSearchPathLength` is the length of
|
||||||
|
// the key leading to its parent.
|
||||||
|
auto queueChild = [&](Node *child, int64_t parentSearchPathLength) {
|
||||||
|
toFree.push_back(child);
|
||||||
|
#if SHOW_MEMORY
|
||||||
|
searchPathLengths.push_back(
|
||||||
|
childSearchPathLength(parentSearchPathLength, child));
|
||||||
|
#else
|
||||||
|
(void)parentSearchPathLength;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
while (toFree.size() > 0) {
|
while (toFree.size() > 0) {
|
||||||
auto *n = toFree.back();
|
auto *n = toFree.back();
|
||||||
@@ -2793,7 +2826,13 @@ void eraseTree(Node *root, WriteContext *writeContext) {
|
|||||||
writeContext->accum.entries_erased += n->entryPresent;
|
writeContext->accum.entries_erased += n->entryPresent;
|
||||||
++writeContext->accum.nodes_released;
|
++writeContext->accum.nodes_released;
|
||||||
|
|
||||||
removeKey(n);
|
#if SHOW_MEMORY
|
||||||
|
const int64_t searchPathLength = searchPathLengths.back();
|
||||||
|
searchPathLengths.pop_back();
|
||||||
|
#else
|
||||||
|
const int64_t searchPathLength = 0;
|
||||||
|
#endif
|
||||||
|
removeKey(n, searchPathLength);
|
||||||
|
|
||||||
switch (n->getType()) {
|
switch (n->getType()) {
|
||||||
case Type_Node0: {
|
case Type_Node0: {
|
||||||
@@ -2803,28 +2842,39 @@ void eraseTree(Node *root, WriteContext *writeContext) {
|
|||||||
case Type_Node3: {
|
case Type_Node3: {
|
||||||
auto *n3 = static_cast<Node3 *>(n);
|
auto *n3 = static_cast<Node3 *>(n);
|
||||||
for (int i = 0; i < n3->numChildren; ++i) {
|
for (int i = 0; i < n3->numChildren; ++i) {
|
||||||
toFree.push_back(n3->children[i]);
|
queueChild(n3->children[i], searchPathLength);
|
||||||
}
|
}
|
||||||
writeContext->release(n3);
|
writeContext->release(n3);
|
||||||
} break;
|
} break;
|
||||||
case Type_Node16: {
|
case Type_Node16: {
|
||||||
auto *n16 = static_cast<Node16 *>(n);
|
auto *n16 = static_cast<Node16 *>(n);
|
||||||
for (int i = 0; i < n16->numChildren; ++i) {
|
for (int i = 0; i < n16->numChildren; ++i) {
|
||||||
toFree.push_back(n16->children[i]);
|
queueChild(n16->children[i], searchPathLength);
|
||||||
}
|
}
|
||||||
writeContext->release(n16);
|
writeContext->release(n16);
|
||||||
} break;
|
} break;
|
||||||
case Type_Node48: {
|
case Type_Node48: {
|
||||||
auto *n48 = static_cast<Node48 *>(n);
|
auto *n48 = static_cast<Node48 *>(n);
|
||||||
for (int i = 0; i < n48->numChildren; ++i) {
|
for (int i = 0; i < n48->numChildren; ++i) {
|
||||||
toFree.push_back(n48->children[i]);
|
queueChild(n48->children[i], searchPathLength);
|
||||||
}
|
}
|
||||||
writeContext->release(n48);
|
writeContext->release(n48);
|
||||||
} break;
|
} break;
|
||||||
case Type_Node256: {
|
case Type_Node256: {
|
||||||
auto *n256 = static_cast<Node256 *>(n);
|
auto *n256 = static_cast<Node256 *>(n);
|
||||||
auto *out = toFree.unsafePrepareAppend(n256->numChildren).data();
|
auto *out = toFree.unsafePrepareAppend(n256->numChildren).data();
|
||||||
n256->bitSet.forEachSet([&](int i) { *out++ = n256->children[i]; });
|
#if SHOW_MEMORY
|
||||||
|
auto *outSearchPathLengths =
|
||||||
|
searchPathLengths.unsafePrepareAppend(n256->numChildren).data();
|
||||||
|
#endif
|
||||||
|
n256->bitSet.forEachSet([&](int i) {
|
||||||
|
Node *child = n256->children[i];
|
||||||
|
*out++ = child;
|
||||||
|
#if SHOW_MEMORY
|
||||||
|
*outSearchPathLengths++ =
|
||||||
|
childSearchPathLength(searchPathLength, child);
|
||||||
|
#endif
|
||||||
|
});
|
||||||
assert(out == toFree.end());
|
assert(out == toFree.end());
|
||||||
writeContext->release(n256);
|
writeContext->release(n256);
|
||||||
} break;
|
} break;
|
||||||
@@ -5012,9 +5062,11 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
|
|||||||
fprintf(stderr, "search path: %s, begin: %s\n",
|
fprintf(stderr, "search path: %s, begin: %s\n",
|
||||||
getSearchPathPrintable(b).c_str(),
|
getSearchPathPrintable(b).c_str(),
|
||||||
printable(context.results[i].remaining).c_str());
|
printable(context.results[i].remaining).c_str());
|
||||||
fprintf(stderr, "search path: %s, end: %s\n",
|
if (e != nullptr) {
|
||||||
getSearchPathPrintable(e).c_str(),
|
fprintf(stderr, "search path: %s, end: %s\n",
|
||||||
printable(context.results[i].endRemaining).c_str());
|
getSearchPathPrintable(e).c_str(),
|
||||||
|
printable(context.results[i].endRemaining).c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -5494,8 +5546,11 @@ void internal_addWrites(ConflictSet::Impl *impl,
|
|||||||
impl->totalBytes += mallocBytesDelta;
|
impl->totalBytes += mallocBytesDelta;
|
||||||
impl->memory_bytes.set(impl->totalBytes);
|
impl->memory_bytes.set(impl->totalBytes);
|
||||||
#if SHOW_MEMORY
|
#if SHOW_MEMORY
|
||||||
|
// Testing-only cross-check of the accounting. `mallocBytes` is process-wide,
|
||||||
|
// while `impl->totalBytes` only covers this instance, so this holds only
|
||||||
|
// while this is the only live ConflictSet (see Internal.h).
|
||||||
if (impl->totalBytes != mallocBytes) {
|
if (impl->totalBytes != mallocBytes) {
|
||||||
abort();
|
abort(); // GCOVR_EXCL_LINE
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -5506,8 +5561,11 @@ void internal_setOldestVersion(ConflictSet::Impl *impl, int64_t oldestVersion) {
|
|||||||
impl->totalBytes += mallocBytesDelta;
|
impl->totalBytes += mallocBytesDelta;
|
||||||
impl->memory_bytes.set(impl->totalBytes);
|
impl->memory_bytes.set(impl->totalBytes);
|
||||||
#if SHOW_MEMORY
|
#if SHOW_MEMORY
|
||||||
|
// Testing-only cross-check of the accounting. `mallocBytes` is process-wide,
|
||||||
|
// while `impl->totalBytes` only covers this instance, so this holds only
|
||||||
|
// while this is the only live ConflictSet (see Internal.h).
|
||||||
if (impl->totalBytes != mallocBytes) {
|
if (impl->totalBytes != mallocBytes) {
|
||||||
abort();
|
abort(); // GCOVR_EXCL_LINE
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -5577,6 +5635,9 @@ Node *firstGeqLogical(Node *n, const TrivialSpan key) {
|
|||||||
goto downLeftSpine;
|
goto downLeftSpine;
|
||||||
} else {
|
} else {
|
||||||
n = nextSibling(n);
|
n = nextSibling(n);
|
||||||
|
if (n == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
goto downLeftSpine;
|
goto downLeftSpine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5874,7 +5935,9 @@ checkMaxVersion(Node *root, Node *node, InternalVersionT oldestVersion,
|
|||||||
expected = std::max(expected, child->entry.rangeVersion);
|
expected = std::max(expected, child->entry.rangeVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto key = getSearchPath(root);
|
// Account for the range version of the first key after `node`'s subtree: a
|
||||||
|
// range write spanning the subtree is recorded there.
|
||||||
|
auto key = getSearchPath(node);
|
||||||
bool ok;
|
bool ok;
|
||||||
auto inc = strinc(key, ok);
|
auto inc = strinc(key, ok);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
@@ -5973,6 +6036,14 @@ checkMaxVersion(Node *root, Node *node, InternalVersionT oldestVersion,
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
// The SHOW_MEMORY instrumentation. Each of these functions is defined exactly
|
||||||
|
// once, with the accounting compiled out when SHOW_MEMORY is disabled. Defining
|
||||||
|
// them once (rather than defining no-op stubs when SHOW_MEMORY is disabled)
|
||||||
|
// keeps every configuration of this file reporting each function on the same
|
||||||
|
// line, which merging coverage from objects compiled with different options
|
||||||
|
// requires: gcovr, merging the SHOW_MEMORY test with the rest of the build,
|
||||||
|
// otherwise aborts with "Got function ... on multiple lines". The optimizer
|
||||||
|
// removes the resulting empty calls.
|
||||||
#if SHOW_MEMORY
|
#if SHOW_MEMORY
|
||||||
|
|
||||||
int64_t nodeBytes = 0;
|
int64_t nodeBytes = 0;
|
||||||
@@ -6001,7 +6072,10 @@ int64_t getNodeSize(struct Node *n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
int64_t getSearchPathLength(Node *n) {
|
int64_t getSearchPathLength(Node *n) {
|
||||||
|
#if SHOW_MEMORY
|
||||||
assert(n != nullptr);
|
assert(n != nullptr);
|
||||||
int64_t result = 0;
|
int64_t result = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -6013,9 +6087,14 @@ int64_t getSearchPathLength(Node *n) {
|
|||||||
n = n->parent;
|
n = n->parent;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
#else
|
||||||
|
(void)n;
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void addNode(Node *n) {
|
void addNode(Node *n) {
|
||||||
|
#if SHOW_MEMORY
|
||||||
nodeBytes += getNodeSize(n);
|
nodeBytes += getNodeSize(n);
|
||||||
partialCapacityBytes += n->getCapacity();
|
partialCapacityBytes += n->getCapacity();
|
||||||
if (nodeBytes > peakNodeBytes) {
|
if (nodeBytes > peakNodeBytes) {
|
||||||
@@ -6024,14 +6103,22 @@ void addNode(Node *n) {
|
|||||||
if (partialCapacityBytes > peakPartialCapacityBytes) {
|
if (partialCapacityBytes > peakPartialCapacityBytes) {
|
||||||
peakPartialCapacityBytes = partialCapacityBytes;
|
peakPartialCapacityBytes = partialCapacityBytes;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
(void)n;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeNode(Node *n) {
|
void removeNode(Node *n) {
|
||||||
|
#if SHOW_MEMORY
|
||||||
nodeBytes -= getNodeSize(n);
|
nodeBytes -= getNodeSize(n);
|
||||||
partialCapacityBytes -= n->getCapacity();
|
partialCapacityBytes -= n->getCapacity();
|
||||||
|
#else
|
||||||
|
(void)n;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void addKey(Node *n) {
|
void addKey(Node *n) {
|
||||||
|
#if SHOW_MEMORY
|
||||||
if (!n->entryPresent) {
|
if (!n->entryPresent) {
|
||||||
++totalKeys;
|
++totalKeys;
|
||||||
keyBytes += getSearchPathLength(n);
|
keyBytes += getSearchPathLength(n);
|
||||||
@@ -6042,15 +6129,36 @@ void addKey(Node *n) {
|
|||||||
peakKeyBytes = keyBytes;
|
peakKeyBytes = keyBytes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
(void)n;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeKey(Node *n) {
|
void removeKey(Node *n) {
|
||||||
|
#if SHOW_MEMORY
|
||||||
if (n->entryPresent) {
|
if (n->entryPresent) {
|
||||||
--totalKeys;
|
--totalKeys;
|
||||||
keyBytes -= getSearchPathLength(n);
|
keyBytes -= getSearchPathLength(n);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
(void)n;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void removeKey(Node *n, int64_t searchPathLength) {
|
||||||
|
#if SHOW_MEMORY
|
||||||
|
if (n->entryPresent) {
|
||||||
|
--totalKeys;
|
||||||
|
keyBytes -= searchPathLength;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
(void)n;
|
||||||
|
(void)searchPathLength;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SHOW_MEMORY
|
||||||
|
|
||||||
struct __attribute__((visibility("default"))) PeakPrinter {
|
struct __attribute__((visibility("default"))) PeakPrinter {
|
||||||
~PeakPrinter() {
|
~PeakPrinter() {
|
||||||
printf("--- radix_tree ---\n");
|
printf("--- radix_tree ---\n");
|
||||||
|
|||||||
+15
@@ -23,7 +23,9 @@ using namespace weaselab;
|
|||||||
#include <callgrind.h>
|
#include <callgrind.h>
|
||||||
|
|
||||||
#define DEBUG_VERBOSE 0
|
#define DEBUG_VERBOSE 0
|
||||||
|
#ifndef SHOW_MEMORY
|
||||||
#define SHOW_MEMORY 0
|
#define SHOW_MEMORY 0
|
||||||
|
#endif
|
||||||
|
|
||||||
// std::span is not trivially constructible. We want a span that leaves its
|
// std::span is not trivially constructible. We want a span that leaves its
|
||||||
// members uninitialized for performance reasons.
|
// members uninitialized for performance reasons.
|
||||||
@@ -94,8 +96,21 @@ static_assert(std::is_trivial_v<TrivialSpan>);
|
|||||||
// GCOVR_EXCL_START
|
// GCOVR_EXCL_START
|
||||||
|
|
||||||
#if SHOW_MEMORY
|
#if SHOW_MEMORY
|
||||||
|
// Process-wide by design: `safe_malloc`/`safe_free` update these for every
|
||||||
|
// allocation in the process, no matter which ConflictSet made it. An instance's
|
||||||
|
// `totalBytes`, in contrast, only accounts for the allocations made during its
|
||||||
|
// own API calls, so the SHOW_MEMORY cross-check of the two in
|
||||||
|
// `internal_addWrites`/`internal_setOldestVersion` only holds while a single
|
||||||
|
// ConflictSet is alive. Running several at once is supported by the library;
|
||||||
|
// SHOW_MEMORY just doesn't instrument it. `peakMallocBytes` is process-wide
|
||||||
|
// too, but as a peak it remains meaningful with several instances.
|
||||||
inline int64_t mallocBytes = 0;
|
inline int64_t mallocBytes = 0;
|
||||||
inline int64_t peakMallocBytes = 0;
|
inline int64_t peakMallocBytes = 0;
|
||||||
|
// Defined in ConflictSet.cpp
|
||||||
|
extern int64_t nodeBytes;
|
||||||
|
extern int64_t partialCapacityBytes;
|
||||||
|
extern int64_t totalKeys;
|
||||||
|
extern int64_t keyBytes;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline thread_local int64_t mallocBytesDelta = 0;
|
inline thread_local int64_t mallocBytesDelta = 0;
|
||||||
|
|||||||
+70
-2
@@ -96,7 +96,7 @@ inline int firstNeqStride(const uint8_t *ap, const uint8_t *bp) {
|
|||||||
// GCOVR_EXCL_START
|
// GCOVR_EXCL_START
|
||||||
#if defined(HAS_AVX) && !defined(__SANITIZE_THREAD__)
|
#if defined(HAS_AVX) && !defined(__SANITIZE_THREAD__)
|
||||||
__attribute__((target("avx512f,avx512bw"))) inline int
|
__attribute__((target("avx512f,avx512bw"))) inline int
|
||||||
longestCommonPrefix(const uint8_t *ap, const uint8_t *bp, int cl) {
|
longestCommonPrefixLong(const uint8_t *ap, const uint8_t *bp, int cl) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int end = cl & ~63;
|
int end = cl & ~63;
|
||||||
while (i < end) {
|
while (i < end) {
|
||||||
@@ -123,7 +123,7 @@ __attribute__((target("default")))
|
|||||||
// GCOVR_EXCL_STOP
|
// GCOVR_EXCL_STOP
|
||||||
|
|
||||||
inline int
|
inline int
|
||||||
longestCommonPrefix(const uint8_t *ap, const uint8_t *bp, int cl) {
|
longestCommonPrefixLong(const uint8_t *ap, const uint8_t *bp, int cl) {
|
||||||
if (!(cl >= 0)) {
|
if (!(cl >= 0)) {
|
||||||
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
||||||
}
|
}
|
||||||
@@ -183,3 +183,71 @@ longestCommonPrefix(const uint8_t *ap, const uint8_t *bp, int cl) {
|
|||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
// Use in-bounds native-word loads for short common lengths. The second load
|
||||||
|
// overlaps the first; because the first load matched, any mismatch found in
|
||||||
|
// the overlap is impossible and the returned absolute index is still correct.
|
||||||
|
// Precondition: cl <= 2 * sizeof(uint64_t)
|
||||||
|
__attribute__((always_inline)) static inline int
|
||||||
|
longestCommonPrefixShort(const uint8_t *ap, const uint8_t *bp, int cl) {
|
||||||
|
assert(cl <= 2 * int(sizeof(uint64_t)));
|
||||||
|
if (cl >= 8) {
|
||||||
|
uint64_t a0;
|
||||||
|
uint64_t b0;
|
||||||
|
memcpy(&a0, ap, sizeof(a0));
|
||||||
|
memcpy(&b0, bp, sizeof(b0));
|
||||||
|
if (uint64_t mismatched = a0 ^ b0) {
|
||||||
|
return std::countr_zero(mismatched) / 8;
|
||||||
|
}
|
||||||
|
if (cl == 8) {
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t a1;
|
||||||
|
uint64_t b1;
|
||||||
|
memcpy(&a1, ap + cl - 8, sizeof(a1));
|
||||||
|
memcpy(&b1, bp + cl - 8, sizeof(b1));
|
||||||
|
if (uint64_t mismatched = a1 ^ b1) {
|
||||||
|
return cl - 8 + std::countr_zero(mismatched) / 8;
|
||||||
|
}
|
||||||
|
return cl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cl >= 4) {
|
||||||
|
uint32_t a0;
|
||||||
|
uint32_t b0;
|
||||||
|
memcpy(&a0, ap, sizeof(a0));
|
||||||
|
memcpy(&b0, bp, sizeof(b0));
|
||||||
|
if (uint32_t mismatched = a0 ^ b0) {
|
||||||
|
return std::countr_zero(mismatched) / 8;
|
||||||
|
}
|
||||||
|
if (cl == 4) {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t a1;
|
||||||
|
uint32_t b1;
|
||||||
|
memcpy(&a1, ap + cl - 4, sizeof(a1));
|
||||||
|
memcpy(&b1, bp + cl - 4, sizeof(b1));
|
||||||
|
if (uint32_t mismatched = a1 ^ b1) {
|
||||||
|
return cl - 4 + std::countr_zero(mismatched) / 8;
|
||||||
|
}
|
||||||
|
return cl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (i < cl && ap[i] == bp[i]) {
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((always_inline)) static inline int
|
||||||
|
longestCommonPrefix(const uint8_t *ap, const uint8_t *bp, int cl) {
|
||||||
|
if (!(cl >= 0)) {
|
||||||
|
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
||||||
|
}
|
||||||
|
if (cl <= 2 * int(sizeof(uint64_t))) {
|
||||||
|
return longestCommonPrefixShort(ap, bp, cl);
|
||||||
|
}
|
||||||
|
return longestCommonPrefixLong(ap, bp, cl);
|
||||||
|
}
|
||||||
@@ -19,30 +19,30 @@ InstalledDir: /usr/lib/llvm-21/bin
|
|||||||
|
|
||||||
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|
||||||
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
|
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
|
||||||
| 168.06 | 5,950,172.07 | 0.0% | 3,107.03 | 615.58 | 5.047 | 558.59 | 0.0% | 2.01 | `point reads`
|
| 165.93 | 6,026,520.04 | 0.1% | 3,107.03 | 610.42 | 5.090 | 558.59 | 0.0% | 1.98 | `point reads`
|
||||||
| 164.93 | 6,063,229.09 | 0.1% | 3,036.76 | 604.27 | 5.025 | 539.35 | 0.0% | 1.97 | `prefix reads`
|
| 163.08 | 6,132,046.57 | 0.1% | 3,036.76 | 599.68 | 5.064 | 539.35 | 0.0% | 1.95 | `prefix reads`
|
||||||
| 242.64 | 4,121,298.49 | 0.1% | 3,722.71 | 891.62 | 4.175 | 692.00 | 0.0% | 2.90 | `range reads`
|
| 250.26 | 3,995,834.21 | 0.1% | 3,722.71 | 920.25 | 4.045 | 692.00 | 0.0% | 2.99 | `range reads`
|
||||||
| 361.34 | 2,767,480.63 | 0.0% | 4,523.64 | 1,328.61 | 3.405 | 720.22 | 2.1% | 4.31 | `point writes`
|
| 359.64 | 2,780,581.63 | 0.1% | 4,523.64 | 1,322.63 | 3.420 | 720.22 | 2.1% | 4.30 | `point writes`
|
||||||
| 349.52 | 2,861,056.44 | 0.1% | 4,484.57 | 1,279.55 | 3.505 | 705.00 | 1.9% | 4.19 | `prefix writes`
|
| 358.20 | 2,791,726.93 | 0.1% | 4,484.57 | 1,317.23 | 3.405 | 705.00 | 2.0% | 4.28 | `prefix writes`
|
||||||
| 194.68 | 5,136,536.22 | 0.1% | 2,224.10 | 712.83 | 3.120 | 377.17 | 3.3% | 2.33 | `range writes`
|
| 193.91 | 5,157,081.29 | 0.1% | 2,224.10 | 713.33 | 3.118 | 377.17 | 3.3% | 2.32 | `range writes`
|
||||||
| 416.48 | 2,401,076.73 | 1.0% | 6,855.96 | 1,522.78 | 4.502 | 1,227.82 | 1.4% | 0.05 | `monotonic increasing point writes`
|
| 422.03 | 2,369,513.38 | 1.1% | 6,855.96 | 1,551.55 | 4.419 | 1,227.82 | 1.4% | 0.05 | `monotonic increasing point writes`
|
||||||
| 131,614.80 | 7,597.93 | 1.0% | 812,044.75 | 484,589.00 | 1.676 | 151,246.50 | 0.1% | 0.01 | `worst case for radix tree`
|
| 142,467.00 | 7,019.17 | 3.8% | 812,047.33 | 508,535.40 | 1.597 | 151,246.50 | 1.0% | 0.01 | `worst case for radix tree`
|
||||||
| 38.24 | 26,148,048.33 | 1.5% | 701.00 | 140.19 | 5.001 | 102.00 | 0.0% | 0.01 | `create and destroy`
|
| 37.66 | 26,554,217.74 | 0.8% | 701.00 | 138.80 | 5.050 | 102.00 | 0.0% | 0.01 | `create and destroy`
|
||||||
|
|
||||||
## Radix tree (this implementation)
|
## Radix tree (this implementation)
|
||||||
|
|
||||||
|
|
||||||
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|
| ns/op | op/s | err% | ins/op | cyc/op | IPC | bra/op | miss% | total | benchmark
|
||||||
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
|
|--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:----------
|
||||||
| 12.15 | 82,299,987.93 | 0.3% | 233.49 | 44.55 | 5.241 | 31.23 | 0.7% | 0.15 | `point reads`
|
| 12.25 | 81,664,669.29 | 0.4% | 233.49 | 44.94 | 5.196 | 31.23 | 0.7% | 0.15 | `point reads`
|
||||||
| 14.31 | 69,893,290.91 | 0.2% | 283.68 | 52.40 | 5.414 | 40.16 | 0.4% | 0.17 | `prefix reads`
|
| 13.35 | 74,903,950.49 | 0.1% | 271.75 | 49.10 | 5.534 | 40.21 | 0.4% | 0.16 | `prefix reads`
|
||||||
| 33.44 | 29,901,162.28 | 0.2% | 714.70 | 122.88 | 5.816 | 100.97 | 0.2% | 0.40 | `range reads`
|
| 31.37 | 31,874,583.62 | 0.1% | 700.39 | 115.43 | 6.067 | 100.83 | 0.2% | 0.37 | `range reads`
|
||||||
| 19.73 | 50,684,375.53 | 0.1% | 369.51 | 72.61 | 5.089 | 47.92 | 0.5% | 0.24 | `point writes`
|
| 19.55 | 51,153,057.80 | 0.1% | 366.51 | 71.90 | 5.098 | 47.92 | 0.5% | 0.23 | `point writes`
|
||||||
| 38.10 | 26,248,730.55 | 0.0% | 662.02 | 140.17 | 4.723 | 100.26 | 0.3% | 0.45 | `prefix writes`
|
| 36.92 | 27,088,344.17 | 0.1% | 645.11 | 135.72 | 4.753 | 100.31 | 0.4% | 0.44 | `prefix writes`
|
||||||
| 39.76 | 25,152,283.94 | 0.0% | 725.90 | 146.25 | 4.964 | 111.09 | 0.1% | 0.48 | `range writes`
|
| 36.84 | 27,143,553.08 | 0.1% | 691.04 | 135.42 | 5.103 | 114.01 | 0.1% | 0.45 | `range writes`
|
||||||
| 80.73 | 12,387,533.79 | 4.3% | 1,424.45 | 296.99 | 4.796 | 271.47 | 0.1% | 0.01 | `monotonic increasing point writes`
|
| 74.62 | 13,400,467.72 | 0.9% | 1,418.37 | 274.20 | 5.173 | 273.39 | 0.1% | 0.01 | `monotonic increasing point writes`
|
||||||
| 310,683.00 | 3,218.71 | 2.9% | 3,866,495.00 | 1,131,201.00 | 3.418 | 713,374.00 | 0.1% | 0.01 | `worst case for radix tree`
|
| 352,671.00 | 2,835.50 | 15.3% | 3,687,535.00 | 1,287,119.00 | 2.865 | 712,030.00 | 0.1% | 0.01 | :wavy_dash: `worst case for radix tree` (Unstable with ~1.0 iters. Increase `minEpochIterations` to e.g. 10)
|
||||||
| 101.32 | 9,869,495.84 | 0.9% | 1,775.00 | 372.75 | 4.762 | 288.00 | 0.0% | 0.01 | `create and destroy`
|
| 96.63 | 10,348,729.42 | 1.2% | 1,766.00 | 355.53 | 4.967 | 283.00 | 0.0% | 0.01 | `create and destroy`
|
||||||
|
|
||||||
# "Real data" test
|
# "Real data" test
|
||||||
|
|
||||||
@@ -51,13 +51,13 @@ Point queries only. Gc ratio is the ratio of time spent doing garbage collection
|
|||||||
## skip list
|
## skip list
|
||||||
|
|
||||||
```
|
```
|
||||||
Check: 4.65794 seconds, 350.058 MB/s, Add: 3.30748 seconds, 169.511 MB/s, Gc ratio: 38.2502%, Peak idle memory: 5.51852e+06
|
Check: 4.69875 seconds, 347.017 MB/s, Add: 3.39475 seconds, 165.153 MB/s, Gc ratio: 38.7414%, Peak idle memory: 5.51852e+06
|
||||||
```
|
```
|
||||||
|
|
||||||
## radix tree
|
## radix tree
|
||||||
|
|
||||||
```
|
```
|
||||||
Check: 0.984726 seconds, 1655.84 MB/s, Add: 1.22965 seconds, 455.946 MB/s, Gc ratio: 38.8292%, Peak idle memory: 2.0226e+06
|
Check: 0.976745 seconds, 1669.37 MB/s, Add: 1.20464 seconds, 465.411 MB/s, Gc ratio: 39.4158%, Peak idle memory: 2.0226e+06
|
||||||
```
|
```
|
||||||
|
|
||||||
## hash table
|
## hash table
|
||||||
@@ -65,6 +65,6 @@ Check: 0.984726 seconds, 1655.84 MB/s, Add: 1.22965 seconds, 455.946 MB/s, Gc ra
|
|||||||
(The hash table implementation doesn't work on range queries, and its purpose is to provide an idea of how fast point queries can be)
|
(The hash table implementation doesn't work on range queries, and its purpose is to provide an idea of how fast point queries can be)
|
||||||
|
|
||||||
```
|
```
|
||||||
Check: 0.864098 seconds, 1886.99 MB/s, Add: 0.636882 seconds, 880.31 MB/s, Gc ratio: 41.0631%, Peak idle memory: 0
|
Check: 0.864589 seconds, 1885.92 MB/s, Add: 0.634604 seconds, 883.47 MB/s, Gc ratio: 41.184%, Peak idle memory: 0
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
// Exercises the SHOW_MEMORY instrumentation, which the other tests don't
|
||||||
|
// compile with (this target passes -DSHOW_MEMORY=1). The instrumentation has
|
||||||
|
// its own consistency checks, which abort on failure, and asserts are enabled
|
||||||
|
// by TEST_FLAGS. Only one ConflictSet may exist at a time: the accounting is
|
||||||
|
// process-wide, while the cross-check compares it against a single instance's
|
||||||
|
// totals (see the comment on `mallocBytes` in Internal.h).
|
||||||
|
#include "ConflictSet.h"
|
||||||
|
#include "Internal.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace weaselab;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void writePoint(ConflictSet &cs, const std::string &key, int64_t writeVersion) {
|
||||||
|
ConflictSet::WriteRange w{{(const uint8_t *)key.data(), int(key.size())},
|
||||||
|
{nullptr, 0}};
|
||||||
|
cs.addWrites(&w, 1, writeVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConflictSet::Result checkPoint(const ConflictSet &cs, const std::string &key,
|
||||||
|
int64_t readVersion) {
|
||||||
|
ConflictSet::ReadRange r{{(const uint8_t *)key.data(), int(key.size())},
|
||||||
|
{nullptr, 0},
|
||||||
|
readVersion};
|
||||||
|
ConflictSet::Result result;
|
||||||
|
cs.check(&r, &result, 1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything a ConflictSet allocated must have been accounted for and released
|
||||||
|
// by the time it's destroyed.
|
||||||
|
void checkFullyReleased() {
|
||||||
|
assert(nodeBytes == 0);
|
||||||
|
assert(partialCapacityBytes == 0);
|
||||||
|
assert(totalKeys == 0);
|
||||||
|
assert(keyBytes == 0);
|
||||||
|
assert(mallocBytes == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Splitting a node replaces it, which defers the release of the old node.
|
||||||
|
// Releasing a deferred node must account for its capacity, and the key it
|
||||||
|
// held must be accounted for without walking `parent` pointers of nodes that
|
||||||
|
// have already been released.
|
||||||
|
{
|
||||||
|
ConflictSet cs{0};
|
||||||
|
writePoint(cs, "a", 1);
|
||||||
|
writePoint(cs, "ab", 2);
|
||||||
|
assert(checkPoint(cs, "a", 0) == ConflictSet::Conflict);
|
||||||
|
assert(checkPoint(cs, "ab", 0) == ConflictSet::Conflict);
|
||||||
|
assert(checkPoint(cs, "b", 0) == ConflictSet::Commit);
|
||||||
|
assert(nodeBytes > 0);
|
||||||
|
assert(keyBytes > 0);
|
||||||
|
}
|
||||||
|
checkFullyReleased();
|
||||||
|
|
||||||
|
// Exceeding the version window erases the tree mid-workload.
|
||||||
|
{
|
||||||
|
ConflictSet cs{0};
|
||||||
|
writePoint(cs, "abc", 1);
|
||||||
|
const int64_t version = int64_t(1) << 40;
|
||||||
|
writePoint(cs, "abc", version);
|
||||||
|
assert(checkPoint(cs, "abc", 0) == ConflictSet::TooOld);
|
||||||
|
assert(checkPoint(cs, "abc", version) == ConflictSet::Commit);
|
||||||
|
}
|
||||||
|
checkFullyReleased();
|
||||||
|
|
||||||
|
// Erase a tree with many nodes, both mid-workload and at destruction.
|
||||||
|
{
|
||||||
|
ConflictSet cs{0};
|
||||||
|
for (int i = 0; i < 1000; ++i) {
|
||||||
|
writePoint(cs, "prefix/" + std::to_string(i), i + 1);
|
||||||
|
}
|
||||||
|
const int64_t version = int64_t(1) << 40;
|
||||||
|
writePoint(cs, "prefix/1", version);
|
||||||
|
assert(checkPoint(cs, "prefix/1", version) == ConflictSet::Commit);
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; ++i) {
|
||||||
|
writePoint(cs, "other/" + std::to_string(i), version + i + 1);
|
||||||
|
}
|
||||||
|
assert(checkPoint(cs, "other/999", version) == ConflictSet::Conflict);
|
||||||
|
}
|
||||||
|
checkFullyReleased();
|
||||||
|
|
||||||
|
// Grow a node through every node type, so that the accounting for each node
|
||||||
|
// type's size is exercised, then erase the entries one at a time so that
|
||||||
|
// nodes are released individually rather than as part of a whole tree.
|
||||||
|
{
|
||||||
|
ConflictSet cs{0};
|
||||||
|
std::string key(1, '\0');
|
||||||
|
for (int i = 0; i < 256; ++i) {
|
||||||
|
key[0] = char(i);
|
||||||
|
writePoint(cs, key, i + 1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 256; ++i) {
|
||||||
|
key[0] = char(i);
|
||||||
|
assert(checkPoint(cs, key, i) == ConflictSet::Conflict);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Garbage collection erases entries that can only fail with TooOld anyway.
|
||||||
|
// Doing it one version at a time walks the tree, erasing, downsizing and
|
||||||
|
// releasing nodes as it goes.
|
||||||
|
for (int i = 0; i < 256; ++i) {
|
||||||
|
cs.setOldestVersion(i + 1);
|
||||||
|
}
|
||||||
|
key[0] = '\0';
|
||||||
|
assert(checkPoint(cs, key, 0) == ConflictSet::TooOld);
|
||||||
|
}
|
||||||
|
checkFullyReleased();
|
||||||
|
}
|
||||||
+30
-5
@@ -31,7 +31,9 @@
|
|||||||
std::span<const uint8_t> keyAfter(Arena &arena, std::span<const uint8_t> key) {
|
std::span<const uint8_t> keyAfter(Arena &arena, std::span<const uint8_t> key) {
|
||||||
auto result =
|
auto result =
|
||||||
std::span<uint8_t>(new (arena) uint8_t[key.size() + 1], key.size() + 1);
|
std::span<uint8_t>(new (arena) uint8_t[key.size() + 1], key.size() + 1);
|
||||||
memcpy(result.data(), key.data(), key.size());
|
if (key.size() > 0) {
|
||||||
|
memcpy(result.data(), key.data(), key.size());
|
||||||
|
}
|
||||||
result[result.size() - 1] = 0;
|
result[result.size() - 1] = 0;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -50,6 +52,14 @@ using Version = int64_t;
|
|||||||
#define force_inline inline __attribute__((always_inline))
|
#define force_inline inline __attribute__((always_inline))
|
||||||
using StringRef = std::span<const uint8_t>;
|
using StringRef = std::span<const uint8_t>;
|
||||||
|
|
||||||
|
// memcmp, but safe for zero-length ranges: memcmp is undefined behavior unless
|
||||||
|
// both pointers are valid, even when the count is 0, and spans for the empty
|
||||||
|
// key are allowed to have a null data().
|
||||||
|
static force_inline bool equalBytes(const uint8_t *a, const uint8_t *b,
|
||||||
|
int size) {
|
||||||
|
return size == 0 || memcmp(a, b, size) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct KeyRangeRef {
|
struct KeyRangeRef {
|
||||||
StringRef begin;
|
StringRef begin;
|
||||||
StringRef end;
|
StringRef end;
|
||||||
@@ -180,7 +190,8 @@ void sortPoints(std::vector<KeyInfo> &points) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int compare(const StringRef &a, const StringRef &b) {
|
static int compare(const StringRef &a, const StringRef &b) {
|
||||||
int c = memcmp(a.data(), b.data(), std::min(a.size(), b.size()));
|
int n = std::min(a.size(), b.size());
|
||||||
|
int c = n > 0 ? memcmp(a.data(), b.data(), n) : 0;
|
||||||
if (c < 0)
|
if (c < 0)
|
||||||
return -1;
|
return -1;
|
||||||
if (c > 0)
|
if (c > 0)
|
||||||
@@ -371,7 +382,7 @@ public:
|
|||||||
Node *n = finger[0]->getNext(
|
Node *n = finger[0]->getNext(
|
||||||
0); // or alreadyChecked, but that is more easily invalidated
|
0); // or alreadyChecked, but that is more easily invalidated
|
||||||
if (n && n->length() == value.size() &&
|
if (n && n->length() == value.size() &&
|
||||||
!memcmp(n->value(), value.data(), value.size()))
|
equalBytes(n->value(), value.data(), value.size()))
|
||||||
return n;
|
return n;
|
||||||
else
|
else
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -695,7 +706,8 @@ private:
|
|||||||
s = nextS;
|
s = nextS;
|
||||||
if (start.finished()) {
|
if (start.finished()) {
|
||||||
if (nextS->length() == start.value.size() &&
|
if (nextS->length() == start.value.size() &&
|
||||||
!memcmp(nextS->value(), start.value.data(), start.value.size()))
|
equalBytes(nextS->value(), start.value.data(),
|
||||||
|
start.value.size()))
|
||||||
return noConflict();
|
return noConflict();
|
||||||
else
|
else
|
||||||
return conflict();
|
return conflict();
|
||||||
@@ -819,10 +831,17 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setOldestVersion(int64_t oldestVersion) {
|
void setOldestVersion(int64_t oldestVersion) {
|
||||||
|
// oldestVersion becomes the max of its previous value and the argument, so
|
||||||
|
// an older version is a no-op. We still fall through in the equal case so
|
||||||
|
// that garbage collection can make progress without advancing the oldest
|
||||||
|
// version.
|
||||||
|
if (oldestVersion < this->oldestVersion) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// This isn't 100% accurate. It overcounts if you hit the end
|
// This isn't 100% accurate. It overcounts if you hit the end
|
||||||
gc_iterations_total.add(keyUpdates);
|
gc_iterations_total.add(keyUpdates);
|
||||||
|
|
||||||
assert(oldestVersion >= this->oldestVersion);
|
|
||||||
this->oldestVersion = oldestVersion;
|
this->oldestVersion = oldestVersion;
|
||||||
SkipList::Finger finger;
|
SkipList::Finger finger;
|
||||||
int temp;
|
int temp;
|
||||||
@@ -908,6 +927,9 @@ void internal_addWrites(ConflictSet::Impl *impl,
|
|||||||
impl->totalBytes += mallocBytesDelta;
|
impl->totalBytes += mallocBytesDelta;
|
||||||
impl->memory_bytes.set(impl->totalBytes);
|
impl->memory_bytes.set(impl->totalBytes);
|
||||||
#if SHOW_MEMORY
|
#if SHOW_MEMORY
|
||||||
|
// Testing-only cross-check of the accounting. `mallocBytes` is process-wide,
|
||||||
|
// while `impl->totalBytes` only covers this instance, so this holds only
|
||||||
|
// while this is the only live ConflictSet (see Internal.h).
|
||||||
if (impl->totalBytes != mallocBytes) {
|
if (impl->totalBytes != mallocBytes) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
@@ -920,6 +942,9 @@ void internal_setOldestVersion(ConflictSet::Impl *impl, int64_t oldestVersion) {
|
|||||||
impl->totalBytes += mallocBytesDelta;
|
impl->totalBytes += mallocBytesDelta;
|
||||||
impl->memory_bytes.set(impl->totalBytes);
|
impl->memory_bytes.set(impl->totalBytes);
|
||||||
#if SHOW_MEMORY
|
#if SHOW_MEMORY
|
||||||
|
// Testing-only cross-check of the accounting. `mallocBytes` is process-wide,
|
||||||
|
// while `impl->totalBytes` only covers this instance, so this holds only
|
||||||
|
// while this is the only live ConflictSet (see Internal.h).
|
||||||
if (impl->totalBytes != mallocBytes) {
|
if (impl->totalBytes != mallocBytes) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-5
@@ -1,4 +1,6 @@
|
|||||||
import struct
|
import struct
|
||||||
|
|
||||||
|
import conflict_set
|
||||||
from conflict_set import *
|
from conflict_set import *
|
||||||
|
|
||||||
|
|
||||||
@@ -68,6 +70,35 @@ def test_hash_table_getBytes():
|
|||||||
assert cs.check(read(0, b"key")) == [Result.CONFLICT]
|
assert cs.check(read(0, b"key")) == [Result.CONFLICT]
|
||||||
|
|
||||||
|
|
||||||
|
def test_setOldestVersion_does_not_decrease():
|
||||||
|
# Regression test for issue #87: `setOldestVersion` sets oldestVersion to
|
||||||
|
# the maximum of its previous value and its argument, so passing an older
|
||||||
|
# version is valid input and must be a no-op. skip_list used to assert in
|
||||||
|
# Debug builds and, in Release builds, lower its oldest version, which
|
||||||
|
# resurrected a window of versions that callers were told resulted in
|
||||||
|
# TooOld.
|
||||||
|
with DebugConflictSet() as cs:
|
||||||
|
cs.addWrites(5, write(b"k"))
|
||||||
|
cs.setOldestVersion(5)
|
||||||
|
# Valid: `2 >= 0` and `2 <= newestVersion == 5`. oldestVersion stays 5,
|
||||||
|
# so 3 and 4 are still TooOld.
|
||||||
|
cs.setOldestVersion(2)
|
||||||
|
assert cs.check(read(3, b"k")) == [Result.TOO_OLD]
|
||||||
|
assert cs.check(read(4, b"k")) == [Result.TOO_OLD]
|
||||||
|
assert cs.check(read(5, b"k")) == [Result.COMMIT]
|
||||||
|
|
||||||
|
# Same, but with the entries at the old oldestVersion already garbage
|
||||||
|
# collected, so lowering oldestVersion would report Commit for a version
|
||||||
|
# that the caller was told resulted in TooOld.
|
||||||
|
with DebugConflictSet() as cs:
|
||||||
|
cs.addWrites(1, write(b"a"))
|
||||||
|
cs.addWrites(2) # No writes; advances newestVersion to 2
|
||||||
|
cs.setOldestVersion(2) # Garbage collects the write at version 1
|
||||||
|
cs.setOldestVersion(1)
|
||||||
|
assert cs.check(read(1, b"a")) == [Result.TOO_OLD]
|
||||||
|
assert cs.check(read(2, b"a")) == [Result.COMMIT]
|
||||||
|
|
||||||
|
|
||||||
def test_hash_table_setOldestVersion_empties_map():
|
def test_hash_table_setOldestVersion_empties_map():
|
||||||
# Regression test for issue #78: setOldestVersion must terminate when
|
# Regression test for issue #78: setOldestVersion must terminate when
|
||||||
# its garbage-collection pass erases every entry in the map. Each write
|
# its garbage-collection pass erases every entry in the map. Each write
|
||||||
@@ -222,13 +253,37 @@ def test_merge_child_node48():
|
|||||||
cs.addWrites(1, write(b"\x00" * 8, b"\x00" * 10))
|
cs.addWrites(1, write(b"\x00" * 8, b"\x00" * 10))
|
||||||
|
|
||||||
|
|
||||||
def test_fixup_256():
|
def test_empty_key_null_pointer():
|
||||||
|
# Regression test for issue #96: the header only constrains the length of a
|
||||||
|
# key, so a zero-length key may be spelled with a null pointer. Both
|
||||||
|
# implementations must agree on such a key, and must not pass the null
|
||||||
|
# pointer to memcpy/memcmp, which is undefined behavior even when the length
|
||||||
|
# is 0 (keyAfter() in SkipList.cpp did, on every empty-key point operation).
|
||||||
|
null_key = conflict_set._Key(None, 0)
|
||||||
|
with DebugConflictSet() as cs:
|
||||||
|
cs.addWrites(1, WriteRange(null_key, null_key))
|
||||||
|
assert cs.check(ReadRange(null_key, null_key, 0)) == [Result.CONFLICT]
|
||||||
|
assert cs.check(ReadRange(null_key, null_key, 1)) == [Result.COMMIT]
|
||||||
|
|
||||||
|
# The same operations spelled with a non-null empty key must agree.
|
||||||
|
with DebugConflictSet() as cs:
|
||||||
|
cs.addWrites(1, write(b""))
|
||||||
|
assert cs.check(read(0, b"")) == [Result.CONFLICT]
|
||||||
|
assert cs.check(read(1, b"")) == [Result.COMMIT]
|
||||||
|
|
||||||
|
|
||||||
|
def test_fixup_256_heterogeneous_versions():
|
||||||
|
# Regression test for issue #89: recomputing the maxVersion of a Node256
|
||||||
|
# must consider all of its children, not just those with an index < 16.
|
||||||
|
# Here the highest-version child ("\x10\x50") has an index >= 16, and the
|
||||||
|
# range write ends at the Node256, which erases that child from the range
|
||||||
|
# and recomputes the Node256's maxVersion.
|
||||||
with DebugConflictSet() as cs:
|
with DebugConflictSet() as cs:
|
||||||
cs.addWrites(0, write(bytes([1])))
|
|
||||||
for i in range(256):
|
for i in range(256):
|
||||||
cs.addWrites(1, write(bytes([1, i])))
|
cs.addWrites(1, write(bytes([0x10, i])))
|
||||||
cs.addWrites(2, write(bytes([0]), bytes([1])))
|
cs.addWrites(10, write(bytes([0x10, 0x50])))
|
||||||
cs.check(read(0, bytes([1]), bytes([2])))
|
cs.addWrites(11, write(bytes([0x05]), bytes([0x10])))
|
||||||
|
assert cs.check(read(5, bytes([0x10, 0x50]))) == [Result.CONFLICT]
|
||||||
|
|
||||||
|
|
||||||
def test_large_removal_buffer():
|
def test_large_removal_buffer():
|
||||||
|
|||||||
Reference in new issue
Block a user