cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
It would make it slightly slower because loops (and functions) have slight overhead. If you have to go deep enough, you'll run out of local variable space (you can have a maximum of 255 (well 250 to be accurate)) |
|
|
Is this just for RBX.Lua or just the general Lua?
If I remember correctly, about the _G, ServerScripts and LocalScripts do not have the same _G content. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
maximum of 250 per function* (forgot that part) |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
A lot of this applies to regular Lua.
Only #5 and #7 are Roblox-specific |
|
|
Is there any downside to doing that though? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Well it's harder to read IMO and (probably insignificantly) slower to nest loops if you can use a single loop. But if it works for you and it looks good to you, you might as well keep doing it. |
|
|
I have seen several people who think this, so I will mention it: ModuleScripts are more efficient.
The above is not true. It actually takes more memory and stuff because you have to call require(), pass variables, etc.
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played. |
|
|
No, I'm talking about declaring global functions as local in the beginning of the script. Like doing this:
--Line 1
local table_insert = table.insert
--Somewhere past line 1000
...
...
...
...
table_insert(Table, Something)
...
...
...
... |
|
|
@dennis96411
But, why would you do that? Isn't table.insert is easier to type rather than table_insert ? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Ohh, okay in which case that's where upvalues come into play.
Nesting blocks (not including functions) will not change anything but if you were to do:
local x = table.insert
local function blah()
-- use "x" here
end
It would be slightly slower to access x because it's in the caller's callstack, not in blah's, but it's still much faster to access than a global variable. |
|
|
So if I'm understanding this correctly, when you index a variable in the environment, it will try to find it in the local stack, then keep going up through each caller's stack until it reaches the global? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Not really. The compiler actually sorts this mess out.
When you have something like this:
local x = 5;
do
local y = 10;
end
local z = 15;
The compiler will spit out something like this:
LOADK 0, // x
LOADK 1, // y
LOADK 1,
The first register will be reused because the compiler has determined that it was in a block of its own. And this code will generate the _same_ thing:
local x = 5;
local y = 10;
y = 15;
However if the compiler sees that there are no local variables, it will just go straight to global. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
"block of its own" as in, after that point it won't ever be used again (since well, that's the point of local variables) so it reuses the register** |
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Anyways, to quickly answer your question since I don't think I actually did:
local x = blah;
do
do
do
do
-- x stuff
end
end
end
end
Accessing x up there is no difference than accessing x below:
local x = blah
-- x stuff
Unless x is outside a function (which in this case there are none). |
|
|
If those were functions, then how would it be different? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
They would be upvalues to that function, as an example:
local x = blah
local function abc()
local function def()
-- do stuff with x
end
end
"x" does not exist in those functions' stack by default, but they exist outside of it (locals outside a function are upvalues to that function). So instead of "getting a local" it would have to "get an upvalue" (which is still really fast, much faster than getting a global). |
|
|
So if I'm doing a lot of long operations with those global functions, given that I have enough space on that stack, I should declare them as local? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Well if speed is super vital, since accessing a global requires a Lua table lookup whereas accessing a local requires just a simple C-sided array lookup. |
|
|
cnt, are you in college or do you like program computers for a living?
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
I'm in high school and I have no life |
|
|
You should make a game on here.
|
|
|
I wish I had your life, lol. Your so smart.
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played. |
|
DrHaximusJoin Date: 2011-11-22 Post Count: 8410 |
lmao ^
what a nerd |
|
|
^ Actully do wish I knew what he knew. Knowledge = money and money = power
|
|