1
0
mirror of synced 2024-11-30 18:34:29 +01:00

impr: Reserve space to avoid multiple allocations when loading scripts (#1929)

std::forward ensures that the arguments are perfectly forwarded to
loadScript
By reserving space in the features vector based on the size of scripts,
we can avoid multiple memory allocations during the loop. If an
exception occurs, returning an empty vector immediately clarifies that
no scripts were loaded. Without reservation, each call to emplace_back
could potentially trigger a reallocation if the current capacity is
exceeded, which is costly in terms of performance. This leads to more
efficient memory management and can significantly speed up the execution
time when dealing with a large number of scripts.


![image](https://github.com/user-attachments/assets/3e290162-fb8b-4f00-a71b-6009494b2dab)
This commit is contained in:
Shubhankar Sarangi 2024-11-24 15:58:45 +05:30 committed by GitHub
parent 0d4f3e5735
commit 4fcf732814
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,32 +32,33 @@ namespace {
scripts.emplace_back(&script);
}
std::vector<const Script*> loadAllScripts() {
std::vector<const Script*> scripts;
std::vector<const Script*> loadAllScripts() {
std::vector<const Script*> scripts;
try {
std::apply([&scripts](auto&&... args) {
(loadScript(scripts, args), ...);
}, s_loaders);
} catch (const std::exception &e) {
log::error("Error when loading scripts: {}", e.what());
}
{
std::vector<hex::Feature> features;
for (const auto &script : scripts) {
if (!script->background)
continue;
features.emplace_back(script->name, true);
}
IMHEX_PLUGIN_FEATURES = features;
}
return scripts;
try {
std::apply([&scripts](auto&&... args) {
(loadScript(scripts, std::forward<decltype(args)>(args)), ...);
}, s_loaders);
} catch (const std::exception &e) {
log::error("Error when loading scripts: {}", e.what());
return {};
}
std::vector<hex::Feature> features;
features.reserve(scripts.size());
for (const auto &script : scripts) {
if (script->background) {
features.emplace_back(script->name, true);
}
}
IMHEX_PLUGIN_FEATURES = std::move(features);
return scripts;
}
void initializeLoader(u32 &count, auto &loader) {
try {
if (loader.initialize())