[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: 10 LOC program is too complex to be compiled
- From: Coda Highland <chighland@...>
- Date: Mon, 27 Apr 2015 05:42:42 -0700
On Mon, Apr 27, 2015 at 5:18 AM, Egor Skriptunoff
<egor.skriptunoff@gmail.com> wrote:
> Hi!
>
> I have defined 150 local variables and created an array,
> in which 50-th element is a nested array of length 50.
> ---------------------------------------------------------
> local a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
> local a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
> local a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
> local a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
> local a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
> local a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a
> a = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
> 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2}}
> ---------------------------------------------------------
> Both Lua 5.2 and Lua 5.3 have failed to compile it:
> test.lua:10: function or expression too complex near '2'
>
> Why such short code is considered to be "too complex"?
> I'd like to write much more complex programs than this one.
>
> --Egor
If I had to take a wild guess it's the fact that you've defined 150
local variables. :P Just because they all have the same name and
therefore only one of them is accessible at all doesn't change the
fact that you've just plunked 150 entries onto the stack. And then
you've plunked a few more entries onto the stack (just for the
duration of the expression) during the assembly of the table. The
compiler observes that it's reserving too many stack slots and says
"nope, you might crash the interpreter at run time if I let you
execute this, stop that!"
If you REALLY MUST define that many locals, consider seeing if there's
a #define somewhere in the Lua source that will let you enlarge the
maximum size of a Lua stack frame.
But more than likely, having that many locals indicates you're doing
something squirrelly and poorly-structured, or you're
machine-generating code. And if you STILL insist on using so many
distinct variables, consider the following ways of addressing your
problem:
(1) Use a table instead of locals. Yes, there's a bit of a performance
hit, but there's no stack-based limits on table size, only the limits
of RAM.
(2) Break up your code into multiple functions. Each function will use
less stack space than the monolithic one, so your limit changes from
"how many stack slots can I reserve in one function" to "how many
stack slots are available to the entire interpreter".
(3) If the control flow is such that you can't break it up into
multiple functions, try using "do"/"end" blocks to constrain some of
the local variables to the smallest scope possible. You're still
subject to the maximum number of stack slots in a single function, so
e.g. wrapping the last couple lines in your example in "do"/"end"
doesn't help because there is a time when the full number of slots is
needed, but e.g. wrapping a couple lines in the middle in such a block
resolves it because it frees up some slots before allocating more.
/s/ Adam