Zero-K icon indicating copy to clipboard operation
Zero-K copied to clipboard

Widget/Gadget handlers use ipairs everywhere

Open sprunk opened this issue 11 years ago • 7 comments

Apparently ipairs are expensive. Should that be changed to normal iteration or is keeping ipairs intended?

sprunk avatar Jun 04 '15 14:06 sprunk

@GoogleFrog did a messy UnitPreDamaged() optimization, judge from that, and decide if you want it on all callins

xponen avatar Jun 04 '15 14:06 xponen

That optimisation is unrelated to the removal of ipairs (it exists to prevent calling the event for gadgets that declare they aren't interested) and is not even messy. It could even be as tidy as all other call-ins if the initialisation stuff actually happened on initialisation instead of checking for the first run every time.

Replacing ipairs with normal iteration is still fairly tidy but even if it wasn't, gadget handler can afford to be messy if it helps gain performance.

sprunk avatar Jun 04 '15 15:06 sprunk

I think you should run a test to determine the difference between ipairs and a manual iterator. I think tidyness is important for the gadget handler but I would also like to keep the difference between the basecontent handler and ours small. If there is no performance gain to be had with this change then I do not want it implemented.

GoogleFrog avatar Jun 04 '15 15:06 GoogleFrog

About that, using normal iteration is faster, but only if the iterator is the most expensive part of a given loop. In other words, it only matters if the loop does 9001 iterations but has a cheap body. I experimented with it in my own code and never noticed a difference.

aeonios avatar Jun 04 '15 15:06 aeonios

there won't be any way to prove this will work because there's no gadgetHandler or widgetHandler profiler.

xponen avatar Jun 04 '15 16:06 xponen

According to https://springrts.com/wiki/Lua_Performance normal iteration is twice as fast. The overhead is comparatively small in most cases however.

There are some call-ins that tend to have cheap bodies and get called a lot and so could benefit from the reduced overhead (for example Allow*BuildStep).

sprunk avatar Jun 06 '15 00:06 sprunk

Closing because any changes in the handlers are difficult to measure.

sprunk avatar Jun 09 '15 22:06 sprunk