Pre-reserve looping components vector to reduce memory allocations (#9177)

This commit is contained in:
J. Nick Koston 2025-06-23 04:10:09 +02:00 committed by GitHub
parent 7fc5bfd787
commit 2a45467bf6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -257,6 +257,17 @@ void Application::teardown_components(uint32_t timeout_ms) {
} }
void Application::calculate_looping_components_() { void Application::calculate_looping_components_() {
// Count total components that need looping
size_t total_looping = 0;
for (auto *obj : this->components_) {
if (obj->has_overridden_loop()) {
total_looping++;
}
}
// Pre-reserve vector to avoid reallocations
this->looping_components_.reserve(total_looping);
// First add all active components // First add all active components
for (auto *obj : this->components_) { for (auto *obj : this->components_) {
if (obj->has_overridden_loop() && if (obj->has_overridden_loop() &&