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

LuaJIT's JIT compiler is disabled by default #899

Closed
devinus opened this issue Oct 29, 2023 · 4 comments · Fixed by #900
Closed

LuaJIT's JIT compiler is disabled by default #899

devinus opened this issue Oct 29, 2023 · 4 comments · Fixed by #900
Labels
bug Something isn't working

Comments

@devinus
Copy link

devinus commented Oct 29, 2023

Ignore this if disabling the JIT compiler is the intended behavior, but the default Lua initialization does not enable the JIT compiler by default:

luaVm.open_libraries(sol::lib::base, sol::lib::string, sol::lib::io, sol::lib::math, sol::lib::package, sol::lib::os, sol::lib::table, sol::lib::bit32);

For the JIT to be enabled, the sol::lib::jit library must be opened even if the jit module is to be sandboxed away later:

luaVm.open_libraries(sol::lib::base, sol::lib::string, sol::lib::io, sol::lib::math, sol::lib::package, sol::lib::os, sol::lib::table, sol::lib::bit32, sol::lib::jit);

The LuaJIT documentation mentions this here: https://luajit.org/install.html#embed

Make sure the jit library is loaded, or the JIT compiler will not be activated.

This is also mentioned in this sol2 issue: ThePhD/sol2#61

@devinus devinus changed the title LuaJIT's compiler JIT is disabled by default LuaJIT's JIT compiler is disabled by default Oct 29, 2023
@devinus
Copy link
Author

devinus commented Oct 29, 2023

It has been pointed out to me that this may be intended: https://discord.com/channels/717692382849663036/795037494106128434/1168024641638518814

@devinus devinus closed this as completed Oct 29, 2023
@devinus
Copy link
Author

devinus commented Oct 29, 2023

It seems that there's a lot of confusion and many mod authors are assume the JIT compiler is enabled by default, so further clarity on this issue would be appreciated.

@WSSDude WSSDude reopened this Oct 29, 2023
@WSSDude WSSDude added the bug Something isn't working label Oct 29, 2023
@WSSDude
Copy link
Collaborator

WSSDude commented Oct 29, 2023

We wouldnt use LuaJIT if we didn't want to use LuaJIT :P
Newer Lua has some nice features we could have used...

So yeah, this should be fixed although I'm pretty certain we use some compile flags which should enable the optimizations even without this... Should be checked though, mainly when we potentially do not do something according to docs, may have been some oversight.

Reopened the issue.

@WSSDude
Copy link
Collaborator

WSSDude commented Oct 29, 2023

I tested with this simple init.lua (bench code was taken from the test that can be found here https://github.com/softdevteam/lua_benchmarking/blob/master/benchmarks/series/bench.lua)

local function integrate(x0, x1, nsteps, omegan, f)
  local x, dx = x0, (x1-x0)/nsteps
  local rvalue = ((x0+1)^x0 * f(omegan*x0)) / 2
  for i=3,nsteps do
    x = x + dx
    rvalue = rvalue + (x+1)^x * f(omegan*x)
  end
  return (rvalue + ((x1+1)^x1 * f(omegan*x1)) / 2) * dx
end

local function series(n)
  local sin, cos = math.sin, math.cos
  local omega = math.pi
  local t = {}

  t[1] = integrate(0, 2, 1000, 0, function() return 1 end) / 2
  t[2] = 0

  for i=2,n do
    t[2*i-1] = integrate(0, 2, 1000, omega*i, cos)
    t[2*i] = integrate(0, 2, 1000, omega*i, sin)
  end

  return t
end

function run_iter(n)
  for i=1,n do
    keep = series(1000)
  end
end

registerForEvent("onUpdate", function(deltaTime)
  run_iter(1);
end)

and I changed ScriptContext::TriggerOnUpdate to

void ScriptContext::TriggerOnUpdate(float aDeltaTime) const
{
    auto lockedState = m_sandbox.GetLockedState();

    const auto start = std::chrono::high_resolution_clock::now();

    TryLuaFunction(m_logger, m_onUpdate, aDeltaTime);

    const auto end = std::chrono::high_resolution_clock::now();

    spdlog::get("scripting")->info("Time taken for last update call: {}", std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
}

I believe the results with and without sol::lib::jit speak for themselves. Time should be in nanoseconds, hence the big numbers...

Without sol::lib::jit:

[2023-10-29 10:37:35 UTC+01:00] [16688] Time taken for last update call: 135667200
[2023-10-29 10:37:35 UTC+01:00] [16688] Time taken for last update call: 119305900
[2023-10-29 10:37:35 UTC+01:00] [16688] Time taken for last update call: 117259300
[2023-10-29 10:37:35 UTC+01:00] [16688] Time taken for last update call: 113797800
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 112941700
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 111484800
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 116755500
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 114880000
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 115790300
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 119274100
[2023-10-29 10:37:36 UTC+01:00] [16688] Time taken for last update call: 131126600
[2023-10-29 10:37:37 UTC+01:00] [16688] Time taken for last update call: 172625600
[2023-10-29 10:37:37 UTC+01:00] [16688] Time taken for last update call: 130748700
[2023-10-29 10:37:37 UTC+01:00] [16688] Time taken for last update call: 153172500
[2023-10-29 10:37:37 UTC+01:00] [16688] Time taken for last update call: 166013500
[2023-10-29 10:37:37 UTC+01:00] [16688] Time taken for last update call: 115196200
[2023-10-29 10:37:38 UTC+01:00] [16688] Time taken for last update call: 170575800
[2023-10-29 10:37:38 UTC+01:00] [16688] Time taken for last update call: 165051000
[2023-10-29 10:37:38 UTC+01:00] [16688] Time taken for last update call: 144305600
[2023-10-29 10:37:38 UTC+01:00] [16688] Time taken for last update call: 191644700

With sol::lib::jit:

[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 111870800
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 77155300
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 82757600
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 75969100
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 76182400
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 77777200
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 77629700
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 80868900
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 75866100
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 77456800
[2023-10-29 10:39:35 UTC+01:00] [17124] Time taken for last update call: 82492100
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 87406000
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 80448700
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 76169800
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 93623600
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 78461200
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 86569700
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 79947000
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 78227700
[2023-10-29 10:39:36 UTC+01:00] [17124] Time taken for last update call: 77492000
[2023-10-29 10:39:37 UTC+01:00] [17124] Time taken for last update call: 77775900
[2023-10-29 10:39:37 UTC+01:00] [17124] Time taken for last update call: 80849000
[2023-10-29 10:39:37 UTC+01:00] [17124] Time taken for last update call: 78437800
[2023-10-29 10:39:37 UTC+01:00] [17124] Time taken for last update call: 78337200
[2023-10-29 10:39:37 UTC+01:00] [17124] Time taken for last update call: 81543300
[2023-10-29 10:39:37 UTC+01:00] [17124] Time taken for last update call: 78106800

WSSDude added a commit that referenced this issue Oct 29, 2023
Add `sol::lib::jit` to actually enable JIT ( resolves #899 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants