Optimization entry in wiki

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
scoobyff666
Prole
Posts: 11
Joined: Fri Sep 16, 2016 5:21 am

Optimization entry in wiki

Post by scoobyff666 » Sat Oct 17, 2020 10:40 pm

There's this page (https://love2d.org/wiki/Optimising), that is not linked from other places in the wiki as far as I can tell, but that I somehow found searching on the internet.

There are some serious problems in this page that make me think it should be deleted or updated and linked from somewhere else in the wiki. One problem is that the code in Use Locals section is a kind of optimization that should not be used with luajit, which love uses, but only in lua (this page has some info on that http://wiki.luajit.org/Numerical-Comput ... ance-Guide). Other problem is the Further Tips link, that seems to be old and not point to where it should.

After seeing this, I thought about looking into how to update it myself, but decided to ask here instead what other people think about is, specially being about a page that is not even linked anywhere as I said.

User avatar
scoobyff666
Prole
Posts: 11
Joined: Fri Sep 16, 2016 5:21 am

Re: Optimization entry in wiki

Post by scoobyff666 » Sun Oct 18, 2020 4:54 am

One thing I found: for the case mentioned in the linked wiki page, about putting math.sin in a local before calling it in a loop, in love (luajit), it would make no effect at all, runs at same speed as using math.sin. But doing the same to love.graphics.*, or love methods in general, actually can optimize code seriously. So this optimization technique still applies in some cases. I don't know exactly what are the circumstances for it working or not, but I made some tests here https://gitlab.com/eduardomezencio/lua- ... ance-tests to get some info.
In the lua-luajit folder of this repo there are tests to measure speed using the lua interpreter against luajit, with a shell script to run the tests (developed with linux in mind). In the love folder, there's a main.lua with 2 test cases. You can run them with 'love . 1' and 'love . 2'. The first case test putting love.graphics.getWidth and love.graphics.getHeight in local variables. The second case tests math.sin. The results are quite significative:

Code: Select all

(time in seconds, less is faster)
---
local functions: 2.6503622910004  # calls getWidth
local graphics: 3.2965999469998  # calls graphics.getWidth
nothing local: 5.5875455580008  # calls love.graphics.getWidth
---
sin local: 3.8322298900002  # calls sin
nothing local: 3.6843466340015  # calls math.sin

User avatar
ReFreezed
Party member
Posts: 160
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Optimization entry in wiki

Post by ReFreezed » Mon Oct 19, 2020 12:41 am

I don't think that page has any serious problems. Using locals is generally faster than having table lookups.

I did a little testing too and my results regarding math.sin is similar to yours, i.e. it doesn't seem to matter if 'sin' is local or not - it's basically the same speed. It might have something to do with math.sin being a math function specifically, or that it's a "built-in" function in Lua. But for other cases locals are faster (and I'm pretty sure most loops don't involve any of Lua's built-in functions, thus that advice about locals is good).
Tools: Hot Particles, LuaPreprocess
Games: Momento Temporis: Light from the Deep, Momento Temporis: Arena, Energize!
"If each mistake being made is a new one, then progress is being made."

User avatar
scoobyff666
Prole
Posts: 11
Joined: Fri Sep 16, 2016 5:21 am

Re: Optimization entry in wiki

Post by scoobyff666 » Mon Oct 19, 2020 4:10 am

Yes, I kind of changed my mind after making my tests. Right now I only think the example itself is bad, because if the reader decides to try that exact code by himself and measure, he will end up with results that contradict what's being said, since luajit will optimize it. This and the broken link.

User avatar
pgimeno
Party member
Posts: 2310
Joined: Sun Oct 18, 2015 2:58 pm

Re: Optimization entry in wiki

Post by pgimeno » Mon Oct 19, 2020 11:11 pm

Try adding jit.off() before trying the example.

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests