Upcoming change to (correctly) limit the local count to 200

It should no longer work. There is no reason to even be at half of the limit imo. I do not know if Luau has an increased limit on upvalues or if it is still 60.

2 Likes

That still wouldn’t allow you exceed the 200 local limit even if you were redefining the variables within the do block’s scope. The only way to get past the limit is to create another function and define locals within the scope of the newly created function. I think you’re confused with how do blocks work.

Edit: Even though tables and functions are the only way to get past the limit, the only reason you’d need 200 locals in the first place is if you were doing aggressive optimizations. In which case, it would make your code even slower because of the function calls/table indexes.

2 Likes

unreal engine gives you 7 packs of chicken legs

10 Likes

Does the local variable inside the function get cleared out after using the function? Or will I have to create a new scope inside the function when declaring the variable?

1 Like

When you create a function the block inside of it is considered a new scope as @zeuxcgsaid said in his post above.

2 Likes

Technically the limit is for 200 simultaneously live locals in a function, so do blocks could help in that you could do smth like

do
-- ... 100 lines worth of locals
end
do
-- ... 100 lines worth of locals
end
do
-- ... 100 lines worth of locals
end
do
-- ... 100 lines worth of locals
end
do
-- ... 100 lines worth of locals
end
do
-- ... 100 lines worth of locals
end
do
-- ... 100 lines worth of locals
end

Although, uh, maybe at this point making a function or 10 isn’t such a bad idea :smiley:

14 Likes

My replies were directed towards their examples since I was confused with how they were presenting their points, but yes, you could do it using your example. Other than that, I appreciate the announcement post. I had a chunk of code that was a few locals away from hitting the 200 limit, so it conveniently gave me a heads up. :+1:

1 Like

200 locals in one script. They wouldnt give a limit of 200 locals per game. That would be way too limiting.

1 Like

This is incorrect. It is 200 locals per scope/function, not script.

8 Likes

Oh it is? Sorry for the misunderstanding then.

2 Likes

Cool, the local limit is going to be enforced. Guess I should go check my code to make sure it doesn’t go over this limit.

1 Like

I’m not sure how Lua activation records are stored in memory, but I’m pretty sure there’s an important point to be made about nested-linkage to local variables. Wouldn’t a nested function with references to 200 other locals in a higher scope still need to maintain memory addresses for the 200 already filled temporary registers?

Like if you have some code like:

function topLevelScopeFunction()
    local a = 1
    local b = 2
    -- etc. (198 more)
    function nestedScopeFunction()
        local sum = a + b -- + 198 more
    end
end

When sum is declared, wouldn’t it be a compiler error? Or do temporary registers empty when scope changes?

2 Likes

Temporary registers are freed back up to be used right after they are. Although I’m not exactly sure what you mean. Functions each have their own section of the stack, though, so they shouldn’t really interfere with each other.

3 Likes

The references to locals in the higher scope are called “upvalues”; their limit is 200 as well (up from 60 in Lua 5.1), but it’s a separate limit, so your example should compile.

4 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.