Skip to content

Commit a95419b

Browse files
authored
fix(server): small string allocations only under 256 bytes str (#2991)
Signed-off-by: adi_holden <[email protected]>
1 parent 2c74bce commit a95419b

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

src/core/compact_object.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,8 @@ void CompactObj::SetString(std::string_view str) {
790790
}
791791
}
792792

793-
if (kUseSmallStrings) {
794-
if ((taglen_ == 0 && encoded.size() < (1 << 13))) {
793+
if (kUseSmallStrings && SmallString::CanAllocate(encoded.size())) {
794+
if (taglen_ == 0) {
795795
SetMeta(SMALL_TAG, mask);
796796
tl.small_str_bytes += u_.small_str.Assign(encoded);
797797
return;

src/core/segment_allocator.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ void SegmentAllocator::ValidateMapSize() {
2525
}
2626
}
2727

28+
bool SegmentAllocator::CanAllocate() {
29+
return address_table_.size() < (1u << kSegmentIdBits);
30+
}
31+
2832
} // namespace dfly

src/core/segment_allocator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SegmentAllocator {
3030
using Ptr = uint32_t;
3131

3232
SegmentAllocator(mi_heap_t* heap);
33+
bool CanAllocate();
3334

3435
uint8_t* Translate(Ptr p) const {
3536
return address_table_[p & kSegmentIdMask] + Offset(p);

src/core/small_string.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ void SmallString::InitThreadLocal(void* heap) {
4545
XXH3_64bits_reset_withSeed(tl.xxh_state.get(), kHashSeed);
4646
}
4747

48+
bool SmallString::CanAllocate(size_t size) {
49+
return size <= kMaxSize && tl.seg_alloc->CanAllocate();
50+
}
51+
4852
size_t SmallString::UsedThreadLocal() {
4953
return tl.seg_alloc ? tl.seg_alloc->used() : 0;
5054
}

src/core/small_string.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010

1111
namespace dfly {
1212

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

2021
public:
2122
static void InitThreadLocal(void* heap);
2223
static size_t UsedThreadLocal();
24+
static bool CanAllocate(size_t size);
2325

2426
void Reset() {
2527
size_ = 0;

0 commit comments

Comments
 (0)