Does LuaJit compile functions called inside of a corountine?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Does LuaJit compile functions called inside of a corountine?

Post by dezoitodemaio »

Or they are just completely ignored as wall as the coroutine?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Does LuaJit compile functions called inside of a corountine?

Post by pgimeno »

LuaJIT is a tracing JIT compiler which compiles loops. When it encounters an instruction that it can't compile, like a coroutine function, the trace is aborted. That goes for LuaJIT 2.0; version 2.1 can do "trace stitching", which consists of resuming traces after a NYI instruction is executed. I don't know if the trace can propagate through coroutine switches, but I wouldn't count on it.

Coroutines are not treated specially, but coroutine switches may be a different beast. Anyway, if the code inside the coroutine contains a compilable loop, that loop will run in compiled mode. When it encounters a NYI instruction, like a call to coroutine.yield(), that part won't be compiled. If the yield is in a loop, that loop won't be compiled unless the yield is very rarely executed.

For example, the loops in this coroutine run in compiled mode (try the difference with jit.on() and jit.off() to see):

Code: Select all

jit.on()
--jit.off()

local function sumPrimes(n)
  local P = 3
  local isFactor = {}
  for i = 2, n do
    if not isFactor[i] then
      for j = i*i, n, i do
        isFactor[j] = true
      end
    end
  end

  local sum = 0
  for i = 2, n do
    if not isFactor[i] then
      sum = sum + i
    end
  end
  coroutine.yield(sum)
end

local co = coroutine.create(sumPrimes)
local a = os.clock()
local success, result = assert(coroutine.resume(co, 2713131))
print(result)
print(os.clock()-a)
That's because the loops don't have anything that breaks the traces.

(I know it's a stupid use of a coroutine, when a normal function call would be much simpler, but it's just for illustrative purposes)
dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Re: Does LuaJit compile functions called inside of a corountine?

Post by dezoitodemaio »

I thought LuaJIT was a method compiler, my bad.

So the code that is inside the callback passed to coroutine.create will be compiled normally as long as i dont call yield, resume, etc in there?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Does LuaJit compile functions called inside of a corountine?

Post by pgimeno »

There are actually many more functions that will stop a trace, not just coroutine stuff. In LuaJIT 2.0, that includes calls to Löve functions. See the list of NYI instructions: http://wiki.luajit.org/NYI
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 45 guests