Skip to content

Commit 87db441

Browse files
committed
chore: remove duplicate code from dash and simplify
1 parent 16b737c commit 87db441

File tree

4 files changed

+132
-162
lines changed

4 files changed

+132
-162
lines changed

src/core/dash.h

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,21 @@ class DashTable : public detail::DashTableBase {
3434
DashTable(const DashTable&) = delete;
3535
DashTable& operator=(const DashTable&) = delete;
3636

37-
struct SegmentPolicy {
38-
static constexpr unsigned NUM_SLOTS = Policy::kSlotNum;
39-
static constexpr unsigned BUCKET_CNT = Policy::kBucketNum;
40-
static constexpr unsigned STASH_BUCKET_NUM = Policy::kStashBucketNum;
41-
static constexpr bool USE_VERSION = Policy::kUseVersion;
42-
};
43-
4437
using Base = detail::DashTableBase;
45-
using SegmentType = detail::Segment<_Key, _Value, SegmentPolicy>;
38+
using SegmentType = detail::Segment<_Key, _Value, Policy>;
4639
using SegmentIterator = typename SegmentType::Iterator;
4740

4841
public:
4942
using Key_t = _Key;
5043
using Value_t = _Value;
5144
using Segment_t = SegmentType;
5245

53-
//! Number of "official" buckets that are used to position a key. In other words, does not include
54-
//! stash buckets.
55-
static constexpr unsigned kLogicalBucketNum = Policy::kBucketNum;
56-
5746
//! Total number of buckets in a segment (including stash).
58-
static constexpr unsigned kPhysicalBucketNum = SegmentType::kTotalBuckets;
59-
static constexpr unsigned kBucketWidth = Policy::kSlotNum;
6047
static constexpr double kTaxAmount = SegmentType::kTaxSize;
6148
static constexpr size_t kSegBytes = sizeof(SegmentType);
6249
static constexpr size_t kSegCapacity = SegmentType::capacity();
63-
static constexpr bool kUseVersion = Policy::kUseVersion;
50+
static constexpr size_t kSlotNum = SegmentType::kSlotNum;
51+
static constexpr size_t kBucketNum = SegmentType::kBucketNum;
6452

6553
// if IsSingleBucket is true - iterates only over a single bucket.
6654
template <bool IsConst, bool IsSingleBucket = false> class Iterator;
@@ -556,11 +544,11 @@ void DashTable<_Key, _Value, Policy>::CVCUponInsert(uint64_t ver_threshold, cons
556544
return;
557545
}
558546

559-
static_assert(kPhysicalBucketNum < 0xFF, "");
547+
static_assert(SegmentType::kTotalBuckets < 0xFF, "");
560548

561549
// Segment is full, we need to return the whole segment, because it can be split
562550
// and its entries can be reshuffled into different buckets.
563-
for (uint8_t i = 0; i < kPhysicalBucketNum; ++i) {
551+
for (uint8_t i = 0; i < SegmentType::kTotalBuckets; ++i) {
564552
if (target->GetVersion(i) < ver_threshold && !target->GetBucket(i).IsEmpty()) {
565553
cb(bucket_iterator{this, seg_id, i});
566554
}
@@ -646,8 +634,8 @@ bool DashTable<_Key, _Value, Policy>::ShiftRight(bucket_iterator it) {
646634
typename Segment_t::Hash_t hash_val = 0;
647635
auto& bucket = seg->GetBucket(it.bucket_id_);
648636

649-
if (bucket.GetBusy() & (1 << (kBucketWidth - 1))) {
650-
it.slot_id_ = kBucketWidth - 1;
637+
if (bucket.GetBusy() & (1 << (kSlotNum - 1))) {
638+
it.slot_id_ = kSlotNum - 1;
651639
hash_val = DoHash(it->first);
652640
policy_.DestroyKey(it->first);
653641
policy_.DestroyValue(it->second);
@@ -800,7 +788,7 @@ auto DashTable<_Key, _Value, Policy>::InsertInternal(U&& key, V&& value, Evictio
800788

801789
for (unsigned i = 0; i < Policy::kStashBucketNum; ++i) {
802790
hotspot.probes.by_type.stash_buckets[i] =
803-
bucket_iterator{this, target_seg_id, uint8_t(kLogicalBucketNum + i), 0};
791+
bucket_iterator{this, target_seg_id, uint8_t(Policy::kBucketNum + i), 0};
804792
}
805793
hotspot.num_buckets = HotspotBuckets::kNumBuckets;
806794

@@ -910,7 +898,7 @@ auto DashTable<_Key, _Value, Policy>::TraverseBySegmentOrder(Cursor curs, Cb&& c
910898
s->TraverseBucket(bid, std::move(dt_cb));
911899

912900
++bid;
913-
if (bid == kPhysicalBucketNum) {
901+
if (bid == SegmentType::kTotalBuckets) {
914902
sid = NextSeg(sid);
915903
bid = 0;
916904
if (sid >= segment_.size()) {
@@ -924,15 +912,15 @@ auto DashTable<_Key, _Value, Policy>::TraverseBySegmentOrder(Cursor curs, Cb&& c
924912
template <typename _Key, typename _Value, typename Policy>
925913
auto DashTable<_Key, _Value, Policy>::GetRandomCursor(absl::BitGen* bitgen) -> Cursor {
926914
uint32_t sid = absl::Uniform<uint32_t>(*bitgen, 0, segment_.size());
927-
uint8_t bid = absl::Uniform<uint8_t>(*bitgen, 0, kLogicalBucketNum);
915+
uint8_t bid = absl::Uniform<uint8_t>(*bitgen, 0, Policy::kBucketNum);
928916

929917
return Cursor{global_depth_, sid, bid};
930918
}
931919

932920
template <typename _Key, typename _Value, typename Policy>
933921
template <typename Cb>
934922
auto DashTable<_Key, _Value, Policy>::Traverse(Cursor curs, Cb&& cb) -> Cursor {
935-
if (curs.bucket_id() >= kLogicalBucketNum) // sanity.
923+
if (curs.bucket_id() >= Policy::kBucketNum) // sanity.
936924
return 0;
937925

938926
uint32_t sid = curs.segment_id(global_depth_);
@@ -955,7 +943,7 @@ auto DashTable<_Key, _Value, Policy>::Traverse(Cursor curs, Cb&& cb) -> Cursor {
955943
sid = 0;
956944
++bid;
957945

958-
if (bid >= kLogicalBucketNum)
946+
if (bid >= Policy::kBucketNum)
959947
return 0; // "End of traversal" cursor.
960948
}
961949
} while (!fetched);

0 commit comments

Comments
 (0)