Icinga / icinga2

The core of our monitoring platform with a powerful configuration language and REST API.
https://icinga.com/docs/icinga2/latest
GNU General Public License v2.0
2.03k stars 578 forks source link

Avoid using `std::distance` in a loop #9503

Closed yhabteab closed 2 years ago

yhabteab commented 2 years ago

Describe the bug

IcingaDB::ChunkObjects() uses std::distance repeatedly to increment the offset iterator by the chunk size each time. However, this computation can also be accomplished through objects.size(), which only has a constant time complexity compared to std::distance in a while loop. https://github.com/Icinga/icinga2/blob/3a8abdcc3b85a3be62a0805b465e5fac55ae0538/lib/icingadb/icingadb-objects.cpp#L558-L561

A minor detail: Replace boost::intrusive_ptr<ConfigObject> with our ::Ptr for the IcingaDB::ChunkObjects() return type and objects parameter. https://github.com/Icinga/icinga2/blob/3a8abdcc3b85a3be62a0805b465e5fac55ae0538/lib/icingadb/icingadb-objects.cpp#L553-L554

yhabteab commented 2 years ago

std::distance also has a constant time complexity with random-access iterators and since vectors do have a support for random-access iterators, it's the same overhead as vector::size(). So, I'm going to close this!