Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add Dash::Prefetch function #4476

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/core/dash.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class DashTable : public detail::DashTableBase {
template <typename U> const_iterator Find(U&& key) const;
template <typename U> iterator Find(U&& key);

// Prefetches the memory where the key would resize into the cache.
template <typename U> void Prefetch(U&& key) const;

// Find first entry with given key hash that evaulates to true on pred.
// Pred accepts either (const key&) or (const key&, const value&)
template <typename Pred> iterator FindFirst(uint64_t key_hash, Pred&& pred);
Expand Down Expand Up @@ -699,6 +702,14 @@ auto DashTable<_Key, _Value, Policy>::Find(U&& key) -> iterator {
return FindFirst(DoHash(key), EqPred(key));
}

template <typename _Key, typename _Value, typename Policy>
template <typename U>
void DashTable<_Key, _Value, Policy>::Prefetch(U&& key) const {
uint64_t key_hash = DoHash(key);
uint32_t seg_id = SegmentId(key_hash);
segment_[seg_id]->Prefetch(key_hash);
}

template <typename _Key, typename _Value, typename Policy>
template <typename Pred>
auto DashTable<_Key, _Value, Policy>::FindFirst(uint64_t key_hash, Pred&& pred) -> iterator {
Expand Down
10 changes: 10 additions & 0 deletions src/core/dash_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ template <typename _Key, typename _Value, typename Policy = DefaultSegmentPolicy

// Find item with given key hash and truthy predicate
template <typename Pred> Iterator FindIt(Hash_t key_hash, Pred&& pred) const;
void Prefetch(Hash_t key_hash) const;

// Returns valid iterator if succeeded or invalid if not (it's full).
// Requires: key should be not present in the segment.
Expand Down Expand Up @@ -1188,6 +1189,15 @@ auto Segment<Key, Value, Policy>::FindIt(Hash_t key_hash, Pred&& pred) const ->
return Iterator{};
}

template <typename Key, typename Value, typename Policy>
void Segment<Key, Value, Policy>::Prefetch(Hash_t key_hash) const {
uint8_t bidx = BucketIndex(key_hash);
const Bucket& target = bucket_[bidx];

// Prefetch the home bucket that might hold the key with high probability.
__builtin_prefetch(&target, 0, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the bucket size? if the size of the bucket is more than the cache line maybe it will be better to load the whole bucket

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bucket size is dependent on the key/value size but the bucket metadata is located at the beginning. we use the metadata/fingerprints to do the initial scan.

Copy link
Contributor

@BorysTheDev BorysTheDev Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify what metadata do you want to read, because to my mind it's more than 64 bytes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SlotBitmap<NUM_SLOTS> slotb_;  // allocation bitmap + pointer bitmap + counter

  /*only use the first 14 bytes, can be accelerated by
    SSE instruction,0-13 for finger, 14-17 for overflowed*/
  FpArray finger_arr_;
  StashFpArray stash_arr_;

  uint8_t stash_busy_ = 0;  // kStashFpLen+1 bits are used
  uint8_t stash_pos_ = 0;   // 4x2 bits for pointing to stash bucket.

BucketBase<NUM_SLOTS, NUM_OVR>::CompareFP is the function that usually hits a cold memory

}

template <typename Key, typename Value, typename Policy>
template <typename Cb>
void Segment<Key, Value, Policy>::TraverseAll(Cb&& cb) const {
Expand Down
Loading