|
| 01 Sep 2015 08:58 PM |
What saves more memory and what should I use for things that could lag.
--Create a local value for every item in v for _,v in pairs(list)do local name = v[1] local price = v[2] end
-- or reassign an already set value local name,price for _,v in pairs(list)do name = v[1] price = v[2] end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 01 Sep 2015 09:04 PM |
| ^well the latter would leave a memory leak |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 01 Sep 2015 09:05 PM |
| Not really a memory leak since chances are it would be in a function or something that would eventually terminate, but in terms of what he posted they would be the exact same. |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2015 09:06 PM |
| Could you explain how they are the same if one is created a bunch of values? |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2015 09:10 PM |
| You don't have to worry about memory on ROBLOX. |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 01 Sep 2015 09:10 PM |
"Not really a memory leak since chances are it would be in a function or something that would eventually terminate" the only reason it's not really a memory leak is because lua is going to auto garbage collect it anyway but they both definitely have a longer lifetime than needed in the second since they're still active outside of their scope (that it should be in). |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 01 Sep 2015 09:11 PM |
'if one is created a bunch of values?' Please rephrase.
Anyways, the way local variables work is that they practically have no overhead. When you 'set a local variable' you are just loading some constant into a 'register'. However doing 'local name, price' on the top you might assume it would take some instructions but simple optimizations (where declaring local variables without a value are really just 'ignored' in a way) they end up doing EXACTLY the same thing. What happens after is different (registers won't be reused in example 2), but what's happening is the same:
Script 1: for loop load constant in register 0 the value of v[1] load constant in register 1 the value of v[2]
Script 2: load nil register 0 -> register 1 (however this instruction is actually optimized away) for loop load constant in register 0 the value of v[1] load constant in register 1 the value of v[2] |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 01 Sep 2015 09:13 PM |
| Lordrambo, I'm not disagreeing with you, doing the second one is pretty stupid but I'm just saying in terms of what's happening they're the same. |
|
|
| Report Abuse |
|
|
| |
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 01 Sep 2015 09:18 PM |
yeah they're going to produce the same output, the assembly dump will be a tiny bit different
I'm sick as hell right now so I might've articulated that last post poorly |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 01 Sep 2015 09:25 PM |
| The assembly dump will be the exact same for the code he's given (unless more stuff comes after it), that's the point I'm trying to make. |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 01 Sep 2015 09:42 PM |
yeah in that case it would be the "pseudo-memory leak" flaw that I pointed out relies on there being more code which there probably will be since he just set some variables |
|
|
| Report Abuse |
|
|