Skip to content

Commit cfb6849

Browse files
committed
small fixes
1 parent be1d359 commit cfb6849

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/server/tiering/small_bins.cc

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,30 @@ std::optional<SmallBins::FilledBin> SmallBins::Stash(DbIndex dbid, std::string_v
3333
size_t value_bytes = StashedValueSize(value);
3434

3535
std::optional<FilledBin> filled_bin;
36-
if (2 /* num entries */ + current_bin_.bytes_ + value_bytes >= kPageSize) {
37-
filled_bin = exchange(current_bin_, FilledBin{++last_bin_id_}); // todo-optimize entries shrink
36+
if (2 /* num entries */ + current_bin_.bytes + value_bytes >= kPageSize) {
37+
filled_bin = exchange(current_bin_, FilledBin{++last_bin_id_});
3838
}
3939

40-
current_bin_.bytes_ += value_bytes;
41-
auto [it, inserted] = current_bin_.entries_.emplace(std::make_pair(dbid, key), string(value));
40+
current_bin_.bytes += value_bytes;
41+
auto [it, inserted] = current_bin_.entries.emplace(std::make_pair(dbid, key), string(value));
4242
CHECK(inserted);
4343

4444
return filled_bin;
4545
}
4646

47-
size_t SmallBins::SerializeBin(FilledBin* bin, io::MutableBytes bytes) {
48-
DCHECK_GT(bin->entries_.size(), 0u);
47+
size_t SmallBins::SerializeBin(FilledBin* bin, io::MutableBytes dest) {
48+
DCHECK_GT(bin->entries.size(), 0u);
49+
DCHECK_GE(dest.size(), 4_KB);
4950

5051
auto& pending_set = pending_bins_[bin->id];
51-
52-
uint8_t* data = bytes.data();
52+
uint8_t* data = dest.data();
5353

5454
// Store number of entries, 2 bytes
55-
absl::little_endian::Store16(data, bin->entries_.size());
55+
absl::little_endian::Store16(data, bin->entries.size());
5656
data += sizeof(uint16_t);
5757

5858
// Store all dbids and hashes, n * 10 bytes
59-
for (const auto& [key, _] : bin->entries_) {
59+
for (const auto& [key, _] : bin->entries) {
6060
absl::little_endian::Store16(data, key.first);
6161
data += sizeof(DbIndex);
6262

@@ -65,18 +65,22 @@ size_t SmallBins::SerializeBin(FilledBin* bin, io::MutableBytes bytes) {
6565
}
6666

6767
// Store all values with sizes, n * (2 + x) bytes
68-
for (const auto& [key, value] : bin->entries_) {
68+
for (const auto& [key, value] : bin->entries) {
6969
absl::little_endian::Store16(data, value.size());
7070
data += sizeof(uint16_t);
7171

72-
pending_set[key] = {size_t(data - bytes.data()), value.size()};
72+
pending_set[key] = {size_t(data - dest.data()), value.size()};
7373
memcpy(data, value.data(), value.size());
7474
data += value.size();
7575
}
7676

77-
// erase does not shrink, unlike clear().
78-
// current_bin_.entries_.erase(current_bin_.entries_.begin(), current_bin_.entries_.end());
79-
return bin->bytes_ + 2;
77+
// Steal backing array from bin if relevant
78+
if (current_bin_.entries.empty()) {
79+
bin->entries.erase(bin->entries.begin(), bin->entries.end()); // clear shrinks backing
80+
current_bin_.entries = std::move(bin->entries);
81+
}
82+
83+
return bin->bytes + 2;
8084
}
8185

8286
SmallBins::KeySegmentList SmallBins::ReportStashed(BinId id, DiskSegment segment) {
@@ -113,12 +117,12 @@ std::vector<std::pair<DbIndex, std::string>> SmallBins::ReportStashAborted(BinId
113117
}
114118

115119
std::optional<SmallBins::BinId> SmallBins::Delete(DbIndex dbid, std::string_view key) {
116-
auto& entries = current_bin_.entries_;
120+
auto& entries = current_bin_.entries;
117121
if (auto it = entries.find(make_pair(dbid, key)); it != entries.end()) {
118122
size_t stashed_size = StashedValueSize(it->second);
119-
DCHECK_GE(current_bin_.bytes_, stashed_size);
123+
DCHECK_GE(current_bin_.bytes, stashed_size);
120124

121-
current_bin_.bytes_ -= stashed_size;
125+
current_bin_.bytes -= stashed_size;
122126
entries.erase(it);
123127
return std::nullopt;
124128
}
@@ -156,8 +160,8 @@ SmallBins::BinInfo SmallBins::Delete(DiskSegment segment) {
156160
SmallBins::Stats SmallBins::GetStats() const {
157161
return Stats{.stashed_bins_cnt = stashed_bins_.size(),
158162
.stashed_entries_cnt = stats_.stashed_entries_cnt,
159-
.current_bin_bytes = current_bin_.bytes_,
160-
.current_entries_cnt = current_bin_.entries_.size()};
163+
.current_bin_bytes = current_bin_.bytes,
164+
.current_entries_cnt = current_bin_.entries.size()};
161165
}
162166

163167
SmallBins::KeyHashDbList SmallBins::DeleteBin(DiskSegment segment, std::string_view value) {

src/server/tiering/small_bins.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SmallBins {
3636
bool fragmented = false, empty = false;
3737
};
3838

39-
// Packaged bin ready to be serialized
39+
// Packaged bin ready to be serialized with SerializeBin()
4040
struct FilledBin {
4141
friend class SmallBins;
4242
BinId id;
@@ -45,8 +45,8 @@ class SmallBins {
4545
explicit FilledBin(BinId id) : id{id} {
4646
}
4747

48-
unsigned bytes_ = 0;
49-
tiering::EntryMap<std::string> entries_;
48+
unsigned bytes = 0;
49+
tiering::EntryMap<std::string> entries;
5050
};
5151

5252
// List of locations of values for corresponding keys of previously filled bin
@@ -57,7 +57,7 @@ class SmallBins {
5757

5858
// Returns true if the entry is pending inside SmallBins.
5959
bool IsPending(DbIndex dbid, std::string_view key) const {
60-
return current_bin_.entries_.count(std::make_pair(dbid, key)) > 0;
60+
return current_bin_.entries.count(std::make_pair(dbid, key)) > 0;
6161
}
6262

6363
// Enqueue key/value pair for stash. Returns page to be stashed if it filled up.
@@ -80,9 +80,10 @@ class SmallBins {
8080
// Mainly used for defragmentation
8181
KeyHashDbList DeleteBin(DiskSegment segment, std::string_view value);
8282

83-
Stats GetStats() const;
83+
// Serialize filled bin to destination buffer (4kb)
84+
size_t SerializeBin(FilledBin* bin, io::MutableBytes dest);
8485

85-
size_t SerializeBin(FilledBin* bin, io::MutableBytes bytes);
86+
Stats GetStats() const;
8687

8788
private:
8889
struct StashInfo {

0 commit comments

Comments
 (0)