Pass correct memory resource to compact object

This commit is contained in:
Roman Gershman 2022-02-22 17:50:09 +02:00
parent 8072e79aab
commit edff35ae3e
4 changed files with 20 additions and 9 deletions

View File

@ -36,13 +36,16 @@ size_t QlUsedSize(quicklist* ql) {
thread_local robj tmp_robj{
.type = 0, .encoding = 0, .lru = 0, .refcount = OBJ_STATIC_REFCOUNT, .ptr = nullptr};
thread_local pmr::memory_resource* local_mr = pmr::get_default_resource();
} // namespace
static_assert(sizeof(CompactObj) == 18);
namespace detail {
CompactBlob::CompactBlob(std::string_view s, pmr::memory_resource* mr)
CompactBlob::CompactBlob(string_view s, pmr::memory_resource* mr)
: ptr_(nullptr), sz(s.size()) {
if (sz) {
ptr_ = mr->allocate(sz);
@ -50,7 +53,7 @@ CompactBlob::CompactBlob(std::string_view s, pmr::memory_resource* mr)
}
}
void CompactBlob::Assign(std::string_view s, std::pmr::memory_resource* mr) {
void CompactBlob::Assign(string_view s, pmr::memory_resource* mr) {
if (s.size() > sz) {
size_t cur_cap = capacity();
if (s.size() > cur_cap)
@ -66,7 +69,7 @@ void CompactBlob::Free(pmr::memory_resource* mr) {
ptr_ = nullptr;
}
void CompactBlob::MakeRoom(size_t current_cap, size_t desired, std::pmr::memory_resource* mr) {
void CompactBlob::MakeRoom(size_t current_cap, size_t desired, pmr::memory_resource* mr) {
if (current_cap * 2 > desired) {
if (desired < SDS_MAX_PREALLOC)
desired *= 2;
@ -200,6 +203,10 @@ bool RobjWrapper::Equal(std::string_view sv) const {
using namespace std;
void CompactObj::InitThreadLocal(pmr::memory_resource* mr) {
local_mr = mr;
}
CompactObj::~CompactObj() {
if (HasAllocated()) {
Free();
@ -294,7 +301,7 @@ void CompactObj::ImportRObj(robj* o) {
if (o->type == OBJ_STRING) {
std::string_view src((char*)o->ptr, sdslen((sds)o->ptr));
u_.r_obj.blob.Assign(src, pmr::get_default_resource());
u_.r_obj.blob.Assign(src, local_mr);
decrRefCount(o);
} else { // Non-string objects we move as is and release Robj wrapper.
u_.r_obj.blob.Set(o->ptr, 0);
@ -374,7 +381,7 @@ void CompactObj::SetString(std::string_view str) {
DCHECK(taglen_ == ROBJ_TAG && u_.r_obj.type == OBJ_STRING);
CHECK_EQ(OBJ_ENCODING_RAW, u_.r_obj.encoding);
u_.r_obj.blob.Assign(input, pmr::get_default_resource());
u_.r_obj.blob.Assign(input, local_mr);
}
std::string_view CompactObj::GetSlice(std::string* scratch) const {
@ -429,7 +436,7 @@ void CompactObj::Free() {
DCHECK(HasAllocated());
if (taglen_ == ROBJ_TAG) {
u_.r_obj.Free(pmr::get_default_resource());
u_.r_obj.Free(local_mr);
} else {
LOG(FATAL) << "Bad compact object type " << int(taglen_);
}

View File

@ -214,6 +214,8 @@ class CompactObj {
return kInlineLen;
}
static void InitThreadLocal(std::pmr::memory_resource* mr);
private:
bool EqualNonInline(std::string_view sv) const;

View File

@ -321,10 +321,10 @@ pair<MainIterator, bool> DbSlice::AddIfNotExist(DbIndex db_ind, string_view key,
auto& db = db_arr_[db_ind];
CompactObj co_key{key};
auto [new_entry, success] = db->prime_table.Insert(std::move(co_key), std::move(obj));
auto [new_entry, inserted] = db->prime_table.Insert(std::move(co_key), std::move(obj));
// in this case obj won't be moved and will be destroyed during unwinding.
if (!success)
if (!inserted)
return make_pair(new_entry, false);
new_entry.SetVersion(NextVersion());

View File

@ -89,6 +89,8 @@ void EngineShard::InitThreadLocal(ProactorBase* pb, bool update_db_time) {
mi_heap_t* tlh = mi_heap_get_backing();
void* ptr = mi_heap_malloc_aligned(tlh, sizeof(EngineShard), alignof(EngineShard));
shard_ = new (ptr) EngineShard(pb, update_db_time, tlh);
CompactObj::InitThreadLocal(shard_->memory_resource());
}
void EngineShard::DestroyThreadLocal() {
@ -99,7 +101,7 @@ void EngineShard::DestroyThreadLocal() {
shard_->~EngineShard();
mi_free(shard_);
shard_ = nullptr;
CompactObj::InitThreadLocal(nullptr);
VLOG(1) << "Shard reset " << index;
}