-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using reserve for optimize inserts and minor fixes #723
Conversation
plugins/arclite/archive.hpp
Outdated
@@ -169,7 +169,7 @@ class ArcAPI { | |||
static const ArcCodecs& codecs() { | |||
return get()->arc_codecs; | |||
} | |||
static size_t Count7zCodecs() { | |||
static const size_t Count7zCodecs() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const is useless on return types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix it here d832463
plugins/arclite/plugin.cpp
Outdated
@@ -702,8 +702,9 @@ class Plugin | |||
}); | |||
const auto error_log = std::make_shared<ErrorLog>(); | |||
size_t im = 0, nm = matched_indices.size(); | |||
std::vector<UInt32> indices; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the semantics: previously on each iteration the vector was recreated, now it's outside the loop and will accumulate all the values.
If you want to optimize here, add indices.clear()
to the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u ar absolutely right, I have changed original behavior function.
fix it here d832463
@alabuzhev, |
plugins/arclite/archive.cpp
Outdated
@@ -233,6 +233,7 @@ class MyCompressCodecsInfo : public ICompressCodecsInfo, public IHashers, privat | |||
} | |||
if (arc_lib.ComHashers) { | |||
UInt32 numHashers = arc_lib.ComHashers->GetNumHashers(); | |||
hashers_.reserve(numHashers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it, all this code is a part of a bigger loop, and calling reserve
in a loop is a known antipattern: it is too easy to pessimise exponential growth down to linear.
I suggest leaving it as is, it's not like like this place is a bottleneck anyway.
plugins/arclite/archive.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above: add_hashers
is calling from a for
loop below, and then again from a while
loop.
reserve
ing here would effectively kill exponential growth,
plugins/arclite/plugin.cpp
Outdated
while (im < nm) { | ||
std::vector<UInt32> indices; | ||
if (!indices.empty()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is redundant, clear
checks it internally.
Signed-off-by: German Semenov <[email protected]>
Kudos, SonarCloud Quality Gate passed! |
Summary
Useful changes, additional insert were found at
emplace_back
, but memory didnt allocated for last insert after loop.