Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua (5.4) performance improvements #981

Merged
merged 4 commits into from Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions code/components/citizen-scripting-lua/src/LuaNativesLoader.cpp
@@ -1,10 +1,14 @@
#include "StdInc.h"

#ifndef IS_FXSERVER
#include <VFSManager.h>
#include <VFSZipFile.h>
#include <ResourceManager.h>

namespace fx
{
bool mountedAnyNatives = false;
}

static void MountNatives(const std::string& name)
{
static std::map<std::string, std::vector<uint8_t>> storedFiles;
Expand All @@ -30,6 +34,7 @@ static void MountNatives(const std::string& name)

if (device->OpenArchive(fmt::sprintf("memory:$%016llx,%d,0:%s", (uintptr_t)thisData, thisDataSize, name)))
{
fx::mountedAnyNatives = true;
vfs::Mount(device, fmt::sprintf("nativesLua:/%s/", name));
}
}
Expand All @@ -47,14 +52,15 @@ static InitFunction initFunction([]()
MountNatives("rdr3_universal");
#elif defined(GTA_NY)
MountNatives("ny_universal");
#else
#elif defined(GTA_FIVE)
MountNatives("natives_universal");
MountNatives("natives_21e43a33");
MountNatives("natives_0193d0af");
#elif defined(IS_FXSERVER)
MountNatives("natives_server");
#endif

mountedFiles = true;
}
});
});
#endif
29 changes: 21 additions & 8 deletions code/components/citizen-scripting-lua/src/LuaScriptNatives.cpp
Expand Up @@ -847,6 +847,23 @@ struct LuaArgumentParser
static_assert(sizeof(T) == 0, "Invalid PushObject");
}

template<typename T>
static LUA_INLINE T ParseInteger(lua_State* L, int idx)
{
const TValue* value = LUA_VALUE(L, idx);

if (ttisinteger(value))
{
return static_cast<T>(ivalue(value));
}
else if (ttisnumber(value))
{
return static_cast<T>(fltvalue(value));
}

return (!l_isfalse(value) ? 1 : 0);
}

static LUA_INLINE const char* ParseFunctionReference(lua_State* L, int idx)
{
return lua_tostring(L, idx); // @TODO: maybe?
Expand Down Expand Up @@ -893,30 +910,26 @@ LUA_INLINE double LuaArgumentParser::ParseArgument<double>(lua_State* L, int idx
template<>
LUA_INLINE int16_t LuaArgumentParser::ParseArgument<int16_t>(lua_State* L, int idx)
{
const TValue* value = LUA_VALUE(L, idx);
return ttisnumber(value) ? sc_nvalue(value, int16_t) : (!l_isfalse(value) ? 1 : 0);
return ParseInteger<int16_t>(L, idx);
}

template<>
LUA_INLINE int32_t LuaArgumentParser::ParseArgument<int32_t>(lua_State* L, int idx)
{
const TValue* value = LUA_VALUE(L, idx);
return ttisnumber(value) ? sc_nvalue(value, int32_t) : (!l_isfalse(value) ? 1 : 0);
return ParseInteger<int32_t>(L, idx);
}

template<>
LUA_INLINE int64_t LuaArgumentParser::ParseArgument<int64_t>(lua_State* L, int idx)
{
const TValue* value = LUA_VALUE(L, idx);
return ttisnumber(value) ? sc_nvalue(value, int64_t) : (!l_isfalse(value) ? 1 : 0);
return ParseInteger<int64_t>(L, idx);
}

#if defined(__GNUC__)
template<>
LUA_INLINE lua_Integer LuaArgumentParser::ParseArgument<lua_Integer>(lua_State* L, int idx)
{
const TValue* value = LUA_VALUE(L, idx);
return ttisnumber(value) ? sc_nvalue(value, lua_Integer) : (!l_isfalse(value) ? 1 : 0);
return ParseInteger<lua_Integer>(L, idx);
}
#endif

Expand Down
Expand Up @@ -989,15 +989,13 @@ result_t LuaScriptRuntime::Create(IScriptHost* scriptHost)
return FX_S_OK;
}

extern bool mountedAnyNatives;

result_t LuaScriptRuntime::LoadNativesBuild(const std::string& nativesBuild)
{
result_t hr = FX_S_OK;

bool useLazyNatives = false;

#if !defined(IS_FXSERVER)
useLazyNatives = true;
#endif
bool useLazyNatives = mountedAnyNatives;

if (!useLazyNatives)
{
Expand Down
4 changes: 3 additions & 1 deletion code/tools/build/run_postbuild.ps1
Expand Up @@ -172,8 +172,10 @@ if (!$IsServer) {
}

# useless client-related scripting stuff
Remove-Item -Force $LayoutDir\citizen\scripting\lua\*.zip
Remove-Item -Force $LayoutDir\citizen\scripting\lua\natives_0*.zip
Remove-Item -Force $LayoutDir\citizen\scripting\lua\natives_2*.zip
Remove-Item -Force $LayoutDir\citizen\scripting\lua\*_universal.lua
Remove-Item -Force $LayoutDir\citizen\scripting\lua\*_universal.zip
Remove-Item -Force $LayoutDir\citizen\scripting\lua\natives_0*.lua
Remove-Item -Force $LayoutDir\citizen\scripting\lua\natives_2*.lua

Expand Down
4 changes: 3 additions & 1 deletion code/tools/ci/build.ps1
Expand Up @@ -429,8 +429,10 @@ if (!$DontBuild -and $IsServer) {
Remove-Item -Force $WorkDir\out\server\citizen\system_resources\monitor\scripts\menu\client\cl_menu.lua

# useless client-related scripting stuff
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\*.zip
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\natives_0*.zip
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\natives_2*.zip
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\*_universal.lua
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\*_universal.zip
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\natives_0*.lua
Remove-Item -Force $WorkDir\out\server\citizen\scripting\lua\natives_2*.lua

Expand Down
8 changes: 6 additions & 2 deletions code/vendor/lua54.lua
Expand Up @@ -19,6 +19,9 @@ return {

if os.istarget('windows') then
flags { "LinkTimeOptimization" }

-- longjmp *should* be exception-safe on Windows non-x86
defines { "LUA_USE_LONGJMP" }
elseif os.istarget('linux') then
defines { "LUA_USE_POSIX" }
end
Expand All @@ -33,8 +36,9 @@ return {

--[[ Lua Extensions ]]
'LUA_SANDBOX', -- Disable many features within ldblib.c
'LUA_C99_MATHLIB ', -- Include c99 math functions in lmathlib
'LUA_CPP_EXCEPTIONS', -- @EXPERIMENT: unprotected calls are wrapped in typed C++ exceptions
'LUA_C99_MATHLIB', -- Include c99 math functions in lmathlib
-- disabled (worse yield performance)
--'LUA_CPP_EXCEPTIONS', -- @EXPERIMENT: unprotected calls are wrapped in typed C++ exceptions
'GRIT_POWER_COMPOUND', -- Add compound operators
'GRIT_POWER_INTABLE', -- Support for unpacking named values from tables using the 'in' keyword
'GRIT_POWER_TABINIT', -- Syntactic sugar to improve the syntax for specifying sets
Expand Down
9 changes: 8 additions & 1 deletion ext/natives/Makefile
Expand Up @@ -100,7 +100,7 @@ export NATIVES_NY_FOOTER
all: out \
out/NativesFive.cs out/NativesServer.cs out/NativesRDR3.cs out/NativesNY.cs \
out/rpc_natives.json \
out/natives_server.lua out/natives_server.d.ts out/natives_server.js \
out/natives_server.lua out/natives_server.d.ts out/natives_server.js out/natives_server.zip \
out/Natives.h out/NativesRDR.h out/NativesServer.h out/NativesNY.h \
$(GTA_OUTPUTS_LUA) $(GTA_OUTPUTS_JS) $(GTA_OUTPUTS_DTS) $(GTA_OUTPUTS_ZIP) \
$(RDR_OUTPUTS_LUA) $(RDR_OUTPUTS_JS) $(RDR_OUTPUTS_DTS) $(RDR_OUTPUTS_ZIP) \
Expand Down Expand Up @@ -161,6 +161,13 @@ out/natives_server.d.ts: $(CODEGEN_SCRIPTS) inp/natives_global.lua codegen_out_d
out/natives_server.js: $(CODEGEN_SCRIPTS) inp/natives_global.lua codegen_out_js.lua rpc_spec_natives.lua
./lua53 codegen.lua inp/natives_global.lua js server > $@

out/natives_server.zip: inp/natives_global.lua $(CODEGEN_SCRIPTS) codegen_out_lua.lua codegen_out_slua.lua rpc_spec_natives.lua
$(eval DIR := $(shell mktemp -d -p out/))
rm $@ || true
NATIVES_DIR="$(DIR)" ./lua53 codegen.lua inp/natives_global.lua slua server > /dev/null
sh -c 'pushd $(DIR) && ../../../../code/tools/ci/7z.exe a -mx=0 ../../$@ \*.lua && popd'
rm -r $(DIR) || true

out/natives_%.lua: inp/natives_global.lua inp/natives_rdr3.lua $(CODEGEN_SCRIPTS) codegen_out_lua.lua
./lua53 codegen.lua $< lua > $@

Expand Down
61 changes: 0 additions & 61 deletions ext/natives/generate_natives.ps1

This file was deleted.