feat: Add global bytecode cache for Eluna scripts (#293)

This commit is contained in:
iThorgrim
2025-08-29 14:27:57 +02:00
committed by GitHub
parent 6fd81ea0e6
commit 65af80f08d
4 changed files with 319 additions and 5 deletions

View File

@@ -26,6 +26,9 @@
#include "LootMgr.h"
#include <mutex>
#include <memory>
#include <vector>
#include <ctime>
#include <unordered_map>
extern "C"
{
@@ -73,12 +76,28 @@ template<typename T> struct EventKey;
template<typename T> struct EntryKey;
template<typename T> struct UniqueObjectKey;
// Type definition for bytecode buffer
typedef std::vector<uint8> BytecodeBuffer;
// Global bytecode cache entry
struct GlobalCacheEntry
{
BytecodeBuffer bytecode;
std::time_t last_modified;
std::string filepath;
GlobalCacheEntry() : last_modified(0) {}
GlobalCacheEntry(const BytecodeBuffer& code, std::time_t modTime, const std::string& path)
: bytecode(code), last_modified(modTime), filepath(path) {}
};
struct LuaScript
{
std::string fileext;
std::string filename;
std::string filepath;
std::string modulepath;
LuaScript() {}
};
#define ELUNA_STATE_PTR "Eluna State Ptr"
@@ -150,6 +169,18 @@ private:
static void LoadScriptPaths();
static void GetScripts(std::string path);
static void AddScriptPath(std::string filename, const std::string& fullpath);
static int LoadCompiledScript(lua_State* L, const std::string& filepath);
static std::time_t GetFileModTime(const std::string& filepath);
static std::time_t GetFileModTimeWithCache(const std::string& filepath);
// Global cache management
static bool CompileScriptToGlobalCache(const std::string& filepath);
static bool CompileMoonScriptToGlobalCache(const std::string& filepath);
static int TryLoadFromGlobalCache(lua_State* L, const std::string& filepath);
static int LoadScriptWithCache(lua_State* L, const std::string& filepath, bool isMoonScript, uint32* compiledCount = nullptr, uint32* cachedCount = nullptr);
static void ClearGlobalCache();
static void ClearTimestampCache();
static size_t GetGlobalCacheSize();
static int StackTrace(lua_State *_L);
static void Report(lua_State* _L);