1
\$\begingroup\$

I'm making an engine in C++ with Lua for its scripting language (using LuaBridge as the binding). I have a lot of entities and those entities have components. One of the components on those entities is a script component. Every time I want to call a different function from a Lua script, I always have to call luaL_dofile. If I call this with more than 20 scripts, my game takes too much time per frame reducing it to 5 FPS on my machine. Every script has an Init, FixedUpdate, Update, and Shutdown function. For every script, I call all of these functions in order. Is there a way to reduce the latency introduced in each frame from using Lua?

Some pseudo code of my usage of LuaBridge:

    // In Update function:
    for (int i = 0; i < scripts.size(); i++)
    {
        luabridge::LuaRef func = luabridge::getGlobal(L, "Update");
        if(func.isFunction())
          func(dt);
    }
\$\endgroup\$
2
\$\begingroup\$

I think part of the problem (which also caused me to misinterpret the question at first) is the lual_dofile() calls.

This sounds like all scripts are always loaded in the same global state, overwriting each other, so loading/parsing causes a significant overhead.

Instead, I'd suggest you use lua_newthread() to create a separated context once per script file loaded.

This allows you to load all your script files once and then simply switch between them before calling update() or any other Lua function, without having to reload/recompile anything.

\$\endgroup\$
  • \$\begingroup\$ I think this is a great answer to get some quick performance gains from fixing simple API misuse of LuaBridge, but I'd maintain the idea that his usage of Lua seems too liberal/unnecessary for his use case. \$\endgroup\$ – user5665 Jul 18 '16 at 6:52
0
\$\begingroup\$

Reduce and Simplify Lua Usage

Think long and hard about every instance where you use Lua over C++, understand why the flexibility of an interpreted script is more valuable than writing C++, why waste a precious context switch from C++ to Lua if you don't actually gain anything for it?

  • Is it easier to tweak it in a Lua script than in C++, is your recompilation time for your engine/game really a bottleneck in iteration?
  • Does the code actually change frequently, or is it something that you may only write once and revise sparingly?
  • Is this a critical component that will get called many times per entity in your game loop, and in turn, have a performance hit? If so, it's probably better suited for C++ just to avoid running in a VM and having to eat the context switching costs.
  • Is this something that you want users/modders to be able to tweak themselves? If so, Lua may be a fitting candidate, if the performance hit is worth it.
  • Are you implementing scripting too early in your project? What if you are wasting your time developing the scripting system or scripts themselves? Scripting things makes them more flexible to be loaded and configured without program recompilation, but it comes with a heavy price. You must spend time working on your script integration, you must spend time debugging your scripts without your native debugger, you must spend extra time working on optimizations trying to make up for the cost of using a scripting language.

I think you might be happier replacing your entire Lua system and stockpile of scripts with C++ until you actually think you fit the criterion of using a scripting language over native C++. Using 4 functions in 20 some scripts per entity in your game loop does not seem remotely necessary unless you really need every component to have the flexibility of Lua at every step (I personally think that is never necessary).

However, if you still consider Lua necessary, I do have the following tips:

  • Reduce Context Switches between Lua and C++: Don't call a Lua function from C++ that loops through a list of object and calls C++ functions that call Lua functions. That back and forth between C++ and Lua is difficult for the CPU to handle.
  • Precompile Lua scripts, Cache references to functions and Lua files so Lua doesn't waste time finding Functions and reinterpreting files.
\$\endgroup\$
  • \$\begingroup\$ sorry for sending a comment after too much time, but i get back here in your answer to read it again, but how exactly I can make cache references to functions without having to reinterpret files again? (and btw your answer helped me alot, i just dont know about this part) \$\endgroup\$ – khofez Aug 3 '16 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.