Skip to content

Commit

Permalink
chore: introduce sorted_map
Browse files Browse the repository at this point in the history
Consolidate skiplist based zset functionality into a dedicated class
called SortedMap. The code is adapted from t_zset.c
The old code in t_zset.c is deleted.

Signed-off-by: Roman Gershman <[email protected]>
  • Loading branch information
romange committed Jul 18, 2023
1 parent a5d8c91 commit 938dfbd
Show file tree
Hide file tree
Showing 18 changed files with 673 additions and 839 deletions.
4 changes: 3 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(dfly_core compact_object.cc dragonfly_core.cc extent_tree.cc
external_alloc.cc interpreter.cc json_object.cc mi_memory_resource.cc sds_utils.cc
segment_allocator.cc simple_lru_counter.cc small_string.cc tx_queue.cc dense_set.cc
segment_allocator.cc simple_lru_counter.cc small_string.cc sorted_map.cc
tx_queue.cc dense_set.cc
string_set.cc string_map.cc detail/bitpacking.cc)

cxx_link(dfly_core base query_parser absl::flat_hash_map absl::str_format redis_lib TRDP::lua lua_modules
Expand All @@ -19,5 +20,6 @@ cxx_test(json_test dfly_core TRDP::jsoncons LABELS DFLY)
cxx_test(simple_lru_counter_test dfly_core LABELS DFLY)
cxx_test(string_set_test dfly_core LABELS DFLY)
cxx_test(string_map_test dfly_core LABELS DFLY)
cxx_test(sorted_map_test dfly_core LABELS DFLY)

add_subdirectory(search)
46 changes: 12 additions & 34 deletions src/core/compact_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include "base/logging.h"
#include "base/pod_array.h"
#include "core/detail/bitpacking.h"
#include "core/sorted_map.h"
#include "core/string_map.h"
#include "core/string_set.h"

Expand All @@ -46,14 +47,6 @@ size_t QlMAllocSize(quicklist* ql) {
return res + ql->count * 16; // we account for each member 16 bytes.
}

// Approximated dictionary size.
size_t DictMallocSize(dict* d) {
size_t res = zmalloc_usable_size(d->ht_table[0]) + zmalloc_usable_size(d->ht_table[1]) +
znallocx(sizeof(dict));

return res + dictSize(d) * 16; // approximation.
}

inline void FreeObjSet(unsigned encoding, void* ptr, MemoryResource* mr) {
switch (encoding) {
case kEncodingStrMap: {
Expand Down Expand Up @@ -107,8 +100,8 @@ size_t MallocUsedZSet(unsigned encoding, void* ptr) {
case OBJ_ENCODING_LISTPACK:
return lpBytes(reinterpret_cast<uint8_t*>(ptr));
case OBJ_ENCODING_SKIPLIST: {
zset* zs = (zset*)ptr;
return DictMallocSize(zs->dict);
detail::SortedMap* ss = (detail::SortedMap*)ptr;
return ss->MallocSize(); // DictMallocSize(zs->dict);
}
}
LOG(DFATAL) << "Unknown set encoding type " << encoding;
Expand All @@ -134,11 +127,10 @@ inline void FreeObjHash(unsigned encoding, void* ptr) {
}

inline void FreeObjZset(unsigned encoding, void* ptr) {
zset* zs = (zset*)ptr;
detail::SortedMap* zs = (detail::SortedMap*)ptr;
switch (encoding) {
case OBJ_ENCODING_SKIPLIST:
zs = (zset*)ptr;
zsetFree(zs);
delete zs;
break;
case OBJ_ENCODING_LISTPACK:
zfree(ptr);
Expand Down Expand Up @@ -238,8 +230,8 @@ size_t RobjWrapper::Size() const {
case OBJ_ZSET: {
switch (encoding_) {
case OBJ_ENCODING_SKIPLIST: {
zset* zs = (zset*)inner_obj_;
return zs->zsl->length;
SortedMap* ss = (SortedMap*)inner_obj_;
return ss->Size();
}
case OBJ_ENCODING_LISTPACK:
return lpLength((uint8_t*)inner_obj_) / 2;
Expand Down Expand Up @@ -427,13 +419,9 @@ int RobjWrapper::ZsetAdd(double score, sds ele, int in_flags, int* out_flags, do
* becomes too long *before* executing zzlInsert. */
if (zl_len + 1 > server.zset_max_listpack_entries ||
sdslen(ele) > server.zset_max_listpack_value || !lpSafeToAdd(lp, sdslen(ele))) {
robj self{.type = type_,
.encoding = encoding_,
.lru = 0,
.refcount = OBJ_STATIC_REFCOUNT,
.ptr = inner_obj_};
zsetConvert(&self, OBJ_ENCODING_SKIPLIST);
inner_obj_ = self.ptr;
unique_ptr<SortedMap> ss = SortedMap::FromListPack(lp);
lpFree(lp);
inner_obj_ = ss.release();
encoding_ = OBJ_ENCODING_SKIPLIST;
} else {
inner_obj_ = zzlInsert(lp, ele, score);
Expand All @@ -449,18 +437,8 @@ int RobjWrapper::ZsetAdd(double score, sds ele, int in_flags, int* out_flags, do
}

CHECK_EQ(encoding_, OBJ_ENCODING_SKIPLIST);

// TODO: to factor out OBJ_ENCODING_SKIPLIST functionality into a separate class.
robj self{.type = type_,
.encoding = encoding_,
.lru = 0,
.refcount = OBJ_STATIC_REFCOUNT,
.ptr = inner_obj_};

int res = zsetAdd(&self, score, ele, in_flags, out_flags, newscore);
inner_obj_ = self.ptr;
encoding_ = self.encoding;
return res;
SortedMap* ss = (SortedMap*)inner_obj_;
return ss->Add(score, ele, in_flags, out_flags, newscore);
}

bool RobjWrapper::Reallocate(MemoryResource* mr) {
Expand Down
8 changes: 5 additions & 3 deletions src/core/compact_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <absl/base/internal/endian.h>

#ifdef __clang__
#if defined(__clang__) && defined(__APPLE__)
#include <experimental/memory_resource>
namespace PMR_NS = std::experimental::pmr;
#else
Expand Down Expand Up @@ -48,6 +48,10 @@ class RobjWrapper {
void SetString(std::string_view s, MemoryResource* mr);
void Init(unsigned type, unsigned encoding, void* inner);

// Equivalent to zsetAdd
int AddZsetMember(std::string_view member, double score, int in_flags, int* out_flags,
double* newscore);

unsigned type() const {
return type_;
}
Expand Down Expand Up @@ -440,5 +444,3 @@ class CompactObjectView {
};

} // namespace dfly

#undef PMR_NS
2 changes: 1 addition & 1 deletion src/core/dash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
#pragma once

#ifdef __clang__
#if defined(__clang__) && defined(__APPLE__)
#include <experimental/memory_resource>
namespace PMR_NS = std::experimental::pmr;
#else
Expand Down
2 changes: 1 addition & 1 deletion src/core/dense_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <functional>
#include <type_traits>

#ifdef __clang__
#if defined(__clang__) && defined(__APPLE__)
#include <experimental/memory_resource>
namespace PMR_NS = std::experimental::pmr;
#else
Expand Down
2 changes: 1 addition & 1 deletion src/core/mi_memory_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <mimalloc.h>

#ifdef __clang__
#if defined(__clang__) && defined(__APPLE__)
#include <experimental/memory_resource>
namespace PMR_NS = std::experimental::pmr;
#else
Expand Down
Loading

0 comments on commit 938dfbd

Please sign in to comment.