|
| 16 Apr 2013 05:39 PM |
While reading the Lua 5.2 reference manual, I just found out the following in the section about incompatibilities with the previous version:
"Equality between function values has changed. Now, a function definition may not create a new value; it may reuse some previous value if there is no observable difference to the new function."
Excited after reading this, I have done some tests. The Lua demo uses Lua 5.2, but Codepad still uses Lua 5.1, so I ran the following code on both:
a = function() end b = function() end
c = {}
for i = 1, 5 do table.insert(c, function() end) end
for z, g in next, c do print(z, g) end
print "---" print(a, b)
On the demo, I obtained the following:
1 function: 0x1cfc950 2 function: 0x1cfc950 3 function: 0x1cfc950 4 function: 0x1cfc950 5 function: 0x1cfc950 --- function: 0x1cf7780 function: 0x1cf7200
On codepad, I obtained the following:
1 function: 0x806f468 2 function: 0x806f480 3 function: 0x806f4b8 4 function: 0x806f8e0 5 function: 0x806f8f8 --- function: 0x806f580 function: 0x806f598
As you can notice, in the output of the demo, the memory address of all the functions created in the loop are the same, although this is not the case for the functions created outside of it. This is not the case, however, in Codepad.
What this means, concretely, is that function closures inside of loops or other constructs that may be ran repetitively will not be created over and over again, but will instead only be created once. This means that we no longer need to define, for example, functions that will be used in events that are connected in loops outside of these loops in fear of creating a lot of identical functions, which can slow down the code. Instead, Lua figures it all out for us and will only create one actual function.
For code that runs a lot of time and creates function closures, this is a huge optimization. It can also make code more clear, which is a great thing.
_________________________________________________________________________ I possess 4 ROBUX and 174 tickets. |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 05:40 PM |
| Would never have noticed that. |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 05:42 PM |
| This has to improve something. |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 05:44 PM |
| That's nice. No one would ever notice, but it's certainly a nice thing to have if you're worried about memory usage. |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 16 Apr 2013 07:25 PM |
Probably won't make a large difference for most projects, though.
Philosopher, Creator, Clanner, Wiki Writer, and Student |
|
|
| Report Abuse |
|
|
xSIXx
|
  |
| Joined: 06 Aug 2010 |
| Total Posts: 9202 |
|
|
| 16 Apr 2013 07:44 PM |
| >waits for someone to mention goto |
|
|
| Report Abuse |
|
|
HaxHelper
|
  |
| Joined: 19 Dec 2009 |
| Total Posts: 1208 |
|
|
| 16 Apr 2013 07:45 PM |
| ogm guise goto is teh best thing in el-yew-aye fiev-point-too |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 16 Apr 2013 08:06 PM |
Or more interesting:
for i = 1, 4 do print(function() return i end) end local a for i = 1, 4 do print(function() return a end) end
It means that one of the above loops is cheaper than the other. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
|
| 16 Apr 2013 08:43 PM |
lol but 5.2 has le goto's and I DONT EVEN KNOW what that means but i hear it is bad so it is bad xDDDDDDDDDDDDDDDDDDDDDDDD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! LOL |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 08:44 PM |
I now imagine Tenal saying "#yoloswag"
_________________________________________________________________________ I possess 17K+ ROBUX and 601 tickets. |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 08:44 PM |
| And Tenal ruined this intelligent thread. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
| |
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
|
| 16 Apr 2013 08:46 PM |
@guarranteed Um, no. You're just a moron. Never have I found anyone on this forum during the duration of three years that was as unintelligible as you.
Learn to spell your name correctly. |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 08:46 PM |
Then "#Gentlemen" or "#GangnamStyle"
_________________________________________________________________________ I possess 17K+ ROBUX and 601 tickets. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
|
| 16 Apr 2013 08:47 PM |
Also, threads can't be intelligent because they are only composed of 1's and 0's. The content of a thread can be intelligent, but either way you haven't contributed to anything anymore than I have.
Now please get the hell out. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
| |
|
|
| 16 Apr 2013 08:49 PM |
Then Tenal tells us the truth.
_________________________________________________________________________ I possess 17K+ ROBUX and 601 tickets. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
|
| 16 Apr 2013 08:51 PM |
| The truth is that the majority of the Scripters forum ignorantly and blindly states how GOTO is inefficient when it's sometimes actually a really great tool for maintaining efficiency and time. |
|
|
| Report Abuse |
|
|
HaxHelper
|
  |
| Joined: 19 Dec 2009 |
| Total Posts: 1208 |
|
|
| 16 Apr 2013 08:53 PM |
go back to your corner, tenal
|
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 16 Apr 2013 09:05 PM |
| Now I can't imagine Roblox implementing 5.2 |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 09:19 PM |
@Tenal Very unfortunate that the only argument you can come up with is about my name. I'm very sorry that I like this name and the actual word "guaranteed" was taken in 2007. Plus I never intended on this account to be my main but boom, it is now.
|
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 09:51 PM |
@Stravant
It seems to me that both should be as cheap. The only difference is that one returns a local variable and the other one returns a local variable one level higher. Would that make a difference?
_________________________________________________________________________ I possess 4 ROBUX and 174 tickets. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
|
| 16 Apr 2013 09:52 PM |
| Did I tell you to have gotten the username with the right spelling? Either way having an -ed verb as a username is quite stupid so even if you were to have a username with the right spelling, it'd still be quite stupid. |
|
|
| Report Abuse |
|
|
|
| 16 Apr 2013 09:53 PM |
| @Tenal I'm still quite confused on why this matters... On the internet. It's a name, deal with it. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 16 Apr 2013 10:08 PM |
Yeah, I realized Lua 5.1 was defining a new function instead of reusing it every time I made a new function a few days ago. I also realize that that means every class I make has a whole lot of memory being taken away. :/
I guess I better switch to Metatables (Which, apparently, crash ROBLOX when I send them over _G) |
|
|
| Report Abuse |
|
|