diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-05-23 13:19:44 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-05-23 13:19:44 +0000 |
commit | c58b5ce1764133acf24e930421f102829fe640d3 (patch) | |
tree | e3ad37319d0c1f4daa0664ebed5932ed0f5288c4 /vespalib | |
parent | cdda3f7d16c59c8bd61b8779e15f67cecff3f588 (diff) |
replace runtime assert with a static assert, and also optimize incIdx/decIdx
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/btree/btreeiterator.h | 14 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/btree/btreenode.h | 7 |
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: |