diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-23 22:44:02 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-23 22:44:02 +1000 |
commit | 0aee45a1174cecca6ed4bd703ef6299185fec6b8 (patch) | |
tree | e0c7d3723406fc96e6091a28df60f490efb504e1 /source/game/StarItemDatabase.hpp | |
parent | 121d27446b42c960014b2e69999dad73322b05f3 (diff) |
Cache certain item generation calls from interfaces
Helps a little with the lag from recipes when having crafting interfaces open, but it's still noticeable.
Also micro-optimized Root maintenance by unlocking the Root mutexes for their respective shared_ptrs earlier once we have our own shared_ptr.
Diffstat (limited to 'source/game/StarItemDatabase.hpp')
-rw-r--r-- | source/game/StarItemDatabase.hpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/source/game/StarItemDatabase.hpp b/source/game/StarItemDatabase.hpp index 2c1fb1c..15dd7ee 100644 --- a/source/game/StarItemDatabase.hpp +++ b/source/game/StarItemDatabase.hpp @@ -6,6 +6,7 @@ #include "StarItem.hpp" #include "StarCasting.hpp" #include "StarLuaRoot.hpp" +#include "StarTtlCache.hpp" namespace Star { @@ -75,6 +76,8 @@ public: ItemDatabase(); + void cleanup(); + // Load an item based on item descriptor. If loadItem is called with a // live ptr, and the ptr matches the descriptor read, then no new item is // constructed. If ItemT is some other type than Item, then loadItem will @@ -112,8 +115,12 @@ public: // from the appropriate factory. If there is a problem instantiating the // item, will return a default item instead. If item is passed a null // ItemDescriptor, it will return a null pointer. + // The returned item pointer will be shared. Either call ->clone() or use item() instead for a copy. + ItemPtr itemShared(ItemDescriptor descriptor, Maybe<float> level = {}, Maybe<uint64_t> seed = {}) const; + // Same as itemShared, but makes a copy instead. Does not cache. ItemPtr item(ItemDescriptor descriptor, Maybe<float> level = {}, Maybe<uint64_t> seed = {}) const; + bool hasRecipeToMake(ItemDescriptor const& item) const; bool hasRecipeToMake(ItemDescriptor const& item, StringSet const& allowedTypes) const; @@ -153,6 +160,7 @@ private: }; static ItemPtr createItem(ItemType type, ItemConfig const& config); + ItemPtr tryCreateItem(ItemDescriptor const& descriptor, Maybe<float> level = {}, Maybe<uint64_t> seed = {}) const; ItemData const& itemData(String const& name) const; ItemRecipe makeRecipe(List<ItemDescriptor> inputs, ItemDescriptor output, float duration, StringSet groups) const; @@ -171,6 +179,11 @@ private: mutable RecursiveMutex m_luaMutex; LuaRootPtr m_luaRoot; + + typedef tuple<ItemDescriptor, Maybe<float>, Maybe<uint64_t>> ItemCacheEntry; + + mutable Mutex m_cacheMutex; + mutable HashTtlCache<ItemCacheEntry, ItemPtr> m_itemCache; }; template <typename ItemT> |