• Drew@sopuli.xyz
    link
    fedilink
    arrow-up
    9
    ·
    3 days ago

    I don’t think luajit compiles lua to C and then compiles the C, that would be pretty silly. JIT compilers directly output machine code.

    And as long as you can do something in a higher level language there’s no need to involve manual memory management or tracking ownership (rust)

    • sp3ctr4l@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      3 days ago

      Ah, I used ‘compiled to C’ as a clumsy phrase, that was incorrect to say, lol.

      Whats actually going on… its a bit complicated.

      On the one hand, you can directly write C code and call C functions in a Lua script, basically encapsulated inside the Foreign Function Call Interface… and that would mean you are writing Lua code, that has C code in it… thus arguably, you are passing Lua to C, runtime compiling that C, and then running that compiled bit of C code is running as machine code, but the rest of the Lua code is running as Lua, which is compiled into machine code.

      On the other hand, LuaJIT basically does a series of analysis passes on your Lua code, and if it finds functions or chains of functions that are called very frequently, or functions that are computationally costly, slow, it will compile those bits directly into machine code… but the actual code that directs that process is itself written in C.

      The point I am trying to get at here is that sometimes it makes more sense to write code that needs to be more highly performant via the use of a more performant, compiled language…

      LuaJIT handles that by just doing it for you ‘automagically’…

      But you may be able to get even better runtime performance if you rearchitect a bit by taking all that stuff that LuaJIT is highlighting for optimization, and just actually permanently writing that stuff in C, C++ or Rust.

      Or, maybe LuaJIT is ‘good enough’, and makes for less workload, simplifies development.

      I threw Rust in there because it seems to generally be almost, but not quite as fast as C++ / C in runtime… but is also much less daunting and confusing for a less experienced coder to learn how to use.

      Lua is extremely human readable, C and C++, a good deal more complicated… Rust is more human readable and easier to learn than C/C++, and also essentially has training wheels on for memory management problems that would be extremely frustrating for someone like a novice game coder that is trying to transition from say Lua or Python, into a lower level, compiled language.