Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Lua Integration Affecting Performance  (Read 5327 times)

0 Members and 1 Guest are viewing this topic.

BlazeTide

  • Newbie
  • *
  • Posts: 22
    • View Profile
[Solved] Lua Integration Affecting Performance
« on: October 18, 2014, 09:06:17 am »
Hello all,

So I've been trying to implement Lua with my C++ application. However, initializing a new state for Lua somehow drastically reduces the performance of the application. The frame rate of the render window drops significantly to about 10 to 20 from 60 whenever "luaL_newstate()" is called. My guess is the problem most likely has to do with the garbage collector running between very short intervals, but at this point it's only my best guess.

My code isn't too complicated either, just simple Lua integration.

main.cpp

#include <iostream>
#include <SFML\Graphics.hpp>
#include <Box2D\Box2D.h>
#include "Engine.h"

extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}

lua_State* L;

void main()
{
    L = luaL_newstate();

    sf::RenderWindow _window(sf::VideoMode(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height, 32), "Untitled", sf::Style::Fullscreen);
    _window.setFramerateLimit(60);

    b2Vec2 _gravity(0.0f, 60.0f/*9.8f*/);
    b2World *_world = new b2World(_gravity);

    Engine engine;

    while (_window.isOpen())
    {
        sf::Event sfEvent;

        while(_window.pollEvent(sfEvent))
        {
            switch(sfEvent.type)
            {
                case sf::Event::Closed:
                    _window.close();
                    break;
                case sf::Event::KeyPressed:
                    if(sfEvent.key.code==sf::Keyboard::Escape)
                        _window.close();
                    break;
                default:
                    break;
            }
        }

        engine.run(_window, _world);

        delete _world;
        _world = NULL;
    }

    lua_close(L);
    return;
}

Thanks in advance!
« Last Edit: October 20, 2014, 12:32:59 am by BlazeTide »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Lua Integration Affecting Performance
« Reply #1 on: October 18, 2014, 10:22:17 am »
And what exactly has that to do with SFML?

Performance issue? Profile!
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xornand

  • Jr. Member
  • **
  • Posts: 78
  • C++ / Python
    • View Profile
Re: Lua Integration Affecting Performance
« Reply #2 on: October 18, 2014, 11:10:46 am »
I think the reason why your framerate drops might be because you're running your Lua state on the rendering thread. Obviously, as stated above, you should profile and see what's really bottlenecking your code.

As a side note, is there a particular reason for using raw Lua in your engine? There are plenty of pretty good Lua wrappers for C++, which take care of threading Lua state automatically (and thus offloading the rendering thread).

BlazeTide

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Lua Integration Affecting Performance
« Reply #3 on: October 19, 2014, 01:54:57 am »
I use Visual Studio C++ Express, so I can't really profile without writing some of my own code. As for wrappers, which library out there do you guys prefer? I didn't intend on including any wrappers at first because technically you don't need any to integrate and run Lua with C++, but now I wouldn't mind to try a few out.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Lua Integration Affecting Performance
« Reply #4 on: October 19, 2014, 02:19:43 am »
I personally recommend none, as per: http://purplepwny.com/blog/binding_lua_to_c_think_twice_before_eating_that_glue.html and the fact that Lua C API is SUPER documented, complete, straightforward (even if a little tedious to type), more spread, explained and tested compared to most binding libraries.

If you really need a binding, then at least learn what Lua and its' C API are about, on most basic level, and then use one, so you are not confused when reading/debuging the code generated or used by your binding library or by the quirks of the language when they explode in your face and none of the tutorials and advice explaining that quirk work for you because they show examples in Lua and C API instead of in your binding of choice.
Case in point: If you ask about Lua (including jit, that I know a bit less but AM interested in) here* or on gamedev.net then you have a fairly hight chance that if I notice it I will respond if I can, just to test myself and help fellow Lua programmer. But if you ask about binding specific problem or post code that requires one then that chance goes very very low (since they don't interest me at all).

As for your original problem, yes - luaL_newstate is (a bit, compared to most other functions) costly but you should only call it once so it's OK.
If you call it once, don't use the L itself at all and it also breaks the framerate then something is wrong with your code or set up, Lua is so efficient there are entire medium sized 2D games written in it (and not even in luajit) that run at OK speed.
And why are you deleting world in your main loop? And what does the engine code do? This example is not debugable at all because it's not minimal.

Also, shameless plug, in case you need a pretty Lua terminal in-game: http://en.sfml-dev.org/forums/index.php?topic=15962.msg114040#msg114040

*you shouldn't ask pure Lua questions here, BTW, since this is (mostly) SFML forum.
« Last Edit: October 19, 2014, 02:27:31 am by FRex »
Back to C++ gamedev with SFML in May 2023

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Lua Integration Affecting Performance
« Reply #5 on: October 19, 2014, 10:50:31 am »
I use Visual Studio C++ Express, so I can't really profile without writing some of my own code.
Not true. The profiler doesn't need to be part of your IDE. There are plenty of profilers available that you can use.

A few examples:
Also check out the big List of performance analysis tools at Wikipedia.

Xornand

  • Jr. Member
  • **
  • Posts: 78
  • C++ / Python
    • View Profile
Re: Lua Integration Affecting Performance
« Reply #6 on: October 19, 2014, 11:17:51 am »
I didn't intend on including any wrappers at first because technically you don't need any to integrate and run Lua with C++, but now I wouldn't mind to try a few out.
I personally switched to luawrapper for the next version of my game. It has a simple API and according to the author, it generates clean assembly code.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lua Integration Affecting Performance
« Reply #7 on: October 19, 2014, 11:59:21 am »
I started with LuaBind, but switched to LuaBridge. It's a rather small dependency and doesn't need Boost, but still quite powerful. For a full overview of possible alternatives, see here.

And have a look at RAII, you don't need to use new and delete ;)
« Last Edit: October 19, 2014, 12:01:12 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

BlazeTide

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Lua Integration Affecting Performance
« Reply #8 on: October 19, 2014, 12:21:13 pm »
I use Visual Studio C++ Express, so I can't really profile without writing some of my own code.
Not true. The profiler doesn't need to be part of your IDE. There are plenty of profilers available that you can use.

A few examples:
Also check out the big List of performance analysis tools at Wikipedia.
So I did end up profiling the application, and the only difference in performance is a 3% increase in samples with Lua integrated. There was also less than 1% change with both the SFML graphics and SFML window. I'm going to try using a Lua wrapper later to see if I can fix the issue that way, but other than that I can't determine what's affecting the performance.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lua Integration Affecting Performance
« Reply #9 on: October 19, 2014, 12:25:18 pm »
So I did end up profiling the application, and the only difference in performance is a 3% increase in samples with Lua integrated.
3% increase compared to what?

I'm going to try using a Lua wrapper later to see if I can fix the issue that way, but other than that I can't determine what's affecting the performance.
You should find out what function calls take most of the time. Profilers should show you this.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

BlazeTide

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Lua Integration Affecting Performance
« Reply #10 on: October 20, 2014, 12:31:34 am »
So I did end up profiling the application, and the only difference in performance is a 3% increase in samples with Lua integrated.
3% increase compared to what?

I'm going to try using a Lua wrapper later to see if I can fix the issue that way, but other than that I can't determine what's affecting the performance.
You should find out what function calls take most of the time. Profilers should show you this.
I figured out the problem using a different profiler this time. It turned out it was neither Lua nor my own code affecting the performance. Apparently my solution contained a file that ran a memory leak detection program during runtime that caused the performance spikes whenever and wherever Lua was initialized. I have no idea where that program came from, but removing it from the solution solved the problem entirely.

Thank you all for the help and profiler suggestions!