summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-05-23 16:18:48 +0200
committerGitHub <noreply@github.com>2023-05-23 16:18:48 +0200
commit9e2fb46e4408f060d037bd23eafca34bf3530f04 (patch)
tree5bdd9addedec63f19e4782e7026ebd9408069521
parent065dc8cd931b2f237d4997d8010083212c9f4989 (diff)
parentc58b5ce1764133acf24e930421f102829fe640d3 (diff)
Merge pull request #27187 from vespa-engine/balder/use-static-assert-for-idx
replace runtime assert with a static assert, and also optimize incIdx…
-rw-r--r--vespalib/src/vespa/vespalib/btree/btreeiterator.h14
-rw-r--r--vespalib/src/vespa/vespalib/btree/btreenode.h7
2 files changed, 8 insertions, 13 deletions
diff --git a/vespalib/src/vespa/vespalib/btree/btreeiterator.h b/vespalib/src/vespa/vespalib/btree/btreeiterator.h
index 14143836c22..7ab28c9b5c9 100644
--- a/vespalib/src/vespa/vespalib/btree/btreeiterator.h
+++ b/vespalib/src/vespa/vespalib/btree/btreeiterator.h
@@ -43,12 +43,13 @@ class NodeElement
static constexpr uint64_t NODE_MASK = (1ul << NODE_BITS) - 1ul;
static constexpr uint64_t IDX_MASK = (1ul << IDX_BITS) - 1ul;
static constexpr uint8_t IDX_SHIFT = NODE_BITS;
+ static constexpr uint64_t IDX_ONE = 1ul << NODE_BITS;
+ static_assert((NodeType::maxSlots() + 1) < (1ul << IDX_BITS), "IDX can be out of bounds above 127");
public:
NodeElement() noexcept : _nodeAndIdx(0ul) { }
NodeElement(const NodeType *node, uint32_t idx) noexcept : _nodeAndIdx(uint64_t(node) | uint64_t(idx) << IDX_SHIFT) {
assert((uint64_t(node) & ~NODE_MASK) == 0ul);
- assert(idx <= IDX_MASK);
}
void invalidate() noexcept { _nodeAndIdx = 0; }
@@ -58,16 +59,14 @@ public:
}
const NodeType * getNode() const noexcept { return reinterpret_cast<const NodeType *>(_nodeAndIdx & NODE_MASK); }
void setIdx(uint32_t idx) noexcept {
- assert(idx <= IDX_MASK);
_nodeAndIdx = (_nodeAndIdx & NODE_MASK) | (uint64_t(idx) << IDX_SHIFT);
}
uint32_t getIdx() const noexcept { return _nodeAndIdx >> IDX_SHIFT; }
- void incIdx() noexcept { setIdx(getIdx() + 1); }
- void decIdx() noexcept { setIdx(getIdx() - 1); }
+ void incIdx() noexcept { _nodeAndIdx += IDX_ONE; }
+ void decIdx() noexcept { _nodeAndIdx -= IDX_ONE; }
void setNodeAndIdx(const NodeType *node, uint32_t idx) noexcept {
assert((uint64_t(node) & ~NODE_MASK) == 0ul);
- assert(idx <= IDX_MASK);
_nodeAndIdx = uint64_t(node) | uint64_t(idx) << IDX_SHIFT;
}
@@ -119,9 +118,7 @@ template <typename KeyT,
class BTreeIteratorBase
{
protected:
- using NodeAllocatorType = BTreeNodeAllocator<KeyT, DataT, AggrT,
- INTERNAL_SLOTS,
- LEAF_SLOTS>;
+ using NodeAllocatorType = BTreeNodeAllocator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>;
using InternalNodeType = BTreeInternalNode<KeyT, AggrT, INTERNAL_SLOTS>;
using LeafNodeType = BTreeLeafNode<KeyT, DataT, AggrT, LEAF_SLOTS> ;
using InternalNodeTypeRefPair = typename InternalNodeType::RefPair;
@@ -161,7 +158,6 @@ protected:
// Temporary leaf node when iterating over short arrays
std::unique_ptr<LeafNodeTempType> _compatLeafNode;
-
private:
/*
* Find the next leaf node, called by operator++() as needed.
diff --git a/vespalib/src/vespa/vespalib/btree/btreenode.h b/vespalib/src/vespa/vespalib/btree/btreenode.h
index cda088556de..0a77a0b4685 100644
--- a/vespalib/src/vespa/vespalib/btree/btreenode.h
+++ b/vespalib/src/vespa/vespalib/btree/btreenode.h
@@ -226,8 +226,8 @@ public:
bool isFull() const noexcept { return validSlots() == NumSlots; }
bool isAtLeastHalfFull() const noexcept { return validSlots() >= minSlots(); }
- static uint32_t maxSlots() noexcept { return NumSlots; }
- static uint32_t minSlots() noexcept { return NumSlots / 2; }
+ static constexpr uint32_t maxSlots() noexcept { return NumSlots; }
+ static constexpr uint32_t minSlots() noexcept { return NumSlots / 2; }
};
template <typename KeyT, typename DataT, typename AggrT, uint32_t NumSlots>
@@ -430,8 +430,7 @@ public:
}
};
-template <typename KeyT, typename DataT, typename AggrT,
- uint32_t NumSlots = 16>
+template <typename KeyT, typename DataT, typename AggrT, uint32_t NumSlots = 16>
class BTreeLeafNode : public BTreeNodeTT<KeyT, DataT, AggrT, NumSlots>
{
public: