forked from dragonflydb/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmall_string.h
60 lines (42 loc) · 1.54 KB
/
small_string.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2022, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//
#pragma once
#include <string_view>
#include "core/core_types.h"
namespace dfly {
// blob strings of upto ~64KB. 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;
public:
static void InitThreadLocal(void * heap);
static size_t UsedThreadLocal();
void Reset() {
size_ = 0;
}
// Returns malloc used.
size_t Assign(std::string_view s);
void Free();
bool Equal(std::string_view o) const;
bool Equal(const SmallString& mps) const;
uint16_t size() const {
return size_;
}
uint64_t HashCode() const;
// I am lying here. we should use mi_malloc_usable size really.
uint16_t MallocUsed() const;
void Get(std::string* dest) const;
// returns 1 or 2 slices representing this small string.
// Guarantees zero copy, i.e. dest will not point to any of external buffers.
// With current implementation, it will return 2 slices for a non-empty string.
unsigned GetV(std::string_view dest[2]) const;
private:
// prefix of the string that is broken down into 2 parts.
char prefix_[kPrefLen];
uint32_t small_ptr_; // 32GB capacity because we ignore 3 lsb bits (i.e. x8).
uint16_t size_; // uint16_t - total size (including prefix)
} __attribute__((packed));
} // namespace dfly