@@ -141,31 +141,34 @@ inline void FreeObjZset(unsigned encoding, void* ptr) {
141
141
}
142
142
}
143
143
144
- // Iterates over allocations of internal hash data structed and re-allocates
144
+ // Iterates over allocations of internal hash data structures and re-allocates
145
145
// them if their pages are underutilized.
146
- void * DefragHash (MemoryResource* mr, unsigned encoding, void * ptr, float ratio) {
146
+ // Returns pointer to new object ptr and whether any re-allocations happened.
147
+ pair<void *, bool > DefragHash (MemoryResource* mr, unsigned encoding, void * ptr, float ratio) {
147
148
switch (encoding) {
148
149
// Listpack is stored as a single contiguous array
149
150
case kEncodingListPack : {
150
151
uint8_t * lp = (uint8_t *)ptr;
151
152
if (!zmalloc_page_is_underutilized (lp, ratio))
152
- return lp ;
153
+ return {lp, false } ;
153
154
154
155
size_t lp_bytes = lpBytes (lp);
155
156
uint8_t * replacement = lpNew (lpBytes (lp));
156
157
memcpy (replacement, lp, lp_bytes);
157
158
lpFree (lp);
158
159
159
- return replacement;
160
+ return { replacement, true } ;
160
161
};
161
162
162
163
// StringMap supports re-allocation of it's internal nodes
163
164
case kEncodingStrMap2 : {
165
+ bool realloced = false ;
166
+
164
167
StringMap* sm = (StringMap*)ptr;
165
168
for (auto it = sm->begin (); it != sm->end (); ++it)
166
- it.ReallocIfNeeded (ratio);
169
+ realloced |= it.ReallocIfNeeded (ratio);
167
170
168
- return sm ;
171
+ return {sm, realloced} ;
169
172
}
170
173
171
174
default :
@@ -394,10 +397,13 @@ void RobjWrapper::SetString(string_view s, MemoryResource* mr) {
394
397
bool RobjWrapper::DefragIfNeeded (float ratio) {
395
398
if (type () == OBJ_STRING) {
396
399
if (zmalloc_page_is_underutilized (inner_obj (), ratio)) {
397
- return Reallocate (tl.local_mr );
400
+ ReallocateString (tl.local_mr );
401
+ return true ;
398
402
}
399
403
} else if (type () == OBJ_HASH) {
400
- inner_obj_ = DefragHash (tl.local_mr , encoding_, inner_obj_, ratio);
404
+ auto [new_ptr, realloced] = DefragHash (tl.local_mr , encoding_, inner_obj_, ratio);
405
+ inner_obj_ = new_ptr;
406
+ return realloced;
401
407
}
402
408
return false ;
403
409
}
@@ -487,12 +493,12 @@ int RobjWrapper::ZsetAdd(double score, sds ele, int in_flags, int* out_flags, do
487
493
return ss->Add (score, ele, in_flags, out_flags, newscore);
488
494
}
489
495
490
- bool RobjWrapper::Reallocate (MemoryResource* mr) {
496
+ void RobjWrapper::ReallocateString (MemoryResource* mr) {
497
+ DCHECK_EQ (type (), OBJ_STRING);
491
498
void * old_ptr = inner_obj_;
492
499
inner_obj_ = mr->allocate (sz_, kAlignSize );
493
500
memcpy (inner_obj_, old_ptr, sz_);
494
501
mr->deallocate (old_ptr, 0 , kAlignSize );
495
- return true ;
496
502
}
497
503
498
504
void RobjWrapper::Init (unsigned type, unsigned encoding, void * inner) {
0 commit comments