From d9e9db0983e9fc2cbe87d0152e16f866133170f3 Mon Sep 17 00:00:00 2001 From: Petr Mikheev Date: Fri, 13 Jan 2023 21:14:23 +0100 Subject: [PATCH] Additional information in Lua profiler --- apps/openmw/mwlua/luamanagerimp.cpp | 4 ++++ apps/openmw/mwlua/uibindings.cpp | 7 ++++--- components/lua/scriptscontainer.cpp | 4 ++++ components/lua/scriptscontainer.hpp | 3 +++ components/lua_ui/content.cpp | 5 ++++- components/lua_ui/content.hpp | 20 ++++++++++++++++++-- components/lua_ui/element.cpp | 2 +- 7 files changed, 38 insertions(+), 7 deletions(-) diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index 8516e5e21f..129b9161a7 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -18,6 +18,7 @@ #include +#include #include #include "../mwbase/windowmanager.hpp" @@ -639,6 +640,9 @@ namespace MWLua out << "Total memory usage:"; outMemSize(mLua.getTotalMemoryUsage()); out << "\n"; + out << "LuaUtil::ScriptsContainer count: " << LuaUtil::ScriptsContainer::getInstanceCount() << "\n"; + out << "LuaUi::Content count: " << LuaUi::Content::getInstanceCount() << "\n"; + out << "\n"; out << "small alloc max size = " << smallAllocSize << " (section [Lua] in settings.cfg)\n"; out << "Smaller values give more information for the profiler, but increase performance overhead.\n"; out << " Memory allocations <= " << smallAllocSize << " bytes:"; diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index ccce8b5b35..7095b2df63 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -115,7 +115,7 @@ namespace MWLua }; uiContent["add"] = [](LuaUi::Content& content, const sol::table& table) { content.insert(content.size(), table); }; - uiContent["indexOf"] = [](LuaUi::Content& content, const sol::table& table) -> sol::optional { + uiContent["indexOf"] = [](const LuaUi::Content& content, const sol::table& table) -> sol::optional { size_t index = content.indexOf(table); if (index < content.size()) return toLuaIndex(index); @@ -123,8 +123,9 @@ namespace MWLua return sol::nullopt; }; { - auto pairs = [](LuaUi::Content& content) { - auto next = [](LuaUi::Content& content, size_t i) -> sol::optional> { + auto pairs = [](const LuaUi::Content& content) { + auto next + = [](const LuaUi::Content& content, size_t i) -> sol::optional> { if (i < content.size()) return std::make_tuple(i + 1, content.at(i)); else diff --git a/components/lua/scriptscontainer.cpp b/components/lua/scriptscontainer.cpp index 6f18727019..eaf8c27af5 100644 --- a/components/lua/scriptscontainer.cpp +++ b/components/lua/scriptscontainer.cpp @@ -15,11 +15,14 @@ namespace LuaUtil static constexpr std::string_view HANDLER_LOAD = "onLoad"; static constexpr std::string_view HANDLER_INTERFACE_OVERRIDE = "onInterfaceOverride"; + int64_t ScriptsContainer::sInstanceCount = 0; + ScriptsContainer::ScriptsContainer(LuaUtil::LuaState* lua, std::string_view namePrefix) : mNamePrefix(namePrefix) , mLua(*lua) , mThis(std::make_shared(this)) { + sInstanceCount++; registerEngineHandlers({ &mUpdateHandlers }); mPublicInterfaces = sol::table(lua->sol(), sol::create); addPackage("openmw.interfaces", mPublicInterfaces); @@ -476,6 +479,7 @@ namespace LuaUtil ScriptsContainer::~ScriptsContainer() { + sInstanceCount--; for (auto& [_, script] : mScripts) script.mHiddenData[sScriptIdKey] = sol::nil; *mThis = nullptr; diff --git a/components/lua/scriptscontainer.hpp b/components/lua/scriptscontainer.hpp index 0177281be1..1592b3ae23 100644 --- a/components/lua/scriptscontainer.hpp +++ b/components/lua/scriptscontainer.hpp @@ -155,6 +155,7 @@ namespace LuaUtil int64_t mMemoryUsage = 0; // bytes }; void collectStats(std::vector& stats) const; + static int64_t getInstanceCount() { return sInstanceCount; } protected: struct Handler @@ -265,6 +266,8 @@ namespace LuaUtil std::map mRemovedScriptsMemoryUsage; std::shared_ptr mThis; // used by LuaState to track ownership of memory allocations + + static int64_t sInstanceCount; // debug information, shown in Lua profiler }; // Wrapper for a Lua function. diff --git a/components/lua_ui/content.cpp b/components/lua_ui/content.cpp index e7cf474bc9..3f66c17c4a 100644 --- a/components/lua_ui/content.cpp +++ b/components/lua_ui/content.cpp @@ -2,8 +2,11 @@ namespace LuaUi { + int64_t Content::sInstanceCount = 0; + Content::Content(const sol::table& table) { + sInstanceCount++; size_t size = table.size(); for (size_t index = 0; index < size; ++index) { @@ -94,7 +97,7 @@ namespace LuaUi return index; } - size_t Content::indexOf(const sol::table& table) + size_t Content::indexOf(const sol::table& table) const { auto it = std::find(mOrdered.begin(), mOrdered.end(), table); if (it == mOrdered.end()) diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index d16f1caef9..df005dc1c2 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -13,7 +13,20 @@ namespace LuaUi public: using iterator = std::vector::iterator; - Content() {} + Content() { sInstanceCount++; } + ~Content() { sInstanceCount--; } + Content(const Content& c) + { + this->mNamed = c.mNamed; + this->mOrdered = c.mOrdered; + sInstanceCount++; + } + Content(Content&& c) + { + this->mNamed = std::move(c.mNamed); + this->mOrdered = std::move(c.mOrdered); + sInstanceCount++; + } // expects a Lua array - a table with keys from 1 to n without any nil values in between // any other keys are ignored @@ -29,11 +42,14 @@ namespace LuaUi sol::table at(std::string_view name) const; size_t remove(size_t index); size_t remove(std::string_view name); - size_t indexOf(const sol::table& table); + size_t indexOf(const sol::table& table) const; + + static int64_t getInstanceCount() { return sInstanceCount; } private: std::map> mNamed; std::vector mOrdered; + static int64_t sInstanceCount; // debug information, shown in Lua profiler }; } diff --git a/components/lua_ui/element.cpp b/components/lua_ui/element.cpp index 1753b0c3fb..41de2536cd 100644 --- a/components/lua_ui/element.cpp +++ b/components/lua_ui/element.cpp @@ -65,7 +65,7 @@ namespace LuaUi } if (!contentObj.is()) throw std::logic_error("Layout content field must be a openmw.ui.content"); - Content content = contentObj.as(); + const Content& content = contentObj.as(); result.resize(content.size()); size_t minSize = std::min(children.size(), content.size()); for (size_t i = 0; i < minSize; i++)