Skip to content

Commit

Permalink
fix(server): small string allocations only under 256 bytes str (#2991)
Browse files Browse the repository at this point in the history
Signed-off-by: adi_holden <[email protected]>
  • Loading branch information
adiholden authored May 2, 2024
1 parent 2c74bce commit a95419b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/compact_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ void CompactObj::SetString(std::string_view str) {
}
}

if (kUseSmallStrings) {
if ((taglen_ == 0 && encoded.size() < (1 << 13))) {
if (kUseSmallStrings && SmallString::CanAllocate(encoded.size())) {
if (taglen_ == 0) {
SetMeta(SMALL_TAG, mask);
tl.small_str_bytes += u_.small_str.Assign(encoded);
return;
Expand Down
4 changes: 4 additions & 0 deletions src/core/segment_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ void SegmentAllocator::ValidateMapSize() {
}
}

bool SegmentAllocator::CanAllocate() {
return address_table_.size() < (1u << kSegmentIdBits);
}

} // namespace dfly
1 change: 1 addition & 0 deletions src/core/segment_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SegmentAllocator {
using Ptr = uint32_t;

SegmentAllocator(mi_heap_t* heap);
bool CanAllocate();

uint8_t* Translate(Ptr p) const {
return address_table_[p & kSegmentIdMask] + Offset(p);
Expand Down
4 changes: 4 additions & 0 deletions src/core/small_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ void SmallString::InitThreadLocal(void* heap) {
XXH3_64bits_reset_withSeed(tl.xxh_state.get(), kHashSeed);
}

bool SmallString::CanAllocate(size_t size) {
return size <= kMaxSize && tl.seg_alloc->CanAllocate();
}

size_t SmallString::UsedThreadLocal() {
return tl.seg_alloc ? tl.seg_alloc->used() : 0;
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/small_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@

namespace dfly {

// blob strings of upto ~64KB. Small sizes are probably predominant
// blob strings of upto ~256B. Small sizes are probably predominant
// for in-memory workloads, especially for keys.
// Please note that this class does not have automatic constructors and destructors, therefore
// it requires explicit management.
class SmallString {
static constexpr unsigned kPrefLen = 10;
static constexpr unsigned kMaxSize = (1 << 8) - 1;

public:
static void InitThreadLocal(void* heap);
static size_t UsedThreadLocal();
static bool CanAllocate(size_t size);

void Reset() {
size_ = 0;
Expand Down

0 comments on commit a95419b

Please sign in to comment.