Skip to content

Commit 043fc14

Browse files
committed
Get rid of some shared_ptrs
Summary: I went through all remaining shared_ptrs and removed the ones that I found not-necessary. Only GenerateCachePrefix() is called fairly often, so don't expect much perf wins. The ones that are left are accessed infrequently and I think we're fine with keeping them. Test Plan: make asan_check Reviewers: dhruba, haobo Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D14427
1 parent 930cb0b commit 043fc14

11 files changed

+41
-40
lines changed

db/db_impl.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ DBImpl::DBImpl(const Options& options, const std::string& dbname)
235235
mutex_(options.use_adaptive_mutex),
236236
shutting_down_(nullptr),
237237
bg_cv_(&mutex_),
238-
mem_rep_factory_(options_.memtable_factory),
238+
mem_rep_factory_(options_.memtable_factory.get()),
239239
mem_(new MemTable(internal_comparator_, mem_rep_factory_,
240240
NumberLevels(), options_)),
241241
logfile_number_(0),

db/db_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class DBImpl : public DB {
315315
port::Mutex mutex_;
316316
port::AtomicPointer shutting_down_;
317317
port::CondVar bg_cv_; // Signalled when background work finishes
318-
std::shared_ptr<MemTableRepFactory> mem_rep_factory_;
318+
MemTableRepFactory* mem_rep_factory_;
319319
MemTable* mem_;
320320
MemTableList imm_; // Memtable that are not changing
321321
uint64_t logfile_number_;

db/db_iter.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class DBIter: public Iterator {
6161
const Comparator* cmp, Iterator* iter, SequenceNumber s)
6262
: dbname_(dbname),
6363
env_(env),
64-
logger_(options.info_log),
64+
logger_(options.info_log.get()),
6565
user_comparator_(cmp),
6666
user_merge_operator_(options.merge_operator.get()),
6767
iter_(iter),
@@ -122,7 +122,7 @@ class DBIter: public Iterator {
122122

123123
const std::string* const dbname_;
124124
Env* const env_;
125-
shared_ptr<Logger> logger_;
125+
Logger* logger_;
126126
const Comparator* const user_comparator_;
127127
const MergeOperator* const user_merge_operator_;
128128
Iterator* const iter_;
@@ -293,7 +293,7 @@ void DBIter::MergeValuesNewToOld() {
293293
// ignore corruption if there is any.
294294
const Slice value = iter_->value();
295295
user_merge_operator_->FullMerge(ikey.user_key, &value, operands,
296-
&saved_value_, logger_.get());
296+
&saved_value_, logger_);
297297
// iter_ is positioned after put
298298
iter_->Next();
299299
return;
@@ -310,7 +310,7 @@ void DBIter::MergeValuesNewToOld() {
310310
Slice(operands[0]),
311311
Slice(operands[1]),
312312
&merge_result,
313-
logger_.get())) {
313+
logger_)) {
314314
operands.pop_front();
315315
swap(operands.front(), merge_result);
316316
} else {
@@ -327,7 +327,7 @@ void DBIter::MergeValuesNewToOld() {
327327
// feed null as the existing value to the merge operator, such that
328328
// client can differentiate this scenario and do things accordingly.
329329
user_merge_operator_->FullMerge(saved_key_, nullptr, operands,
330-
&saved_value_, logger_.get());
330+
&saved_value_, logger_);
331331
}
332332

333333
void DBIter::Prev() {

db/memtable.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct hash<rocksdb::Slice> {
3333
namespace rocksdb {
3434

3535
MemTable::MemTable(const InternalKeyComparator& cmp,
36-
std::shared_ptr<MemTableRepFactory> table_factory,
36+
MemTableRepFactory* table_factory,
3737
int numlevel,
3838
const Options& options)
3939
: comparator_(cmp),
@@ -274,7 +274,7 @@ bool MemTable::Update(SequenceNumber seq, ValueType type,
274274
Slice memkey = lkey.memtable_key();
275275

276276
std::shared_ptr<MemTableRep::Iterator> iter(
277-
table_.get()->GetIterator(lkey.user_key()));
277+
table_->GetIterator(lkey.user_key()));
278278
iter->Seek(memkey.data());
279279

280280
if (iter->Valid()) {

db/memtable.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class MemTable {
3535
// is zero and the caller must call Ref() at least once.
3636
explicit MemTable(
3737
const InternalKeyComparator& comparator,
38-
std::shared_ptr<MemTableRepFactory> table_factory,
38+
MemTableRepFactory* table_factory,
3939
int numlevel = 7,
4040
const Options& options = Options());
4141

db/repair.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Repairer {
196196
std::string scratch;
197197
Slice record;
198198
WriteBatch batch;
199-
MemTable* mem = new MemTable(icmp_, options_.memtable_factory,
199+
MemTable* mem = new MemTable(icmp_, options_.memtable_factory.get(),
200200
options_.num_levels);
201201
mem->Ref();
202202
int counter = 0;

db/write_batch_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace rocksdb {
2222
static std::string PrintContents(WriteBatch* b) {
2323
InternalKeyComparator cmp(BytewiseComparator());
2424
auto factory = std::make_shared<SkipListFactory>();
25-
MemTable* mem = new MemTable(cmp, factory);
25+
MemTable* mem = new MemTable(cmp, factory.get());
2626
mem->Ref();
2727
std::string state;
2828
Options options;

table/block_based_table_builder.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ BlockBasedTableBuilder::BlockBasedTableBuilder(
127127
rep_->filter_block->StartBlock(0);
128128
}
129129
if (options.block_cache_compressed.get() != nullptr) {
130-
BlockBasedTable::GenerateCachePrefix(options.block_cache_compressed, file,
131-
&rep_->compressed_cache_key_prefix[0],
132-
&rep_->compressed_cache_key_prefix_size);
130+
BlockBasedTable::GenerateCachePrefix(
131+
options.block_cache_compressed.get(), file,
132+
&rep_->compressed_cache_key_prefix[0],
133+
&rep_->compressed_cache_key_prefix_size);
133134
}
134135
}
135136

table/block_based_table_reader.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ void BlockBasedTable::SetupCacheKeyPrefix(Rep* rep) {
9595
rep->cache_key_prefix_size = 0;
9696
rep->compressed_cache_key_prefix_size = 0;
9797
if (rep->options.block_cache != nullptr) {
98-
GenerateCachePrefix(rep->options.block_cache, rep->file.get(),
98+
GenerateCachePrefix(rep->options.block_cache.get(), rep->file.get(),
9999
&rep->cache_key_prefix[0],
100100
&rep->cache_key_prefix_size);
101101
}
102102
if (rep->options.block_cache_compressed != nullptr) {
103-
GenerateCachePrefix(rep->options.block_cache_compressed, rep->file.get(),
104-
&rep->compressed_cache_key_prefix[0],
103+
GenerateCachePrefix(rep->options.block_cache_compressed.get(),
104+
rep->file.get(), &rep->compressed_cache_key_prefix[0],
105105
&rep->compressed_cache_key_prefix_size);
106106
}
107107
}
108108

109-
void BlockBasedTable::GenerateCachePrefix(shared_ptr<Cache> cc,
109+
void BlockBasedTable::GenerateCachePrefix(Cache* cc,
110110
RandomAccessFile* file, char* buffer, size_t* size) {
111111

112112
// generate an id from the file
@@ -120,7 +120,7 @@ void BlockBasedTable::GenerateCachePrefix(shared_ptr<Cache> cc,
120120
}
121121
}
122122

123-
void BlockBasedTable::GenerateCachePrefix(shared_ptr<Cache> cc,
123+
void BlockBasedTable::GenerateCachePrefix(Cache* cc,
124124
WritableFile* file, char* buffer, size_t* size) {
125125

126126
// generate an id from the file

table/block_based_table_reader.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ class BlockBasedTable : public TableReader {
167167
rep_ = rep;
168168
}
169169
// Generate a cache key prefix from the file
170-
static void GenerateCachePrefix(shared_ptr<Cache> cc,
170+
static void GenerateCachePrefix(Cache* cc,
171171
RandomAccessFile* file, char* buffer, size_t* size);
172-
static void GenerateCachePrefix(shared_ptr<Cache> cc,
172+
static void GenerateCachePrefix(Cache* cc,
173173
WritableFile* file, char* buffer, size_t* size);
174174

175175
// The longest prefix of the cache key used to identify blocks.

table/table_test.cc

+18-18
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,15 @@ class MemTableConstructor: public Constructor {
370370
: Constructor(cmp),
371371
internal_comparator_(cmp),
372372
table_factory_(new SkipListFactory) {
373-
memtable_ = new MemTable(internal_comparator_, table_factory_);
373+
memtable_ = new MemTable(internal_comparator_, table_factory_.get());
374374
memtable_->Ref();
375375
}
376376
~MemTableConstructor() {
377377
delete memtable_->Unref();
378378
}
379379
virtual Status FinishImpl(const Options& options, const KVMap& data) {
380380
delete memtable_->Unref();
381-
memtable_ = new MemTable(internal_comparator_, table_factory_);
381+
memtable_ = new MemTable(internal_comparator_, table_factory_.get());
382382
memtable_->Ref();
383383
int seq = 1;
384384
for (KVMap::const_iterator it = data.begin();
@@ -930,19 +930,19 @@ TEST(TableTest, NumBlockStat) {
930930

931931
class BlockCacheProperties {
932932
public:
933-
explicit BlockCacheProperties(std::shared_ptr<Statistics> statistics) {
933+
explicit BlockCacheProperties(Statistics* statistics) {
934934
block_cache_miss =
935-
statistics.get()->getTickerCount(BLOCK_CACHE_MISS);
935+
statistics->getTickerCount(BLOCK_CACHE_MISS);
936936
block_cache_hit =
937-
statistics.get()->getTickerCount(BLOCK_CACHE_HIT);
937+
statistics->getTickerCount(BLOCK_CACHE_HIT);
938938
index_block_cache_miss =
939-
statistics.get()->getTickerCount(BLOCK_CACHE_INDEX_MISS);
939+
statistics->getTickerCount(BLOCK_CACHE_INDEX_MISS);
940940
index_block_cache_hit =
941-
statistics.get()->getTickerCount(BLOCK_CACHE_INDEX_HIT);
941+
statistics->getTickerCount(BLOCK_CACHE_INDEX_HIT);
942942
data_block_cache_miss =
943-
statistics.get()->getTickerCount(BLOCK_CACHE_DATA_MISS);
943+
statistics->getTickerCount(BLOCK_CACHE_DATA_MISS);
944944
data_block_cache_hit =
945-
statistics.get()->getTickerCount(BLOCK_CACHE_DATA_HIT);
945+
statistics->getTickerCount(BLOCK_CACHE_DATA_HIT);
946946
}
947947

948948
// Check if the fetched props matches the expected ones.
@@ -993,7 +993,7 @@ TEST(TableTest, BlockCacheTest) {
993993

994994
// At first, no block will be accessed.
995995
{
996-
BlockCacheProperties props(options.statistics);
996+
BlockCacheProperties props(options.statistics.get());
997997
// index will be added to block cache.
998998
props.AssertEqual(
999999
1, // index block miss
@@ -1006,7 +1006,7 @@ TEST(TableTest, BlockCacheTest) {
10061006
// Only index block will be accessed
10071007
{
10081008
iter.reset(c.NewIterator());
1009-
BlockCacheProperties props(options.statistics);
1009+
BlockCacheProperties props(options.statistics.get());
10101010
// NOTE: to help better highlight the "detla" of each ticker, I use
10111011
// <last_value> + <added_value> to indicate the increment of changed
10121012
// value; other numbers remain the same.
@@ -1021,7 +1021,7 @@ TEST(TableTest, BlockCacheTest) {
10211021
// Only data block will be accessed
10221022
{
10231023
iter->SeekToFirst();
1024-
BlockCacheProperties props(options.statistics);
1024+
BlockCacheProperties props(options.statistics.get());
10251025
props.AssertEqual(
10261026
1,
10271027
1,
@@ -1034,7 +1034,7 @@ TEST(TableTest, BlockCacheTest) {
10341034
{
10351035
iter.reset(c.NewIterator());
10361036
iter->SeekToFirst();
1037-
BlockCacheProperties props(options.statistics);
1037+
BlockCacheProperties props(options.statistics.get());
10381038
props.AssertEqual(
10391039
1,
10401040
1 + 1, // index block hit
@@ -1054,7 +1054,7 @@ TEST(TableTest, BlockCacheTest) {
10541054
iter.reset(c.NewIterator());
10551055
iter->SeekToFirst();
10561056
ASSERT_EQ("key", iter->key().ToString());
1057-
BlockCacheProperties props(options.statistics);
1057+
BlockCacheProperties props(options.statistics.get());
10581058
// Nothing is affected at all
10591059
props.AssertEqual(0, 0, 0, 0);
10601060
}
@@ -1065,7 +1065,7 @@ TEST(TableTest, BlockCacheTest) {
10651065
options.block_cache = NewLRUCache(1);
10661066
c.Reopen(options);
10671067
{
1068-
BlockCacheProperties props(options.statistics);
1068+
BlockCacheProperties props(options.statistics.get());
10691069
props.AssertEqual(
10701070
1, // index block miss
10711071
0,
@@ -1080,7 +1080,7 @@ TEST(TableTest, BlockCacheTest) {
10801080
// It first cache index block then data block. But since the cache size
10811081
// is only 1, index block will be purged after data block is inserted.
10821082
iter.reset(c.NewIterator());
1083-
BlockCacheProperties props(options.statistics);
1083+
BlockCacheProperties props(options.statistics.get());
10841084
props.AssertEqual(
10851085
1 + 1, // index block miss
10861086
0,
@@ -1093,7 +1093,7 @@ TEST(TableTest, BlockCacheTest) {
10931093
// SeekToFirst() accesses data block. With similar reason, we expect data
10941094
// block's cache miss.
10951095
iter->SeekToFirst();
1096-
BlockCacheProperties props(options.statistics);
1096+
BlockCacheProperties props(options.statistics.get());
10971097
props.AssertEqual(
10981098
2,
10991099
0,
@@ -1268,7 +1268,7 @@ class MemTableTest { };
12681268
TEST(MemTableTest, Simple) {
12691269
InternalKeyComparator cmp(BytewiseComparator());
12701270
auto table_factory = std::make_shared<SkipListFactory>();
1271-
MemTable* memtable = new MemTable(cmp, table_factory);
1271+
MemTable* memtable = new MemTable(cmp, table_factory.get());
12721272
memtable->Ref();
12731273
WriteBatch batch;
12741274
Options options;

0 commit comments

Comments
 (0)