Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

What is the maximum lua can take
Started by JoePringles Jun 21 2018 10:46 AM

- - - - -
30 replies to this topic

Best Answer roaminggamer , 23 June 2018 - 08:23 AM

**UPDATE**  This post has been updated to fix some errors that @davebollinger corrected/pointed out.  

Thanks Dave!

 

@Beluga,

 

This is essentially how it works:

-- This is a module file as an example
--
local m = {}
local bob = 1
local bill = 2

-- At this point, and in this scope there are three(3) locals
-- m, bob, bill

function m.doit1()
   -- Zero (0) locals
end


function m.doit2() 
   print( bob )
   --  Zero (0) locals
   --  One upvalue: bob
end


function m.doit3( arg1, arg2, arg3, arg4 )
   -- Four(4) locals: arg1, arg2, arg3, arg4
end


function m.doit4( params )
   -- One (1) local: params  
   -- This is true, event if doit4() is later called like this:
   -- ???.doit3( { arg1 = "a", arg2 = "b", arg3 = "c", arg4 = "d" } )
end


function m:doit5( params ) -- notice use of colon notation
   local sue = bill
   print( bob )
   -- Three (3) locals: this, params, sue
   -- Two (2) upvalues: bill, bob
end

return m

In the code above, you have these chunks:

  • The entire file
  • doit1()  .. doit5()

 

Yes, chunks can contain sub-chunks.

 

https://www.lua.org/pil/1.1.html

 

Tip: Take some time to read the these chapters of the PIL:

  • 1..8
  • 11, 12
  • 18 .. 23

It will help you a lot with understanding Lua and writing better code.

[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 358 posts
  • Corona SDK

Soooooooooo here is the shtick:

 

A while back I learned that functions can only take upto 60 variables.

This affected me a bit but its all fine now.

 

But going forward... my app files are going to get bigger and bigger.

 

That being said, how much can a LUA file take?

Like for example: how many lines of code can a lua file take or how many variable can it contain?

 

Sort of just popped up in my mind...

 

thanks!



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,814 posts
  • Corona SDK

Simple solution is put variables in tables.

One table = one variable, no matter how many values or sub-tables contained within.

As for lines of code, I wouldn't worry about line number limits, and would encourage you to split your code into multiple small files each doing different jobs.

[TOPIC: post.html]
#3

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

The max 60 upvalues limit is per function so it's not really an issue even if you dont use tables.



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,588 posts
  • Corona SDK

@Beluga,
 
Don't do this:
local function poorWay( arg1, arg2, arg3, ..., argN )
end
Do it this way:
local function betterWay( stdArg1, stdArg2, params  )
   params = params or {}
   local someArg1 = params.someArg1 or "some default"
   local someArg2 = params.someArg2 or "some default"
   ...

end
Later called like this:
betterWay( 10, 20, { someArg1 = "Bob", someArg2 = "Bill", coins = 15 } )
You will notice, this is the way most SSK builders work:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/#the-ssk-way

Edited by roaminggamer, 21 June 2018 - 02:25 PM.


[TOPIC: post.html]
#5

Develephant

[GLOBAL: userInfoPane.html]
Develephant
  • Corona Geek

  • 1,450 posts
  • Corona SDK

Hi,

 

Use the module pattern and abstract your code functionality where appropriate. And as @nick_sherman pointed out, utilize tables (which is pretty much what modules are anyway).

 

-dev



[TOPIC: post.html]
#6

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,111 posts
  • Corona SDK

Even using tables I struggle to get why any function needs that many inputs?

 

I max at around 5 params per function call.



[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,935 posts
  • Enterprise

The other limit is 200 local variables in any one chunk, but again, tables can help you get passed this too.

 

Rob



[TOPIC: post.html]
#8

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 358 posts
  • Corona SDK

The other limit is 200 local variables in any one chunk, but again, tables can help you get passed this too.
 
Rob


Sorry but what do you mean “chunk”?

[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,935 posts
  • Enterprise

Lua uses the term "chunk" to mean any one block of code.  A for loop for instance is a chunk, and If-then-end block is a chunk. But more importantly the top level of a module is it's self a chunk.  Since you're very likely not going to have 200 local variables inside an if-then-end block it's only really a problem at the whole module (or main.lua) level.

 

Rob



[TOPIC: post.html]
#10

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • Contributor

  • 976 posts
  • Corona SDK

Rob probably means per single .lua file



[TOPIC: post.html]
#11

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,111 posts
  • Corona SDK

if you are hitting lua max issues then the issue is yours not lua and it is time to refactor your code.

 

the only exception to this rule is the amount of functions per DEX - which is usually eaten up by plugins - especially ad networks.



[TOPIC: post.html]
#12

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 358 posts
  • Corona SDK

would using parameters instead of local variables affect the chunk?

 

ex.

local personpic ------->  person.pic



[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,588 posts
  • Corona SDK

  Best Answer

**UPDATE**  This post has been updated to fix some errors that @davebollinger corrected/pointed out.  

Thanks Dave!

 

@Beluga,

 

This is essentially how it works:

-- This is a module file as an example
--
local m = {}
local bob = 1
local bill = 2

-- At this point, and in this scope there are three(3) locals
-- m, bob, bill

function m.doit1()
   -- Zero (0) locals
end


function m.doit2() 
   print( bob )
   --  Zero (0) locals
   --  One upvalue: bob
end


function m.doit3( arg1, arg2, arg3, arg4 )
   -- Four(4) locals: arg1, arg2, arg3, arg4
end


function m.doit4( params )
   -- One (1) local: params  
   -- This is true, event if doit4() is later called like this:
   -- ???.doit3( { arg1 = "a", arg2 = "b", arg3 = "c", arg4 = "d" } )
end


function m:doit5( params ) -- notice use of colon notation
   local sue = bill
   print( bob )
   -- Three (3) locals: this, params, sue
   -- Two (2) upvalues: bill, bob
end

return m

In the code above, you have these chunks:

  • The entire file
  • doit1()  .. doit5()

 

Yes, chunks can contain sub-chunks.

 

https://www.lua.org/pil/1.1.html

 

Tip: Take some time to read the these chapters of the PIL:

  • 1..8
  • 11, 12
  • 18 .. 23

It will help you a lot with understanding Lua and writing better code.


Edited by roaminggamer, 23 June 2018 - 05:23 PM.


[TOPIC: post.html]
#14

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 814 posts
  • Corona SDK

Basically, a chunk is a block that has been successfully compiled and can be called. The formal details can be found here in the 5.1 docs.

 

So a block like

local x = 4

print(x + 7)

gets run through loadstring(), or as a file through loadfile() or require(), and basically becomes

local function Chunk (...)
  local x = 4

  print(x + 7)
end

With require() and main.lua you never actually see this, since the require() machinery and Corona, respectively, call the function behind the scenes. But with the other two you can dig into it. For instance:

local chunk = loadstring[[
  local a, b = ... -- gets 7, "dog"

  function WhatAnimal ()
    return b
  end

  return a + 3
]]

if chunk then
  local how_many = chunk(7, "dog")

  print(how_many, WhatAnimal())
end

Some of the limits such as maximum number of variables are defined in luaconf.h in the Lua source. (If you dig around in the Native directory a bit, you'll find Corona's slightly modified version; I don't think it's touched those particular values, though.) As far as the "must be less than 250" comments, while I can't say for sure, I imagine that keeps indices to them in 8 bits while reserving a few special values. I do know Lua's authors are very concerned about keeping data structures tight.

 

For anybody curious about Lua's code, the author of LuaJIT has a recommended reading order.



[TOPIC: post.html]
#15

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 358 posts
  • Corona SDK

Ah thanks for all that information for chunks.

 

As chunks have a maximum of 250 local variables, would using paramaters help?

 

ex.

local personpic ------->  person.pic



[TOPIC: post.html]
#16

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,588 posts
  • Corona SDK

1. Why do you keep asking the same question?

 

2. What does it mean?  That line of code is NOT a parameter table.  Is a local variable, which adds to your local count.

 

Please go back and more carefully read my long and detailed reply showing you how many locals are in scope at various times.

 

3. I would stick to 200 or fewer locals actually.  I think that is safer.

 

 

I feel like theses questions have been answered:

  • Maximum allowed arguments passed as parameters to a function?  <= 60
  • (Safe) Maximum allowed locals in any specific scope?  <= 200
  • How does passing a table of named parameters affect the local count?  See m.doit3() in my clearly coded example.

 

If you still have a question related to local counts, passing named parameter tables, etc.

 

 

Please ask it more clearly and concisely, and don't just re-ask the same question.


Edited by roaminggamer, 23 June 2018 - 01:35 PM.


[TOPIC: post.html]
#17

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 358 posts
  • Corona SDK

Right well nobody answered my question earlier.

 

I understand the chunks part but all I'm asking whether parameters add up the number of local variables used.



[TOPIC: post.html]
#18

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,111 posts
  • Corona SDK

By parameter I assume you mean a value passed to a function?  

 

Limits are there not to get in your way, but to make you code properly.

 

So instead of

local a = 1
local b = 2
local c = 3

you have

local values = {1,2,3}

The first uses 3 "slots" the second only one.

 

Like I said earlier, if you are hitting hard limits you need to refactor out to smaller modules.



[TOPIC: post.html]
#19

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,349 posts
  • Enterprise

every time you reference something in a more-outer scope it will count against the 60 limit, fe

local a,b,c,d
local foo = function() return 7 end
local bar = function()
  a = b + c -- 3 here
  d = foo() -- 2 more
end -- 5 upvalues total


[TOPIC: post.html]
#20

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 814 posts
  • Corona SDK

@BelugaWhale It looks like they must: parlist

 

When a parameter name is parsed it does that new_localvar() call, which is what's enforcing the limit. (And the count is indeed 8 bits. Also looks like ... is one of the "special values".)

 

But to echo the others, if you get to this point, time to clean things up a bit.  :)



[TOPIC: post.html]
#21

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,349 posts
  • Enterprise

the 200 limit is for "locals" - local to a module, local to a function, local to a block, .. whatever.

the 60 limit is upvalues per closure (ie "# of references to outer-scopes")

both of these limits are far more than enough for well-structured code.



[TOPIC: post.html]
#22

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,349 posts
  • Enterprise

local a0,a1,a2,a3,a4,a5,a6,a7,a8,a9
local b0,b1,b2,b3,b4,b5,b6,b7,b8,b9
local c0,c1,c2,c3,c4,c5,c6,c7,c8,c9
local d0,d1,d2,d3,d4,d5,d6,d7,d8,d9
local e0,e1,e2,e3,e4,e5,e6,e7,e8,e9
local f0,f1,f2,f3,f4,f5,f6,f7,f8,f9
local g0
-- keep going til 200 of these to break the local limit

function foobar()
  a0 = a1 or a2 or a3 or a4 or a5 or a6 or a7 or a8 or a9
  b0 = b1 or b2 or b3 or b4 or b5 or b6 or b7 or b8 or b9
  c0 = c1 or c2 or c3 or c4 or c5 or c6 or c7 or c8 or c9
  d0 = d1 or d2 or d3 or d4 or d5 or d6 or d7 or d8 or d9
  e0 = e1 or e2 or e3 or e4 or e5 or e6 or e7 or e8 or e9
  f0 = f1 or f2 or f3 or f4 or f5 or f6 or f7 or f8 or f9
  -- just one more will break the upvalue limit:
  g0 = 0
end



[TOPIC: post.html]
#23

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 358 posts
  • Corona SDK

Thanks to everyone!

 

I finally get it.

 

I would like each of these posts but I have a limit.

 

As said by roaming gamer i will read the guides.

 

Thanks a bunch!



[TOPIC: post.html]
#24

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,111 posts
  • Corona SDK

Prior to this post I was unaware of these limits so I've learned something tonight.

 

Now considering I have large and complicated games... maybe I do actually know a thing or two about coding.... who knew?

 

Note: this is a light-hearted comment



[TOPIC: post.html]
#25

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,349 posts
  • Enterprise

function m.doit1() 
   -- 2.  At this point, and in this scope there still ONLY three (3) locals
   -- m, bob, bill
end

"in this scope" (ie, the scope of function m.doit1) there are no locals (nor upvalues)

at this point, at module scope, there are still only three locals yet defined




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]