Bläddra i källkod

Use small_vector for bonus list to reduce allocations

Ivan Savenko 10 månader sedan
förälder
incheckning
ab45c58e26

+ 0 - 2
AI/Nullkiller/Pathfinding/AINodeStorage.h

@@ -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

+ 2 - 0
Global.h

@@ -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>

+ 0 - 6
lib/bonuses/BonusList.cpp

@@ -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);

+ 1 - 2
lib/bonuses/BonusList.h

@@ -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]; }

+ 0 - 3
lib/pathfinder/CPathfinder.h

@@ -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;

+ 9 - 0
lib/serializer/BinaryDeserializer.h

@@ -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)
 	{

+ 9 - 0
lib/serializer/BinarySerializer.h

@@ -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)
 	{