avoid copying when iterating using range-based for (#4459)

Summary:
this avoids a few copies of std::string and other structs
in the context of range-based for loops. instead of copying
the values for each iteration, use a const reference to avoid
copying.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4459

Differential Revision: D10282045

Pulled By: sagar0

fbshipit-source-id: 5012e910dca279abd2be847e1fb432d96274edfb
This commit is contained in:
jsteemann 2018-10-09 17:13:53 -07:00 committed by Facebook Github Bot
parent f45c0d20de
commit 141ef7f8d3
5 changed files with 9 additions and 9 deletions

View File

@ -937,8 +937,8 @@ Status CompactionPicker::SanitizeCompactionInputFiles(
// any currently-existing files. // any currently-existing files.
for (auto file_num : *input_files) { for (auto file_num : *input_files) {
bool found = false; bool found = false;
for (auto level_meta : cf_meta.levels) { for (const auto& level_meta : cf_meta.levels) {
for (auto file_meta : level_meta.files) { for (const auto& file_meta : level_meta.files) {
if (file_num == TableFileNameToNumber(file_meta.name)) { if (file_num == TableFileNameToNumber(file_meta.name)) {
if (file_meta.being_compacted) { if (file_meta.being_compacted) {
return Status::Aborted("Specified compaction input file " + return Status::Aborted("Specified compaction input file " +

View File

@ -126,7 +126,7 @@ Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,
// create names of the live files. The names are not absolute // create names of the live files. The names are not absolute
// paths, instead they are relative to dbname_; // paths, instead they are relative to dbname_;
for (auto live_file : live) { for (const auto& live_file : live) {
ret.push_back(MakeTableFileName("", live_file.GetNumber())); ret.push_back(MakeTableFileName("", live_file.GetNumber()));
} }

View File

@ -547,7 +547,7 @@ Status DBImpl::CompactFilesImpl(
} }
std::unordered_set<uint64_t> input_set; std::unordered_set<uint64_t> input_set;
for (auto file_name : input_file_names) { for (const auto& file_name : input_file_names) {
input_set.insert(TableFileNameToNumber(file_name)); input_set.insert(TableFileNameToNumber(file_name));
} }
@ -579,7 +579,7 @@ Status DBImpl::CompactFilesImpl(
return s; return s;
} }
for (auto inputs : input_files) { for (const auto& inputs : input_files) {
if (cfd->compaction_picker()->AreFilesInCompaction(inputs.files)) { if (cfd->compaction_picker()->AreFilesInCompaction(inputs.files)) {
return Status::Aborted( return Status::Aborted(
"Some of the necessary compaction input " "Some of the necessary compaction input "

View File

@ -42,7 +42,7 @@ void DumpDBFileSummary(const ImmutableDBOptions& options,
"Error when reading %s dir\n", dbname.c_str()); "Error when reading %s dir\n", dbname.c_str());
} }
std::sort(files.begin(), files.end()); std::sort(files.begin(), files.end());
for (std::string file : files) { for (const std::string& file : files) {
if (!ParseFileName(file, &number, &type)) { if (!ParseFileName(file, &number, &type)) {
continue; continue;
} }
@ -85,7 +85,7 @@ void DumpDBFileSummary(const ImmutableDBOptions& options,
continue; continue;
} }
std::sort(files.begin(), files.end()); std::sort(files.begin(), files.end());
for (std::string file : files) { for (const std::string& file : files) {
if (ParseFileName(file, &number, &type)) { if (ParseFileName(file, &number, &type)) {
if (type == kTableFile && ++file_num < 10) { if (type == kTableFile && ++file_num < 10) {
file_info.append(file).append(" "); file_info.append(file).append(" ");
@ -109,7 +109,7 @@ void DumpDBFileSummary(const ImmutableDBOptions& options,
return; return;
} }
wal_info.clear(); wal_info.clear();
for (std::string file : files) { for (const std::string& file : files) {
if (ParseFileName(file, &number, &type)) { if (ParseFileName(file, &number, &type)) {
if (type == kLogFile) { if (type == kLogFile) {
env->GetFileSize(options.wal_dir + "/" + file, &file_size); env->GetFileSize(options.wal_dir + "/" + file, &file_size);

View File

@ -222,7 +222,7 @@ GeoIterator* GeoDBImpl::SearchRadial(const GeoPosition& pos,
Iterator* iter = db_->NewIterator(ReadOptions()); Iterator* iter = db_->NewIterator(ReadOptions());
// Process each prospective quadkey // Process each prospective quadkey
for (std::string qid : qids) { for (const std::string& qid : qids) {
// The user is interested in only these many objects. // The user is interested in only these many objects.
if (number_of_values == 0) { if (number_of_values == 0) {
break; break;