OK, that's a fair point ...thanks for making me see that !Exasperation wrote:This is a bad idea. Rather than an optimization, this is an anti-optimization.Simcra wrote:all ontick functions have been surrounded with the appropriate associative_table_count check similaraly to digging_robots_manager(event).has strictly worse performance than justCode: Select all
if associative_table_count(t) ~= 0 then for k, v in pairs(t) do --do some stuff end end
this is because the associative_table_count version is equivalent toCode: Select all
for k, v in pairs(t) do --do some stuff end
Note how if the table is empty, you're doing the same loop setup and termination check in either case, but have added the overhead of a function call, a variable assignment, and a comparison. On the other hand, if the table isn't empty, you've added the overhead of a function call, a comparison, and an additional complete loop through the entire table (with variable assignments at each step). And by doing all this extra work, you've gained... absolutely nothing in return.Code: Select all
function waste_some_time (t) local time_to_waste = 0 for k, v in pairs(t) do time_to_waste = time_to_waste + 1 end return time_to_waste ~= 0 end if waste_some_time(t) then for k, v in pairs(t) do --do some stuff end end
TL;DR: only use associative_table_count if you absolutely need to know how many things are in the table. If you just need to check if it's empty, replacewithCode: Select all
if associative_table_count(t) ~= 0 then
orCode: Select all
if next(t) then
withCode: Select all
if associative_table_count(t) == 0 then
or ask yourself if checking for emptiness is even necessary in this case (for the use case in question, it's probably better to just let the loop terminate because there are no elements to loop through than to throw in a separate emptiness check).Code: Select all
if not next(t) then
although in the next version it shouldn't even be necessary
...
actually, calling a function is really bad ?
my refactoring into classes, each capable of handling events might not be a good idea then ... (performance wise at least, because I find it much clearer to read!)