Skip to content

Commit

Permalink
avoid copying when iterating using range-based for (facebook#4459)
Browse files Browse the repository at this point in the history
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: facebook#4459

Differential Revision: D10282045

Pulled By: sagar0

fbshipit-source-id: 5012e910dca279abd2be847e1fb432d96274edfb
  • Loading branch information
jsteemann authored and facebook-github-bot committed Oct 10, 2018
1 parent f45c0d2 commit 141ef7f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions db/compaction_picker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,8 @@ Status CompactionPicker::SanitizeCompactionInputFiles(
// any currently-existing files.
for (auto file_num : *input_files) {
bool found = false;
for (auto level_meta : cf_meta.levels) {
for (auto file_meta : level_meta.files) {
for (const auto& level_meta : cf_meta.levels) {
for (const auto& file_meta : level_meta.files) {
if (file_num == TableFileNameToNumber(file_meta.name)) {
if (file_meta.being_compacted) {
return Status::Aborted("Specified compaction input file " +
Expand Down
2 changes: 1 addition & 1 deletion db/db_filesnapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,

// create names of the live files. The names are not absolute
// 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()));
}

Expand Down
4 changes: 2 additions & 2 deletions db/db_impl_compaction_flush.cc
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ Status DBImpl::CompactFilesImpl(
}

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));
}

Expand Down Expand Up @@ -579,7 +579,7 @@ Status DBImpl::CompactFilesImpl(
return s;
}

for (auto inputs : input_files) {
for (const auto& inputs : input_files) {
if (cfd->compaction_picker()->AreFilesInCompaction(inputs.files)) {
return Status::Aborted(
"Some of the necessary compaction input "
Expand Down
6 changes: 3 additions & 3 deletions db/db_info_dumper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void DumpDBFileSummary(const ImmutableDBOptions& options,
"Error when reading %s dir\n", dbname.c_str());
}
std::sort(files.begin(), files.end());
for (std::string file : files) {
for (const std::string& file : files) {
if (!ParseFileName(file, &number, &type)) {
continue;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ void DumpDBFileSummary(const ImmutableDBOptions& options,
continue;
}
std::sort(files.begin(), files.end());
for (std::string file : files) {
for (const std::string& file : files) {
if (ParseFileName(file, &number, &type)) {
if (type == kTableFile && ++file_num < 10) {
file_info.append(file).append(" ");
Expand All @@ -109,7 +109,7 @@ void DumpDBFileSummary(const ImmutableDBOptions& options,
return;
}
wal_info.clear();
for (std::string file : files) {
for (const std::string& file : files) {
if (ParseFileName(file, &number, &type)) {
if (type == kLogFile) {
env->GetFileSize(options.wal_dir + "/" + file, &file_size);
Expand Down
2 changes: 1 addition & 1 deletion utilities/geodb/geodb_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ GeoIterator* GeoDBImpl::SearchRadial(const GeoPosition& pos,
Iterator* iter = db_->NewIterator(ReadOptions());

// Process each prospective quadkey
for (std::string qid : qids) {
for (const std::string& qid : qids) {
// The user is interested in only these many objects.
if (number_of_values == 0) {
break;
Expand Down

0 comments on commit 141ef7f

Please sign in to comment.