Skip to content

Commit

Permalink
Using reserve for optimize inserts and minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: German Semenov <[email protected]>
  • Loading branch information
GermanAizek committed Aug 24, 2023
1 parent 5bc48ec commit d832463
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions plugins/arclite/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class MyCompressCodecsInfo : public ICompressCodecsInfo, public IHashers, privat
}
if (arc_lib.ComHashers) {
UInt32 numHashers = arc_lib.ComHashers->GetNumHashers();
hashers_.reserve(numHashers);
for (UInt32 i = 0; i < numHashers; ++i) {
CDllHasherInfo info;
info.LibIndex = static_cast<UInt32>(ilib);
Expand Down Expand Up @@ -400,6 +401,7 @@ void ArcAPI::load_codecs(const std::wstring& path) {
const auto& add_hashers = [this](ArcLib &arc_lib, size_t lib_index) {
if (arc_lib.ComHashers) {
UInt32 numHashers = arc_lib.ComHashers->GetNumHashers();
arc_hashers.reserve(numHashers);
for (UInt32 i = 0; i < numHashers; i++) {
CDllHasherInfo info;
info.LibIndex = static_cast<UInt32>(lib_index);
Expand Down
5 changes: 4 additions & 1 deletion plugins/arclite/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,11 @@ class Plugin
});
const auto error_log = std::make_shared<ErrorLog>();
size_t im = 0, nm = matched_indices.size();
std::vector<UInt32> indices;
indices.reserve(nm);
while (im < nm) {
std::vector<UInt32> indices;
if (!indices.empty())
indices.clear();
auto parent = archive->file_list[matched_indices[im]].parent;
while (im < nm && archive->file_list[matched_indices[im]].parent == parent)
indices.push_back(matched_indices[im++]);
Expand Down
2 changes: 1 addition & 1 deletion plugins/arclite/sfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class SfxOptionsDialog: public Far::Dialog {
bool show() {
label(Far::get_msg(MSG_SFX_OPTIONS_DLG_PROFILE));
std::vector<std::wstring> profile_names;
profile_names.reserve(profiles.size());
profile_names.reserve(profiles.size() + 1);
for (unsigned i = 0; i < profiles.size(); i++) {
profile_names.push_back(profiles[i].name);
}
Expand Down
8 changes: 6 additions & 2 deletions plugins/arclite/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ class UpdateDialog: public Far::Dialog {
DisableEvents de(*this);
std::vector<FarListItem> fl_items;
FarListItem fl_item{};
fl_items.reserve(profiles.size() + 1);
for (unsigned i = 0; i < profiles.size(); i++) {
fl_item.Text = profiles[i].name.c_str();
fl_items.push_back(fl_item);
Expand Down Expand Up @@ -1280,7 +1281,7 @@ class UpdateDialog: public Far::Dialog {
old_ext = ArcAPI::formats().at(m_options.arc_type).default_extension();

std::vector<std::wstring> profile_names;
profile_names.reserve(profiles.size());
profile_names.reserve(profiles.size() + 1);
unsigned profile_idx = static_cast<unsigned>(profiles.size());
std::for_each(profiles.begin(), profiles.end(), [&] (const UpdateProfile& profile) {
profile_names.push_back(profile.name);
Expand Down Expand Up @@ -1347,6 +1348,7 @@ class UpdateDialog: public Far::Dialog {
std::vector<std::wstring> other_format_names;
unsigned other_format_index = 0;
bool found = false;
other_format_names.reserve(other_formats.size());
for (const auto& t : other_formats) {
if (!found && t == m_options.arc_type) {
other_format_index = static_cast<unsigned>(other_format_names.size());
Expand Down Expand Up @@ -1380,7 +1382,9 @@ class UpdateDialog: public Far::Dialog {
std::vector<std::wstring> method_names;
const auto& codecs = ArcAPI::codecs();
unsigned method_sel = 0;
for (unsigned i = 0; i < ARRAYSIZE(c_methods) + ArcAPI::Count7zCodecs(); ++i) {
const auto sizeMethods = ARRAYSIZE(c_methods) + ArcAPI::Count7zCodecs();
method_names.reserve(sizeMethods);
for (unsigned i = 0; i < sizeMethods; ++i) {
std::wstring method_name = i < ARRAYSIZE(c_methods) ? c_methods[i].value : codecs[i - ARRAYSIZE(c_methods)].Name;
if (method == method_name)
method_sel = i;
Expand Down

0 comments on commit d832463

Please sign in to comment.