question about performance

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

question about performance

Ulrich Schmidt
Hi all.

I want to avoid any unnecessary table creation to keep the gc workload low.

Question: will the following code create the new table "{}" only if the
parameter "tbl" is nil OR will the new table created always and garbage
collected if the parameter "tbl" is given?

---8<----------------------------
function xx(tbl)
  local todolist = tbl or {};
  ...
end;
---8<----------------------------

tia.

Ulrich Schmidt

Reply | Threaded
Open this post in threaded view
|

Re: question about performance

Marc Balmer
Hi all. 

I want to avoid any unnecessary table creation to keep the gc workload low. 

Question: will the following code create the new table "{}" only if the 
parameter "tbl" is nil

yes.

OR will the new table created always and garbage 
collected if the parameter "tbl" is given? 

no.



---8<---------------------------- 
function xx(tbl) 
local todolist = tbl or {}; 
... 
end; 
---8<---------------------------- 

tia. 

Ulrich Schmidt 

Reply | Threaded
Open this post in threaded view
|

Re: question about performance

Mike Nelson



On 10/23/2016 9:28 AM, Marc Balmer wrote:
Hi all. 

I want to avoid any unnecessary table creation to keep the gc workload low. 

Question: will the following code create the new table "{}" only if the 
parameter "tbl" is nil

yes.

OR will the new table created always and garbage 
collected if the parameter "tbl" is given? 

no.



---8<---------------------------- 
function xx(tbl) 
local todolist = tbl or {}; 
... 
end; 
---8<---------------------------- 

tia. 

Ulrich Schmidt 


This is very slightly incorrect. This code will also create a table if "tbl" is false. This is probably not that important to your use case.

-- Mike Nelson