5

I am trying to "Skip" a variable, by either never declaring it or just having it garbage collected immediately, but I don't know if it's possible.

Example:

function TestFunc()
   return 1, 2
end

function SecondFunction()
   local nodeclare, var = TestFunc()
end

Basically what I wanted was for "nodeclare" to not even exist. So if I did print(nodeclare, var) it would do nil, 2. The same thing would be if I was doing a pairs loop and I didn't need to use the keyvalue. Is there some special thing I can put as the variable name for this to happen? If say I was doing a pairs loop over 100 values, would that even have a signifigant impact?

2 Answers2

6

First of all, variables are not garbage collected, objects are. In this case, there's nothing to garbage collect.

However, let's say that TestFunc was creating objects (say, tables):

function TestFunc()
   return {1}, {2}
end

function SecondFunction()
   local nodeclare, var = TestFunc()
end

Now nodeclare is referencing a table returned by TestFunc. That's an object, allocated on the heap, that we don't want hanging around forever.

That object will eventually be collected if there is nothing left referring to it. In your case, as soon as SecondFunction returns, the local nodeclare goes out of scope and goes away. As long as there's nothing else referencing that table, the table will be collected (during next collection cycle).

You can avoid declaring nodeclare entirely by skipping the first return value of TestFunc like this:

local var = select(2, TestFunc())

However, when you're talking about a temporary local variable, as in your example, you normally just create the temporary variable then ignore it. This avoids the overhead of the call to select. Sometimes you use a variable name that indicates it's trash:

local _, var = TestFunc()

If say I was doing a pairs loop over 100 values, would that even have a signifigant impact?

None whatsoever. You're just continually overwriting the value of a local variable.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • Upvoted, but there is a gotcha here. If you call this in a loop, declare the locals outside the loop. Otherwise, instead of overwriting the local, you are shadowing it and this gives more work to the GC. – catwell Feb 28 '14 at 07:20
  • @catwell I've not heard of that. Can you elaborate? – Mud Feb 28 '14 at 16:59
  • @catwell No it doesn't. Lua uses a register-based VM; even if the inner variables shadow the outer ones, each variable is assigned a single register, and this assignment cannot be changed. As Mud said, _variables_ are not GC'd. – Colonel Thirty Two Feb 28 '14 at 18:13
  • 1
    @ColonelThirtyTwo You are right actually, the same register will be re-used so it makes no difference. Although technically it is not the same variable, but it matters only if you create closures. I made a gist to show it here: https://gist.github.com/catwell/9281887 – catwell Feb 28 '14 at 23:11
1

What impact do you mean exactly? Memory? Performance?

According to the Programming in Lua book, you can sort of skip the second return value, but not ignore the first and use the second:

x,y = foo2()        -- x='a', y='b'
x = foo2()          -- x='a', 'b' is discarded
x,y,z = 10,foo2()   -- x=10, y='a', z='b'
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • I was actually already doing this, but I assume there would at some point be an impact when the variable gets gc'd as it goes out of scope. I basically have to loop through a table of up to 200 objects every frame, and I thought maybe saving 200 variable declarations and 200 variables going out of scope (and if they're not declared they can't become un-gc'd upvalues.) – user2671497 Feb 27 '14 at 20:14
  • 1
    I don't think you have to worry -- Lua is very well designed and by far the fastest interpreted language. – Ridcully Feb 27 '14 at 20:16
  • Edited my previous comment. – user2671497 Feb 27 '14 at 20:16
  • I see. Perhaps you can rewrite the function so that it only returns the value you use? If you mean `pairs()` you don't have to worry as it returns just references to key and value as far as I know. – Ridcully Feb 27 '14 at 20:19
  • I am pretty sure that they actually declare local named variables, because if you set those variables to something, the table remains unaffected, and you can pass them as upvalues when out of scope. – user2671497 Feb 27 '14 at 20:21
  • The variables in Lua are just pointers. I you set them to another value, of course the table remains unaffected. – Ridcully Feb 27 '14 at 20:28