Skip to content

Commit 178aa39

Browse files
committed
more fixes
1 parent a853ed1 commit 178aa39

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/core/compact_object.cc

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,33 +1144,32 @@ void CompactObj::GetString(char* dest) const {
11441144
}
11451145

11461146
if (mask_bits_.encoding) {
1147+
StrEncoding str_encoding = GetStrEncoding();
1148+
string_view decode_blob;
1149+
11471150
if (taglen_ == ROBJ_TAG) {
11481151
CHECK_EQ(OBJ_STRING, u_.r_obj.type());
11491152
DCHECK_EQ(OBJ_ENCODING_RAW, u_.r_obj.encoding());
1150-
string_view blob{(const char*)u_.r_obj.inner_obj(), u_.r_obj.Size()};
1151-
GetStrEncoding().Decode(blob, dest);
1152-
return;
1153+
decode_blob = {(const char*)u_.r_obj.inner_obj(), u_.r_obj.Size()};
11531154
} else {
11541155
CHECK_EQ(SMALL_TAG, taglen_);
11551156
auto& ss = u_.small_str;
11561157

1157-
size_t decoded_len = GetStrEncoding().DecodedSize(ss.size(), ss.first_byte());
1158-
1159-
if (mask_bits_.encoding == HUFFMAN_ENC) {
1158+
char* copy_dest;
1159+
if (str_encoding.enc_ == HUFFMAN_ENC) {
11601160
tl.tmp_buf.resize(ss.size());
1161-
auto* base = reinterpret_cast<char*>(tl.tmp_buf.data());
1162-
ss.Get(base);
1163-
1164-
const auto& decoder = tl.GetHuffmanDecoder(huffman_domain_);
1165-
CHECK(decoder.Decode({base + 1, ss.size() - 1}, decoded_len, dest)); // skip first char
1166-
return;
1161+
copy_dest = reinterpret_cast<char*>(tl.tmp_buf.data());
1162+
} else {
1163+
// Write to rightmost location of dest buffer to leave some bytes for inline unpacking
1164+
size_t decoded_len = str_encoding.DecodedSize(ss.size(), ss.first_byte());
1165+
copy_dest = dest + (decoded_len - ss.size());
11671166
}
11681167

1169-
// we left some space on the left to allow inplace ascii unpacking.
1170-
char* offset_dest = dest + (decoded_len - u_.small_str.size());
1171-
ss.Get(offset_dest);
1172-
detail::ascii_unpack_simd(reinterpret_cast<uint8_t*>(offset_dest), decoded_len, dest);
1168+
ss.Get(copy_dest);
1169+
decode_blob = {copy_dest, ss.size()};
11731170
}
1171+
1172+
str_encoding.Decode(decode_blob, dest);
11741173
return;
11751174
}
11761175

src/core/small_string.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ size_t SmallString::Assign(std::string_view s) {
6161

6262
// If the allocation is large enough and space efficient, we can avoid allocating
6363
if (s.size() >= size_ || s.size() * 2 < MallocUsed()) {
64-
if (size_)
65-
Free();
64+
Free();
65+
6666
auto [sp, rp] = tl.seg_alloc->Allocate(s.size() - kPrefLen);
6767
small_ptr_ = sp;
6868
realptr = rp;
69+
} else {
70+
realptr = tl.seg_alloc->Translate(small_ptr_);
6971
}
7072

7173
size_ = s.size();
@@ -75,7 +77,8 @@ size_t SmallString::Assign(std::string_view s) {
7577
}
7678

7779
void SmallString::Free() {
78-
tl.seg_alloc->Free(small_ptr_);
80+
if (size_)
81+
tl.seg_alloc->Free(small_ptr_);
7982
size_ = 0;
8083
}
8184

src/core/small_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace dfly {
1010

1111
class PageUsage;
1212

13-
// Efficient storage of strings longer than 10 bytes and up to 256 bytes.
13+
// Efficient storage of strings longer than 10 bytes.
1414
// Requires explicit memory management
1515
class SmallString {
1616
static constexpr unsigned kPrefLen = 10;

0 commit comments

Comments
 (0)