Skip to content
Draft
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
26 changes: 12 additions & 14 deletions src/core/compact_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,15 +782,12 @@ CompactObj::~CompactObj() {
CompactObj& CompactObj::operator=(CompactObj&& o) noexcept {
DCHECK(&o != this);

SetMeta(o.taglen_, o.mask_); // Frees underlying resources if needed.
SetMeta(o.taglen_, o.mask_); // frees own previous resources
memcpy(&u_, &o.u_, sizeof(u_));
huffman_domain_ = o.huffman_domain_;

tagbyte_ = o.tagbyte_;

// SetMeta deallocates the object and we only want reset it.
o.tagbyte_ = 0;
o.mask_ = 0;

o.taglen_ = 0; // forget all data
o.Reset();
return *this;
}

Expand Down Expand Up @@ -1275,7 +1272,8 @@ void CompactObj::Reset() {
if (HasAllocated()) {
Free();
}
tagbyte_ = 0;
taglen_ = 0;
huffman_domain_ = 0;
mask_ = 0;
}

Expand Down Expand Up @@ -1387,13 +1385,11 @@ bool CompactObj::operator==(const CompactObj& o) const {
return memcmp(u_.inline_str, o.u_.inline_str, taglen_) == 0;
}

bool CompactObj::EqualNonInline(std::string_view sv) const {
bool CompactObj::CmpNonInline(std::string_view sv) const {
DCHECK_GT(taglen_, kInlineLen);
switch (taglen_) {
case INT_TAG: {
absl::AlphaNum an(u_.ival);
return sv == an.Piece();
}

case INT_TAG:
return absl::AlphaNum(u_.ival).Piece() == sv;
case ROBJ_TAG:
return u_.r_obj.Equal(sv);
case SMALL_TAG:
Expand All @@ -1405,6 +1401,8 @@ bool CompactObj::EqualNonInline(std::string_view sv) const {
}

bool CompactObj::CmpEncoded(string_view sv) const {
DCHECK(mask_bits_.encoding);

if (mask_bits_.encoding == HUFFMAN_ENC) {
size_t sz = Size();
if (sv.size() != sz)
Expand Down
33 changes: 9 additions & 24 deletions src/core/compact_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class CompactObj {
bool is_key_;
};

using PrefixArray = std::vector<std::string_view>;
using MemoryResource = detail::RobjWrapper::MemoryResource;

// Different representations of external values
Expand All @@ -166,7 +165,7 @@ class CompactObj {
SERIALIZED_MAP // OBJ_HASH, Serialized map
};

CompactObj() { // By default - empty string.
CompactObj() : taglen_{0}, huffman_domain_{0} { // default - empty string
}

explicit CompactObj(std::string_view str, bool is_key) {
Expand All @@ -191,7 +190,8 @@ class CompactObj {
CompactObj AsRef() const {
CompactObj res;
memcpy(&res.u_, &u_, sizeof(u_));
res.tagbyte_ = tagbyte_;
res.taglen_ = taglen_;
res.huffman_domain_ = huffman_domain_;
res.mask_ = mask_;
res.mask_bits_.ref = 1;

Expand Down Expand Up @@ -397,10 +397,6 @@ class CompactObj {

uint8_t GetFirstByte() const;

static constexpr unsigned InlineLen() {
return kInlineLen;
}

struct Stats {
size_t small_string_bytes = 0;
uint64_t huff_encode_total = 0, huff_encode_success = 0;
Expand Down Expand Up @@ -451,12 +447,11 @@ class CompactObj {
private:
void EncodeString(std::string_view str, bool is_key);

bool EqualNonInline(std::string_view sv) const;

// Requires: HasAllocated() - true.
void Free();

bool CmpEncoded(std::string_view sv) const;
bool CmpNonInline(std::string_view sv) const;

void SetMeta(uint8_t taglen, uint8_t mask = 0) {
if (HasAllocated()) {
Expand Down Expand Up @@ -516,10 +511,7 @@ class CompactObj {
bool DefragIfNeeded(PageUsage* page_usage);
};

// My main data structure. Union of representations.
// RobjWrapper is kInlineLen=16 bytes, so we employ SSO of that size via inline_str.
// In case of int values, we waste 8 bytes. I am assuming it's ok and it's not the data type
// with biggest memory usage.
// Union of different representations
union U {
char inline_str[kInlineLen];

Expand All @@ -536,7 +528,6 @@ class CompactObj {
}
} u_;

//
static_assert(sizeof(u_) == 16);

union {
Expand All @@ -562,15 +553,9 @@ class CompactObj {
} mask_bits_;
};

// We currently reserve 5 bits for tags and 3 bits for extending the mask. currently reserved.
union {
uint8_t tagbyte_ = 0;
struct {
uint8_t taglen_ : 5;
uint8_t huffman_domain_ : 1; // value from HuffmanDomain enum.
uint8_t reserved : 2;
};
};
// TODO: use c++20 bitfield initializers
uint8_t taglen_ : 5; // Either length of inline string or tag of type
uint8_t huffman_domain_ : 1; // Value from HuffmanDomain enum. TODO: replace as is_key
};

inline bool CompactObj::operator==(std::string_view sv) const {
Expand All @@ -580,7 +565,7 @@ inline bool CompactObj::operator==(std::string_view sv) const {
if (IsInline()) {
return std::string_view{u_.inline_str, taglen_} == sv;
}
return EqualNonInline(sv);
return CmpNonInline(sv);
}

std::string_view ObjTypeToString(CompactObjType type);
Expand Down
Loading