News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Lua performance & optimisation tips  (Read 2734 times)

0 Members and 1 Guest are viewing this topic.

Offline RBS ID

  • Jr. Member
  • **
  • Posts: 67
  • Gender: Male
  • I don't know.
    • View Profile
    • This is website title.

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua performance & optimisation tips
« Reply #1 on: April 09, 2022, 03:11:58 AM »
That's cool.

If you are not pretty sure about your script optimization, use gcinfo() to prove it.
Code: [Select]
while true do
  TextPrintString('Memory = '..gcinfo()..' Kb', 0, 2)
  Wait(0)
end
If the memory growth is stable, then it's a good sign.

Offline SimonBestia

  • Full Member
  • ***
  • Posts: 294
  • Gender: Male
  • Bully-Board's Best Weeb
    • View Profile
    • Youtube Channel
Re: Lua performance & optimisation tips
« Reply #2 on: April 09, 2022, 04:07:25 AM »
If you are not pretty sure about your script optimization, use gcinfo() to prove it.
If the memory growth is stable, then it's a good sign.

Wish I knew this sooner!
Ironically don't really think about this stuff too much even though I primarily mod on the PS2 where that should be a concern lol.

Any way to reduce the growth as much as possible?
I noticed any time I create tables, text... (because of repeatedly running functions when selecting them from a menu) it grows slightly. Not really good for an extended play time, I guess.
I imagine it's due to the new stuff getting created on top of the old ones, or something?

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: Lua performance & optimisation tips
« Reply #3 on: April 09, 2022, 04:17:07 PM »
Wish I knew this sooner!
Ironically don't really think about this stuff too much even though I primarily mod on the PS2 where that should be a concern lol.

I originally planned it for another thread of Lua tutorial, but since Rangga made it first, why should i keep holding my breath lol.

I heard that PS2 is fragile about memory stuff, but SE and AE are same in my opinion. Optimization stuff will always be a concern to every platform you know. You don't know how much i got crash complaint for my Selector Mod -REMAKE-, that's because i had no idea about how things work and declared 260 global variables at once lmao.

Two days ago, i reinstalled my Selector Mod -REMAKE- to check its memory growth.
It grew up so fast lol. No wonder some people told me it crashes every 10 minutes.

Any way to reduce the growth as much as possible?
I noticed any time I create tables, text... (because of repeatedly running functions when selecting them from a menu) it grows slightly. Not really good for an extended play time, I guess.

Some optimization tips based on my experience:
1. Use local instead of global variable unless you need it the most.
2. Do not set local repeatedly in a loop.
Example:
Code: [Select]
local X, Y, Z
while true do
  X, Y, Z = PlayerGetPosXYZ()
  Wait(0)
end
3. Use collectgarbage() routinely to collect unused and dead objects (except global variables).
4. Avoid to execute function repeatedly unless you don't have any choice.
Example:
Code: [Select]
-- Pause game clock (most used method)
while true do
  PauseGameClock()
  Wait(0)
end

-- Pause game clock (better method)
while true do
  if not ClockIsPaused() then
    PauseGameClock()
  end
  Wait(0)
end

* CMIIW. I'd love to learn too.

Even though Lua 5.0.2 is actually run garbage collection automatically from time to time and Bully itself calls collectgarbage() whenever you exit from an area, it does not help much if you make a huge script. You need to clean your own mess with collectgarbage() because it's effective to reduce the memory growth.

I imagine it's due to the new stuff getting created on top of the old ones, or something?

I can't prove that actually. gcinfo() (or collectgarbage('count') in Lua 5.1) itself is also raising the memory growth.
But, i agree with you in common sense. It's like: you can't achieve anything without any sacrifice.