From 5121c6240d84ec8452a9d67f097693df04d08edb Mon Sep 17 00:00:00 2001 From: Vladislav Oleshko Date: Sun, 30 Nov 2025 16:40:04 +0300 Subject: [PATCH 1/3] chore(core): Remove some unused CompactObj code --- src/core/compact_object.cc | 27 +++++++++++++-------------- src/core/compact_object.h | 30 +++++++----------------------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/core/compact_object.cc b/src/core/compact_object.cc index 6859991cc68d..04a80688e5c0 100644 --- a/src/core/compact_object.cc +++ b/src/core/compact_object.cc @@ -782,15 +782,13 @@ 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_)); + taglen_ = o.taglen_; + 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; } @@ -1275,7 +1273,8 @@ void CompactObj::Reset() { if (HasAllocated()) { Free(); } - tagbyte_ = 0; + taglen_ = 0; + huffman_domain_ = 0; mask_ = 0; } @@ -1387,13 +1386,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: @@ -1405,6 +1402,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) diff --git a/src/core/compact_object.h b/src/core/compact_object.h index 50f1797d9c93..cda660131fc3 100644 --- a/src/core/compact_object.h +++ b/src/core/compact_object.h @@ -157,7 +157,6 @@ class CompactObj { bool is_key_; }; - using PrefixArray = std::vector; using MemoryResource = detail::RobjWrapper::MemoryResource; // Different representations of external values @@ -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; @@ -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; @@ -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()) { @@ -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]; @@ -536,7 +528,6 @@ class CompactObj { } } u_; - // static_assert(sizeof(u_) == 16); union { @@ -562,15 +553,8 @@ 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; - }; - }; + 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 { @@ -580,7 +564,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); From 98266359ca1ed27ca4243d94dabda16d4a8b2df7 Mon Sep 17 00:00:00 2001 From: Vladislav Oleshko Date: Fri, 5 Dec 2025 21:33:19 +0300 Subject: [PATCH 2/3] fixes --- src/core/compact_object.cc | 1 - src/core/compact_object.h | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/compact_object.cc b/src/core/compact_object.cc index 04a80688e5c0..69c347896b1a 100644 --- a/src/core/compact_object.cc +++ b/src/core/compact_object.cc @@ -784,7 +784,6 @@ CompactObj& CompactObj::operator=(CompactObj&& o) noexcept { SetMeta(o.taglen_, o.mask_); // frees own previous resources memcpy(&u_, &o.u_, sizeof(u_)); - taglen_ = o.taglen_; huffman_domain_ = o.huffman_domain_; o.taglen_ = 0; // forget all data diff --git a/src/core/compact_object.h b/src/core/compact_object.h index cda660131fc3..ea715888a3f4 100644 --- a/src/core/compact_object.h +++ b/src/core/compact_object.h @@ -165,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) { @@ -553,6 +553,7 @@ class CompactObj { } mask_bits_; }; + // 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 }; From 38b11d2485543d4e748e2a81f6d2cc76d344631c Mon Sep 17 00:00:00 2001 From: Vladislav Oleshko Date: Fri, 5 Dec 2025 21:33:19 +0300 Subject: [PATCH 3/3] fixes Signed-off-by: Vladislav Oleshko --- src/core/compact_object.cc | 3 ++- src/core/compact_object.h | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/compact_object.cc b/src/core/compact_object.cc index 69c347896b1a..6e91888646c1 100644 --- a/src/core/compact_object.cc +++ b/src/core/compact_object.cc @@ -787,7 +787,8 @@ CompactObj& CompactObj::operator=(CompactObj&& o) noexcept { huffman_domain_ = o.huffman_domain_; o.taglen_ = 0; // forget all data - o.Reset(); + o.huffman_domain_ = 0; + o.mask_ = 0; return *this; } diff --git a/src/core/compact_object.h b/src/core/compact_object.h index ea715888a3f4..21a3a6a7db65 100644 --- a/src/core/compact_object.h +++ b/src/core/compact_object.h @@ -168,11 +168,11 @@ class CompactObj { CompactObj() : taglen_{0}, huffman_domain_{0} { // default - empty string } - explicit CompactObj(std::string_view str, bool is_key) { + explicit CompactObj(std::string_view str, bool is_key) : CompactObj() { SetString(str, is_key); } - CompactObj(CompactObj&& cs) noexcept { + CompactObj(CompactObj&& cs) noexcept : CompactObj() { operator=(std::move(cs)); };