Skip to content

Commit 14554d9

Browse files
committed
refactor: rename intrusive_string_set to OAHSet
1 parent b29ca11 commit 14554d9

File tree

4 files changed

+103
-116
lines changed

4 files changed

+103
-116
lines changed

src/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cxx_test(interpreter_test dfly_core LABELS DFLY)
4040

4141
cxx_test(string_set_test dfly_core LABELS DFLY)
4242
cxx_test(string_map_test dfly_core LABELS DFLY)
43-
cxx_test(intrusive_string_set_test dfly_core LABELS DFLY)
43+
cxx_test(oah_set_test dfly_core LABELS DFLY)
4444
cxx_test(sorted_map_test dfly_core redis_test_lib LABELS DFLY)
4545
cxx_test(bptree_set_test dfly_core LABELS DFLY)
4646
cxx_test(score_map_test dfly_core LABELS DFLY)
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static uint32_t BucketId(uint64_t hash, uint32_t capacity_log) {
2727
return hash >> (64 - capacity_log);
2828
}
2929
// doesn't possess memory, it should be created and release manually
30-
class ISLEntry {
30+
class OAHEntry {
3131
// we can assume that high 12 bits of user address space
3232
// can be used for tagging. At most 52 bits of address are reserved for
3333
// some configurations, and usually it's 48 bits.
@@ -47,9 +47,9 @@ class ISLEntry {
4747
static constexpr size_t kTagMask = 4095ULL << 52; // we reserve 12 high bits.
4848

4949
public:
50-
ISLEntry() = default;
50+
OAHEntry() = default;
5151

52-
ISLEntry(std::string_view key, uint32_t expiry = UINT32_MAX) {
52+
OAHEntry(std::string_view key, uint32_t expiry = UINT32_MAX) {
5353
uint32_t key_size = key.size();
5454

5555
uint32_t expiry_size = (expiry != UINT32_MAX) * sizeof(expiry);
@@ -80,25 +80,25 @@ class ISLEntry {
8080
}
8181

8282
// TODO add initializer list constructor
83-
ISLEntry(size_t vector_size) {
83+
OAHEntry(size_t vector_size) {
8484
// TODO rewrite to simple array
85-
data_ = reinterpret_cast<char*>(new std::vector<ISLEntry>(vector_size));
85+
data_ = reinterpret_cast<char*>(new std::vector<OAHEntry>(vector_size));
8686
SetVectorBit();
8787
}
8888

89-
ISLEntry(const ISLEntry& e) = delete;
90-
ISLEntry(ISLEntry&& e) {
89+
OAHEntry(const OAHEntry& e) = delete;
90+
OAHEntry(OAHEntry&& e) {
9191
data_ = e.data_;
9292
e.data_ = nullptr;
9393
}
9494

9595
// consider manual removing, we waste a lot of time to check nullptr
96-
inline ~ISLEntry() {
96+
inline ~OAHEntry() {
9797
Clear();
9898
}
9999

100-
ISLEntry& operator=(const ISLEntry& e) = delete;
101-
ISLEntry& operator=(ISLEntry&& e) {
100+
OAHEntry& operator=(const OAHEntry& e) = delete;
101+
OAHEntry& operator=(OAHEntry&& e) {
102102
std::swap(data_, e.data_);
103103
return *this;
104104
}
@@ -115,8 +115,8 @@ class ISLEntry {
115115
return (uptr() & kVectorBit) != 0;
116116
}
117117

118-
inline std::vector<ISLEntry>& AsVector() {
119-
return *reinterpret_cast<std::vector<ISLEntry>*>(Raw());
118+
inline std::vector<OAHEntry>& AsVector() {
119+
return *reinterpret_cast<std::vector<OAHEntry>*>(Raw());
120120
}
121121

122122
inline std::string_view Key() const {
@@ -139,11 +139,11 @@ class ISLEntry {
139139
}
140140

141141
// TODO consider another option to implement iterator
142-
ISLEntry* operator->() {
142+
OAHEntry* operator->() {
143143
return this;
144144
}
145145

146-
inline uint64_t GetExtendedHash() const {
146+
inline uint64_t GetHash() const {
147147
return (uptr() & kExtHashShiftedMask) >> kExtHashShift;
148148
}
149149

@@ -154,9 +154,9 @@ class ISLEntry {
154154
uint32_t bucket_id_hash_part = capacity_log > shift_log ? shift_log : capacity_log;
155155
uint32_t bucket_mask = (1 << bucket_id_hash_part) - 1;
156156
bucket_id &= bucket_mask;
157-
auto stored_hash = GetExtendedHash();
157+
auto stored_hash = GetHash();
158158
if (!stored_hash) {
159-
stored_hash = SetExtendedHash(Hash(Key()), capacity_log, shift_log);
159+
stored_hash = SetHash(Hash(Key()), capacity_log, shift_log);
160160
}
161161
uint32_t stored_bucket_id = stored_hash >> (kExtHashSize - bucket_id_hash_part);
162162
return bucket_id == stored_bucket_id;
@@ -168,16 +168,16 @@ class ISLEntry {
168168
const uint32_t start_hash_bit = capacity_log > shift_log ? capacity_log - shift_log : 0;
169169
const uint32_t ext_hash_shift = 64 - start_hash_bit - kExtHashSize;
170170
const uint64_t ext_hash = (hash >> ext_hash_shift) & kExtHashMask;
171-
auto stored_hash = GetExtendedHash();
171+
auto stored_hash = GetHash();
172172
if (!stored_hash && !IsVector()) {
173-
stored_hash = SetExtendedHash(Hash(Key()), capacity_log, shift_log);
173+
stored_hash = SetHash(Hash(Key()), capacity_log, shift_log);
174174
}
175175
return stored_hash == ext_hash;
176176
}
177177

178178
// TODO rename to SetHash
179179
// shift_log identify which bucket the element belongs to
180-
uint64_t SetExtendedHash(uint64_t hash, uint32_t capacity_log, uint32_t shift_log) {
180+
uint64_t SetHash(uint64_t hash, uint32_t capacity_log, uint32_t shift_log) {
181181
DCHECK(!IsVector());
182182
const uint32_t start_hash_bit = capacity_log > shift_log ? capacity_log - shift_log : 0;
183183
const uint32_t ext_hash_shift = 64 - start_hash_bit - kExtHashSize;
@@ -195,7 +195,7 @@ class ISLEntry {
195195
uint32_t Rehash(uint32_t current_bucket_id, uint32_t prev_capacity_log, uint32_t new_capacity_log,
196196
uint32_t shift_log) {
197197
DCHECK(!IsVector());
198-
auto stored_hash = GetExtendedHash();
198+
auto stored_hash = GetHash();
199199

200200
const uint32_t logs_diff = new_capacity_log - prev_capacity_log;
201201
const uint32_t prev_significant_bits =
@@ -204,7 +204,7 @@ class ISLEntry {
204204

205205
if (!stored_hash || needed_hash_bits > kExtHashSize) {
206206
auto hash = Hash(Key());
207-
SetExtendedHash(hash, new_capacity_log, shift_log);
207+
SetHash(hash, new_capacity_log, shift_log);
208208
return BucketId(hash, new_capacity_log);
209209
}
210210

@@ -232,7 +232,7 @@ class ISLEntry {
232232
auto* expiry_pos = Raw();
233233
std::memcpy(expiry_pos, &at_sec, sizeof(at_sec));
234234
} else {
235-
*this = ISLEntry(Key(), at_sec);
235+
*this = OAHEntry(Key(), at_sec);
236236
}
237237
}
238238

@@ -266,12 +266,12 @@ class ISLEntry {
266266
}
267267

268268
// TODO refactor, because it's inefficient
269-
inline uint32_t Insert(ISLEntry&& e) {
269+
inline uint32_t Insert(OAHEntry&& e) {
270270
if (Empty()) {
271271
*this = std::move(e);
272272
return 0;
273273
} else if (!IsVector()) {
274-
ISLEntry tmp(2);
274+
OAHEntry tmp(2);
275275
auto& arr = tmp.AsVector();
276276
arr[0] = std::move(*this);
277277
arr[1] = std::move(e);
@@ -300,7 +300,7 @@ class ISLEntry {
300300
}
301301

302302
// TODO remove, it is inefficient
303-
inline ISLEntry& operator[](uint32_t pos) {
303+
inline OAHEntry& operator[](uint32_t pos) {
304304
DCHECK(!Empty());
305305
if (!IsVector()) {
306306
DCHECK(pos == 0);
@@ -312,11 +312,11 @@ class ISLEntry {
312312
}
313313
}
314314

315-
ISLEntry Remove(uint32_t pos) {
315+
OAHEntry Remove(uint32_t pos) {
316316
if (Empty()) {
317317
// I'm not sure that this scenario should be check at all
318318
DCHECK(pos == 0);
319-
return ISLEntry();
319+
return OAHEntry();
320320
} else if (!IsVector()) {
321321
DCHECK(pos == 0);
322322
return std::move(*this);
@@ -327,7 +327,7 @@ class ISLEntry {
327327
}
328328
}
329329

330-
ISLEntry Pop() {
330+
OAHEntry Pop() {
331331
if (IsVector()) {
332332
auto& arr = AsVector();
333333
for (auto& e : arr) {
Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@
1010
#include <vector>
1111

1212
#include "base/pmr/memory_resource.h"
13-
#include "intrusive_string_list.h"
13+
#include "oah_entry.h"
1414

1515
namespace dfly {
1616

17-
// NOTE: do not change the ABI of ISLEntry struct as long as we support
18-
// --stringset_experimental=false
19-
20-
class IntrusiveStringSet {
21-
using Buckets = std::vector<ISLEntry, PMR_NS::polymorphic_allocator<ISLEntry>>;
17+
class OAHSet { // Open Addressing Hash Set
18+
using Buckets = std::vector<OAHEntry, PMR_NS::polymorphic_allocator<OAHEntry>>;
2219

2320
public:
2421
class iterator {
2522
public:
2623
using iterator_category = std::forward_iterator_tag;
2724
using difference_type = std::ptrdiff_t;
28-
using value_type = ISLEntry;
29-
using pointer = ISLEntry*;
30-
using reference = ISLEntry&;
25+
using value_type = OAHEntry;
26+
using pointer = OAHEntry*;
27+
using reference = OAHEntry&;
3128

32-
iterator(IntrusiveStringSet* owner, uint32_t bucket_id, uint32_t pos_in_bucket)
29+
iterator(OAHSet* owner, uint32_t bucket_id, uint32_t pos_in_bucket)
3330
: owner_(owner), bucket_(bucket_id), pos_(pos_in_bucket) {
3431
// TODO rewrite, it's inefficient
3532
SetEntryIt();
@@ -94,7 +91,7 @@ class IntrusiveStringSet {
9491
}
9592

9693
private:
97-
IntrusiveStringSet* owner_;
94+
OAHSet* owner_;
9895
uint32_t bucket_;
9996
uint32_t pos_;
10097
};
@@ -107,8 +104,7 @@ class IntrusiveStringSet {
107104
return iterator(nullptr, 0, 0);
108105
}
109106

110-
explicit IntrusiveStringSet(PMR_NS::memory_resource* mr = PMR_NS::get_default_resource())
111-
: entries_(mr) {
107+
explicit OAHSet(PMR_NS::memory_resource* mr = PMR_NS::get_default_resource()) : entries_(mr) {
112108
}
113109

114110
static constexpr uint32_t kMaxBatchLen = 32;
@@ -122,10 +118,6 @@ class IntrusiveStringSet {
122118

123119
// TODO FindInternal and FindEmptyAround can be one function to get better performance
124120
if (auto item = FindInternal(bucket_id, str, hash); item != end()) {
125-
// Update ttl if found
126-
// if (!keepttl) {
127-
// item->SetExpiryTime(EntryTTL(ttl_sec) /*, &entries_[item.second].obj_malloc_used_*/);
128-
// }
129121
return false;
130122
}
131123

@@ -157,8 +149,8 @@ class IntrusiveStringSet {
157149
uint32_t ttl_sec = UINT32_MAX) {
158150
++size_;
159151
uint32_t at = EntryTTL(ttl_sec);
160-
uint32_t pos = entries_[bucket].Insert(ISLEntry(str, at));
161-
entries_[bucket][pos].SetExtendedHash(hash, capacity_log_, kShiftLog);
152+
uint32_t pos = entries_[bucket].Insert(OAHEntry(str, at));
153+
entries_[bucket][pos].SetHash(hash, capacity_log_, kShiftLog);
162154
return iterator(this, bucket, pos);
163155
}
164156

@@ -173,8 +165,8 @@ class IntrusiveStringSet {
173165
return res;
174166
}
175167

176-
// TODO: Consider using chunks for this as string set
177-
void Fill(IntrusiveStringSet* other) {
168+
// TODO: Consider using chunks for this as in StringSet
169+
void Fill(OAHSet* other) {
178170
DCHECK(other->entries_.empty());
179171
other->Reserve(UpperBoundSize());
180172
other->set_time(time_now());
@@ -200,7 +192,6 @@ class IntrusiveStringSet {
200192

201193
using ItemCb = std::function<void(std::string_view)>;
202194

203-
// TODO fix with CheckExtendedHash
204195
uint32_t Scan(uint32_t cursor, const ItemCb& cb) {
205196
const uint32_t capacity_mask = Capacity() - 1;
206197
uint32_t bucket_id = cursor >> (32 - capacity_log_);
@@ -224,7 +215,7 @@ class IntrusiveStringSet {
224215
return bucket_id << (32 - capacity_log_);
225216
}
226217

227-
ISLEntry Pop() {
218+
OAHEntry Pop() {
228219
for (auto& bucket : entries_) {
229220
if (auto res = bucket.Pop(); !res.Empty()) {
230221
--size_;
@@ -242,7 +233,7 @@ class IntrusiveStringSet {
242233
auto bucket_id = BucketId(hash, capacity_log_);
243234
auto item = FindInternal(bucket_id, str, hash);
244235
if (item != end()) {
245-
*item = ISLEntry();
236+
*item = OAHEntry();
246237
return true;
247238
}
248239
return false;
@@ -290,15 +281,14 @@ class IntrusiveStringSet {
290281
}
291282

292283
size_t ObjMallocUsed() const {
293-
size_t bucket_obj_memory = 0;
294-
// for (const auto& bucket : entries_) {
295-
// bucket_obj_memory += bucket.ObjMallocUsed();
296-
// }
297-
return bucket_obj_memory;
284+
// TODO implement
285+
LOG(FATAL) << "ExpirationUsed() isn't implemented";
286+
return 0;
298287
}
299288

300289
size_t SetMallocUsed() const {
301-
// return entries_.capacity() * sizeof(ISLEntry);
290+
// TODO implement
291+
LOG(FATAL) << "ExpirationUsed() isn't implemented";
302292
return 0;
303293
}
304294

@@ -349,10 +339,7 @@ class IntrusiveStringSet {
349339
const uint32_t bucket_id = (bid + i) & capacity_mask;
350340
if (entries_[bucket_id].Empty())
351341
return bucket_id;
352-
// it->ExpireIfNeeded(time_now_, &size_);
353-
// if (it->Empty()) {
354-
// return it;
355-
// }
342+
// TODO add expiration logic
356343
}
357344

358345
DCHECK(Capacity() > kDisplacementSize);

0 commit comments

Comments
 (0)