1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-13 11:40:38 +02:00

Use small_vector for bonus list to reduce allocations

This commit is contained in:
Ivan Savenko 2024-12-18 13:36:11 +00:00
parent 56d5911452
commit ab45c58e26
7 changed files with 21 additions and 13 deletions

View File

@ -23,8 +23,6 @@ constexpr int NKAI_GRAPH_TRACE_LEVEL = 0;
#include "Actions/SpecialAction.h"
#include "Actors.h"
#include <boost/container/small_vector.hpp>
namespace NKAI
{
namespace AIPathfinding

View File

@ -168,6 +168,8 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
#include <boost/algorithm/string.hpp>
#include <boost/crc.hpp>
#include <boost/current_function.hpp>
#include <boost/container/small_vector.hpp>
#include <boost/container/static_vector.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/time_formatters.hpp>
#include <boost/filesystem.hpp>

View File

@ -195,7 +195,6 @@ std::shared_ptr<const Bonus> BonusList::getFirst(const CSelector &selector) cons
void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
{
out.reserve(bonuses.size());
for(const auto & b : bonuses)
{
if(selector(b.get()) && (!limit || ((bool)limit && limit(b.get()))))
@ -259,11 +258,6 @@ void BonusList::resize(BonusList::TInternalContainer::size_type sz, const std::s
changed();
}
void BonusList::reserve(TInternalContainer::size_type sz)
{
bonuses.reserve(sz);
}
void BonusList::insert(BonusList::TInternalContainer::iterator position, BonusList::TInternalContainer::size_type n, const std::shared_ptr<Bonus> & x)
{
bonuses.insert(position, n, x);

View File

@ -17,7 +17,7 @@ VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE BonusList
{
public:
using TInternalContainer = std::vector<std::shared_ptr<Bonus>>;
using TInternalContainer = boost::container::small_vector<std::shared_ptr<Bonus>, 16>;
private:
TInternalContainer bonuses;
@ -43,7 +43,6 @@ public:
void clear();
bool empty() const { return bonuses.empty(); }
void resize(TInternalContainer::size_type sz, const std::shared_ptr<Bonus> & c = nullptr);
void reserve(TInternalContainer::size_type sz);
TInternalContainer::size_type capacity() const { return bonuses.capacity(); }
STRONG_INLINE std::shared_ptr<Bonus> &operator[] (TInternalContainer::size_type n) { return bonuses[n]; }
STRONG_INLINE const std::shared_ptr<Bonus> &operator[] (TInternalContainer::size_type n) const { return bonuses[n]; }

View File

@ -13,9 +13,6 @@
#include "../IGameCallback.h"
#include "../bonuses/BonusEnum.h"
#include <boost/container/static_vector.hpp>
#include <boost/container/small_vector.hpp>
VCMI_LIB_NAMESPACE_BEGIN
class CGWhirlpool;

View File

@ -215,6 +215,15 @@ public:
load( data[i]);
}
template <typename T, size_t N>
void load(boost::container::small_vector<T, N>& data)
{
uint32_t length = readAndCheckLength();
data.resize(length);
for (uint32_t i = 0; i < length; i++)
load(data[i]);
}
template <typename T, typename std::enable_if_t < !std::is_same_v<T, bool >, int > = 0>
void load(std::deque<T> & data)
{

View File

@ -275,6 +275,15 @@ public:
for(uint32_t i=0;i<length;i++)
save(data[i]);
}
template <typename T, size_t N>
void save(const boost::container::small_vector<T, N>& data)
{
uint32_t length = data.size();
*this& length;
for (uint32_t i = 0; i < length; i++)
save(data[i]);
}
template <typename T, typename std::enable_if_t < !std::is_same_v<T, bool >, int > = 0>
void save(const std::deque<T> & data)
{