Skip to content
Merged
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
5 changes: 2 additions & 3 deletions src/core/compact_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ struct TL {

thread_local TL tl;

constexpr bool kUseSmallStrings = false;
constexpr bool kUseSmallStrings = true;

/// TODO: Ascii encoding becomes slow for large blobs. We should factor it out into a separate
/// file and implement with SIMD instructions.
Expand Down Expand Up @@ -877,8 +877,7 @@ bool CompactObj::DefragIfNeeded(float ratio) {
}
return false;
case SMALL_TAG:
// TODO - support this later
return false;
return u_.small_str.DefragIfNeeded(ratio);
case INT_TAG:
// this is not relevant in this case
return false;
Expand Down
21 changes: 21 additions & 0 deletions src/core/small_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "core/small_string.h"

#include <mimalloc.h>
#include <xxhash.h>

#include <memory>
Expand Down Expand Up @@ -155,6 +156,7 @@ void SmallString::Get(std::string* dest) const {
}

unsigned SmallString::GetV(string_view dest[2]) const {
DCHECK_GT(size_, kPrefLen);
if (size_ <= kPrefLen) {
dest[0] = string_view{prefix_, size_};
return 1;
Expand All @@ -166,4 +168,23 @@ unsigned SmallString::GetV(string_view dest[2]) const {
return 2;
}

bool SmallString::DefragIfNeeded(float ratio) {
DCHECK_GT(size_, kPrefLen);
if (size_ <= kPrefLen) {
return false;
}

uint8_t* cur_real_ptr = tl.seg_alloc->Translate(small_ptr_);
if (!mi_heap_page_is_underutilized(tl.seg_alloc->heap(), cur_real_ptr, ratio))
return false;

auto [sp, rp] = tl.seg_alloc->Allocate(size_ - kPrefLen);

memcpy(rp, cur_real_ptr, size_ - kPrefLen);
tl.seg_alloc->Free(small_ptr_);
small_ptr_ = sp;

return true;
}

} // namespace dfly
5 changes: 3 additions & 2 deletions src/core/small_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class SmallString {
static constexpr unsigned kPrefLen = 10;

public:

static void InitThreadLocal(void * heap);
static void InitThreadLocal(void* heap);
static size_t UsedThreadLocal();

void Reset() {
Expand Down Expand Up @@ -48,6 +47,8 @@ class SmallString {
// With current implementation, it will return 2 slices for a non-empty string.
unsigned GetV(std::string_view dest[2]) const;

bool DefragIfNeeded(float ratio);

private:
// prefix of the string that is broken down into 2 parts.
char prefix_[kPrefLen];
Expand Down