@@ -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
8286SmallBins::KeySegmentList SmallBins::ReportStashed (BinId id, DiskSegment segment) {
@@ -113,12 +117,12 @@ std::vector<std::pair<DbIndex, std::string>> SmallBins::ReportStashAborted(BinId
113117}
114118
115119std::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) {
156160SmallBins::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
163167SmallBins::KeyHashDbList SmallBins::DeleteBin (DiskSegment segment, std::string_view value) {
0 commit comments