Skip to content

Commit

Permalink
Make for-loops more efficient
Browse files Browse the repository at this point in the history
It saves a lot of work reserving and working with references instead of copying directly
  • Loading branch information
AZero13 committed Oct 24, 2022
1 parent 35c1f6b commit 43d37d0
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/vcpkg/base/cofffilereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ namespace vcpkg
{
std::vector<MachineType> machine_types; // used as a set because n is tiny
// Next we have the obj and pseudo-object files
for (size_t idx = 0; idx < member_offsets.size(); ++idx)
for (unsigned int offset : member_offsets)
{
const auto offset = member_offsets[idx];
// Skip the header, no need to read it
Checks::check_exit(VCPKG_LINE_INFO, fs.seek(offset + sizeof(ArchiveMemberHeader), SEEK_SET) == 0);
uint16_t machine_type_raw;
Expand Down
8 changes: 4 additions & 4 deletions src/vcpkg/base/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,7 @@ namespace vcpkg
virtual std::vector<Path> get_files_recursive(const Path& dir, std::error_code& ec) const override
{
std::vector<Path> result;
Path out_base = dir;
const Path& out_base = dir;
get_files_recursive_impl(result, dir, out_base, ec, true, true, true);
return result;
}
Expand All @@ -2373,7 +2373,7 @@ namespace vcpkg
virtual std::vector<Path> get_directories_recursive(const Path& dir, std::error_code& ec) const override
{
std::vector<Path> result;
Path out_base = dir;
const Path& out_base = dir;
get_files_recursive_impl(result, dir, out_base, ec, true, false, false);

return result;
Expand Down Expand Up @@ -2403,7 +2403,7 @@ namespace vcpkg
virtual std::vector<Path> get_regular_files_recursive(const Path& dir, std::error_code& ec) const override
{
std::vector<Path> result;
Path out_base = dir;
const Path& out_base = dir;
get_files_recursive_impl(result, dir, out_base, ec, false, true, false);
return result;
}
Expand Down Expand Up @@ -3310,7 +3310,7 @@ namespace vcpkg
#endif // ^^^!_WIN32

static const std::vector<Path> path_bases = calculate_path_bases();
for (Path path_base : path_bases)
for (const Path& path_base : path_bases)
{
for (auto&& stem : stems)
{
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/cmakevars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace vcpkg::CMakeVars
Triplet host_triplet) const
{
std::vector<FullPackageSpec> install_package_specs;
install_package_specs.reserve(action_plan.install_actions.size());
for (auto&& action : action_plan.install_actions)
{
install_package_specs.emplace_back(FullPackageSpec{action.spec, action.feature_list});
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/commands.fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace vcpkg::Commands::Fetch
const auto parsed = args.parse_arguments(COMMAND_STRUCTURE);
const bool stderr_status = Util::Sets::contains(parsed.switches, STDERR_STATUS.name);
const std::string tool = args.command_arguments[0];
const Path tool_path = paths.get_tool_exe(tool, stderr_status ? stderr_sink : stdout_sink);
const Path& tool_path = paths.get_tool_exe(tool, stderr_status ? stderr_sink : stdout_sink);
msg::write_unlocalized_text_to_stdout(Color::none, tool_path.native() + '\n');
Checks::exit_success(VCPKG_LINE_INFO);
}
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/commands.format-manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace

Optional<ToWrite> read_manifest(Filesystem& fs, Path&& manifest_path)
{
auto path_string = manifest_path.native();
const auto& path_string = manifest_path.native();
Debug::println("Reading ", path_string);
auto contents = fs.read_contents(manifest_path, VCPKG_LINE_INFO);
auto parsed_json_opt = Json::parse(contents, manifest_path);
Expand Down
3 changes: 3 additions & 0 deletions src/vcpkg/export.prefab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace vcpkg::Export::Prefab
static std::string jsonify(const std::vector<std::string>& dependencies)
{
std::vector<std::string> deps;
deps.reserve(dependencies.size());
for (const auto& dep : dependencies)
{
deps.push_back("\"" + dep + "\"");
Expand Down Expand Up @@ -497,6 +498,7 @@ namespace vcpkg::Export::Prefab
utils.write_contents(prefab_path, pm.to_json(), VCPKG_LINE_INFO);

std::vector<std::string> triplet_names;
triplet_names.reserve(triplets.size());
for (auto&& triplet : triplets)
{
triplet_names.push_back(triplet.canonical_name());
Expand All @@ -523,6 +525,7 @@ namespace vcpkg::Export::Prefab

std::vector<Path> modules_shared = find_modules(paths, libs, ".so");

modules.reserve(modules_shared.size());
for (const auto& module : modules_shared)
{
modules.push_back(module);
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/vcpkglib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace vcpkg
auto pghs = Paragraphs::get_paragraphs(fs, vcpkg_dir_status_file).value_or_exit(VCPKG_LINE_INFO);

std::vector<std::unique_ptr<StatusParagraph>> status_pghs;
status_pghs.reserve(pghs.size());
for (auto&& p : pghs)
{
status_pghs.push_back(std::make_unique<StatusParagraph>(std::move(p)));
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/vcpkgpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ namespace vcpkg
m_pimpl->m_registry_set = m_pimpl->m_config.instantiate_registry_set(*this);
}

for (std::string triplet : this->overlay_triplets)
for (const std::string& triplet : this->overlay_triplets)
{
m_pimpl->triplets_dirs.emplace_back(filesystem.almost_canonical(triplet, VCPKG_LINE_INFO));
}
Expand Down

0 comments on commit 43d37d0

Please sign in to comment.