Performance questions?

Hey there, Recently some performance questions have been floating on my head all the time especially when i start off a serious project. So i would like to share it with you and hopefully you have the answers that i’m looking for.

First of all, what’s more efficient when you have multiple parts or things that share the same behavior. in both cases where you have just ones or tens of objects or hunbreds to thousands of objects. Is it better to handle all of those using a loop in one script or duplicating 1 script into all of them regardless that the last method isn’t reliable?

Secondly, are all roblox objects designed using C or some of them uses Lua, or all of them are designed in lua just with some Lua-C bridges. And how that affects performance overall?
Additionaly, Does it worth it storing your variables using roblox values objects or just using modules tables. and if so what’s the difference between them in term of performance, ressources and speed?

Thirdly, are regular arrays more performant than dictionaries and metatables driven tables. and also does Lua cares about data localization and cpu caching and if so, can we control it like other low level languages like C++.

Firthly, in OOP, when i create a class, should i give an update function in the API and let the user update the object as needed, or should i update it myself. and of course we’re talking about the same questions performance.

Finally i hope you give me clear logical answers on my questions and also share with us the things that you think most of developers today do wrong in terms of performance and what’s your best day to day programming or scripting practices especially Lua.

4 Likes

A. Having one Script managing a multitude of parts/mechanisms in your game is better performance-wise and is easier to maintain than having to clone the same script into each object. Bonus points for using a ModuleScript as an object wrapper.

B. I know that a majority of the exposed Roblox API in Lua is a bridge to C, but I can’t answer exactly what parts are and aren’t. Storing your variables in code is way more performant than having them in their respective XValue (no need for a reference to the value in your code).

C. I am not able to answer this one :stuck_out_tongue:

D. This depends on your usage case. If it’s a class to wrap a Gui object, I would connect the update method to a ViewportSize property change signal, as well as calling it whenever I update the Gui itself.

Hopefully this helps, but take everything I said above with a grain of salt :slight_smile:

5 Likes

Kind of Tl;dr, so I will just answer what @wow13524 didn’t..

Thirdly, are regular arrays more performant than dictionaries and metatables driven tables. and also does Lua cares about data localization and cpu cashing and if so, can we control it like other low level languages like C++.

I believe when it comes to any sort of Array, it’s all the use case and how you structure it. Obviously, if you have an array of 200,000 different instances, you probably aren’t going to want to search for it by going from

index 0 to 200,000.

There’s definitely better ways of sorting through an array.

The second part of the question is a little obscure for me, so I’m just going to reply with what I think you are getting at. Every instance / array / whatever in roblox is assigned in memory. If you print({“Hello!”}) you will get something along the lines of Table: ‘pointer to assigned memory here’.

I guess in some essence, yes, you can control the memory usage.

Edit:

lua tables start at 1 not 0.. which always bugs me

2 Likes

Well that sounds reasonable. But what about cpu caching, since all of the modern cpu’s uses heavly cpu caching. well cpu caching simply means that when a program requires a piece of data from memory, instead of just passing that data, it passes also the data that are next to it in the physical memory. Now the question that can arrise when you locate an array or variable in lua, does it accounts for caching by placing related variables and data next to each other or does it just randomly allocates memory.
For instance in C++ if you use a regular static array, all the data are stored next to each other in memory so when you require the first one for example. all the next data that can be stored in the cpu cache will get loaded into it. and so when you request access to those data, it’s already there in the cpu cache, so it will be faster and better to read those data!
Whereas C++ vectors elements are scattered around the memory that links to the next element in that vector arrays. which makes vectors good when as you said for searching an element and indexing it easily but not the best when it comes to looping through objects since it usually breaks the process of caching

I’d like to believe that Lua (and Roblox’s Luau) does not use caching, only because tables and dictionaries are dynamically sized and cannot have a declared starting size.

1 Like

Well that’s logical. 30 characters lol

I believe I understand what you are getting at now. I’m not exactly sure how ROBLOX goes about get/request of memory… however… Lua is a successor of essentially C… (ANSI C standard)
With that being said, Lua programming is imperative. Which means it’s more of a list of what the computer needs. A polar opposite would be declarative programming which focuses on what the program should achieve and not specifically how it should do it. So I’m assuming when it comes to memory, commonly accessed memory is more readily available than less-commonly accessed memory?

I hope that answered your question, my brain just took a flashback to a couple lectures I probably shouldn’t have snooz’d through

1 Like

Well i suggest reading this article if you have free time. it’s really helpful and opened my mind into a lot of questions especially about data structures.

https://gameprogrammingpatterns.com/data-locality.html

2 Likes