Skip to content

Commit ca24135

Browse files
Xingbo Wangfacebook-github-bot
authored andcommitted
Remove the 2 duplicated fields in block.h Block class (facebook#13733)
Summary: `data_` and `size_` fields are duplicated in `Block` class, as `contents_` field already have a `data` member variable, which contains `data` and `size` already. This reduces memory consumption by 16 bytes per block. Pull Request resolved: facebook#13733 Test Plan: Unit test Reviewed By: pdillinger Differential Revision: D77389791 Pulled By: xingbowang fbshipit-source-id: 50a56bc5fae494ed5bc39bdfde7303ca06ce87c6
1 parent 4e42588 commit ca24135

2 files changed

Lines changed: 33 additions & 38 deletions

File tree

table/block_based/block.cc

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,10 @@ bool IndexBlockIter::PrefixSeek(const Slice& target, uint32_t* index,
10161016
}
10171017

10181018
uint32_t Block::NumRestarts() const {
1019-
assert(size_ >= 2 * sizeof(uint32_t));
1020-
uint32_t block_footer = DecodeFixed32(data_ + size_ - sizeof(uint32_t));
1019+
assert(size() >= 2 * sizeof(uint32_t));
1020+
uint32_t block_footer = DecodeFixed32(data() + size() - sizeof(uint32_t));
10211021
uint32_t num_restarts = block_footer;
1022-
if (size_ > kMaxBlockSizeSupportedByHashIndex) {
1022+
if (size() > kMaxBlockSizeSupportedByHashIndex) {
10231023
// In BlockBuilder, we have ensured a block with HashIndex is less than
10241024
// kMaxBlockSizeSupportedByHashIndex (64KiB).
10251025
//
@@ -1038,12 +1038,12 @@ uint32_t Block::NumRestarts() const {
10381038
}
10391039

10401040
BlockBasedTableOptions::DataBlockIndexType Block::IndexType() const {
1041-
assert(size_ >= 2 * sizeof(uint32_t));
1042-
if (size_ > kMaxBlockSizeSupportedByHashIndex) {
1041+
assert(size() >= 2 * sizeof(uint32_t));
1042+
if (size() > kMaxBlockSizeSupportedByHashIndex) {
10431043
// The check is for the same reason as that in NumRestarts()
10441044
return BlockBasedTableOptions::kDataBlockBinarySearch;
10451045
}
1046-
uint32_t block_footer = DecodeFixed32(data_ + size_ - sizeof(uint32_t));
1046+
uint32_t block_footer = DecodeFixed32(data() + size() - sizeof(uint32_t));
10471047
uint32_t num_restarts = block_footer;
10481048
BlockBasedTableOptions::DataBlockIndexType index_type;
10491049
UnPackIndexTypeAndNumRestarts(block_footer, &index_type, &num_restarts);
@@ -1059,54 +1059,51 @@ Block::~Block() {
10591059

10601060
Block::Block(BlockContents&& contents, size_t read_amp_bytes_per_bit,
10611061
Statistics* statistics)
1062-
: contents_(std::move(contents)),
1063-
data_(contents_.data.data()),
1064-
size_(contents_.data.size()),
1065-
restart_offset_(0),
1066-
num_restarts_(0) {
1062+
: contents_(std::move(contents)), restart_offset_(0), num_restarts_(0) {
10671063
TEST_SYNC_POINT("Block::Block:0");
1068-
if (size_ < sizeof(uint32_t)) {
1069-
size_ = 0; // Error marker
1064+
auto& size = contents_.data.size_;
1065+
if (size < sizeof(uint32_t)) {
1066+
size = 0; // Error marker
10701067
} else {
10711068
// Should only decode restart points for uncompressed blocks
10721069
num_restarts_ = NumRestarts();
10731070
switch (IndexType()) {
10741071
case BlockBasedTableOptions::kDataBlockBinarySearch:
1075-
restart_offset_ = static_cast<uint32_t>(size_) -
1072+
restart_offset_ = static_cast<uint32_t>(size) -
10761073
(1 + num_restarts_) * sizeof(uint32_t);
1077-
if (restart_offset_ > size_ - sizeof(uint32_t)) {
1074+
if (restart_offset_ > size - sizeof(uint32_t)) {
10781075
// The size is too small for NumRestarts() and therefore
10791076
// restart_offset_ wrapped around.
1080-
size_ = 0;
1077+
size = 0;
10811078
}
10821079
break;
10831080
case BlockBasedTableOptions::kDataBlockBinaryAndHash:
1084-
if (size_ < sizeof(uint32_t) /* block footer */ +
1085-
sizeof(uint16_t) /* NUM_BUCK */) {
1086-
size_ = 0;
1081+
if (size < sizeof(uint32_t) /* block footer */ +
1082+
sizeof(uint16_t) /* NUM_BUCK */) {
1083+
size = 0;
10871084
break;
10881085
}
10891086

10901087
uint16_t map_offset;
10911088
data_block_hash_index_.Initialize(
1092-
data_, static_cast<uint16_t>(size_ - sizeof(uint32_t)), /*chop off
1093-
NUM_RESTARTS*/
1094-
&map_offset);
1089+
contents_.data.data(),
1090+
/* chop off NUM_RESTARTS */
1091+
static_cast<uint16_t>(size - sizeof(uint32_t)), &map_offset);
10951092

10961093
restart_offset_ = map_offset - num_restarts_ * sizeof(uint32_t);
10971094

10981095
if (restart_offset_ > map_offset) {
10991096
// map_offset is too small for NumRestarts() and
11001097
// therefore restart_offset_ wrapped around.
1101-
size_ = 0;
1098+
size = 0;
11021099
break;
11031100
}
11041101
break;
11051102
default:
1106-
size_ = 0; // Error marker
1103+
size = 0; // Error marker
11071104
}
11081105
}
1109-
if (read_amp_bytes_per_bit != 0 && statistics && size_ != 0) {
1106+
if (read_amp_bytes_per_bit != 0 && statistics && size != 0) {
11101107
read_amp_bitmap_.reset(new BlockReadAmpBitmap(
11111108
restart_offset_, read_amp_bytes_per_bit, statistics));
11121109
}
@@ -1148,7 +1145,7 @@ void Block::InitializeDataBlockProtectionInfo(uint8_t protection_bytes_per_key,
11481145
assert(!iter->status().ok() || i == num_keys * protection_bytes_per_key);
11491146
}
11501147
if (!iter->status().ok()) {
1151-
size_ = 0; // Error marker
1148+
contents_.data.size_ = 0; // Error marker
11521149
return;
11531150
}
11541151
protection_bytes_per_key_ = protection_bytes_per_key;
@@ -1197,7 +1194,7 @@ void Block::InitializeIndexBlockProtectionInfo(uint8_t protection_bytes_per_key,
11971194
assert(!iter->status().ok() || i == num_keys * protection_bytes_per_key);
11981195
}
11991196
if (!iter->status().ok()) {
1200-
size_ = 0; // Error marker
1197+
contents_.data.size_ = 0; // Error marker
12011198
return;
12021199
}
12031200
protection_bytes_per_key_ = protection_bytes_per_key;
@@ -1231,7 +1228,7 @@ void Block::InitializeMetaIndexBlockProtectionInfo(
12311228
assert(!iter->status().ok() || i == num_keys * protection_bytes_per_key);
12321229
}
12331230
if (!iter->status().ok()) {
1234-
size_ = 0; // Error marker
1231+
contents_.data.size_ = 0; // Error marker
12351232
return;
12361233
}
12371234
protection_bytes_per_key_ = protection_bytes_per_key;
@@ -1240,14 +1237,14 @@ void Block::InitializeMetaIndexBlockProtectionInfo(
12401237

12411238
MetaBlockIter* Block::NewMetaIterator(bool block_contents_pinned) {
12421239
MetaBlockIter* iter = new MetaBlockIter();
1243-
if (size_ < 2 * sizeof(uint32_t)) {
1240+
if (size() < 2 * sizeof(uint32_t)) {
12441241
iter->Invalidate(Status::Corruption("bad block contents"));
12451242
return iter;
12461243
} else if (num_restarts_ == 0) {
12471244
// Empty block.
12481245
iter->Invalidate(Status::OK());
12491246
} else {
1250-
iter->Initialize(data_, restart_offset_, num_restarts_,
1247+
iter->Initialize(data(), restart_offset_, num_restarts_,
12511248
block_contents_pinned, protection_bytes_per_key_,
12521249
kv_checksum_, block_restart_interval_);
12531250
}
@@ -1265,7 +1262,7 @@ DataBlockIter* Block::NewDataIterator(const Comparator* raw_ucmp,
12651262
} else {
12661263
ret_iter = new DataBlockIter;
12671264
}
1268-
if (size_ < 2 * sizeof(uint32_t)) {
1265+
if (size() < 2 * sizeof(uint32_t)) {
12691266
ret_iter->Invalidate(Status::Corruption("bad block contents"));
12701267
return ret_iter;
12711268
}
@@ -1275,7 +1272,7 @@ DataBlockIter* Block::NewDataIterator(const Comparator* raw_ucmp,
12751272
return ret_iter;
12761273
} else {
12771274
ret_iter->Initialize(
1278-
raw_ucmp, data_, restart_offset_, num_restarts_, global_seqno,
1275+
raw_ucmp, data(), restart_offset_, num_restarts_, global_seqno,
12791276
read_amp_bitmap_.get(), block_contents_pinned,
12801277
user_defined_timestamps_persisted,
12811278
data_block_hash_index_.Valid() ? &data_block_hash_index_ : nullptr,
@@ -1303,7 +1300,7 @@ IndexBlockIter* Block::NewIndexIterator(
13031300
} else {
13041301
ret_iter = new IndexBlockIter;
13051302
}
1306-
if (size_ < 2 * sizeof(uint32_t)) {
1303+
if (size() < 2 * sizeof(uint32_t)) {
13071304
ret_iter->Invalidate(Status::Corruption("bad block contents"));
13081305
return ret_iter;
13091306
}
@@ -1315,7 +1312,7 @@ IndexBlockIter* Block::NewIndexIterator(
13151312
BlockPrefixIndex* prefix_index_ptr =
13161313
total_order_seek ? nullptr : prefix_index;
13171314
ret_iter->Initialize(
1318-
raw_ucmp, data_, restart_offset_, num_restarts_, global_seqno,
1315+
raw_ucmp, data(), restart_offset_, num_restarts_, global_seqno,
13191316
prefix_index_ptr, have_first_key, key_includes_seq, value_is_full,
13201317
block_contents_pinned, user_defined_timestamps_persisted,
13211318
protection_bytes_per_key_, kv_checksum_, block_restart_interval_);

table/block_based/block.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ class Block {
163163

164164
~Block();
165165

166-
size_t size() const { return size_; }
167-
const char* data() const { return data_; }
166+
size_t size() const { return contents_.data.size(); }
167+
const char* data() const { return contents_.data.data(); }
168168
// The additional memory space taken by the block data.
169169
size_t usable_size() const { return contents_.usable_size(); }
170170
uint32_t NumRestarts() const;
@@ -277,8 +277,6 @@ class Block {
277277

278278
private:
279279
BlockContents contents_;
280-
const char* data_; // contents_.data.data()
281-
size_t size_; // contents_.data.size()
282280
uint32_t restart_offset_; // Offset in data_ of restart array
283281
uint32_t num_restarts_;
284282
std::unique_ptr<BlockReadAmpBitmap> read_amp_bitmap_;

0 commit comments

Comments
 (0)