Please don't take this as personal criticism, but having a for that goes through all units every n div 9 frames and does something with them is really bad.

I can only assume that the function that gets called if the condition is met also has a for in it.

There's no condition here. checkEveryUnitRegularlyForEvents() is run every 10 ticks no matter what and it does only what you see it doing. The function it calls on each unit is this:

`function regularUnitChecks(unit)`

if not unit or not df.unit.find(unit.id) then return false end

if unitHasCreatureClass(unit,'ZENKAI') and not unitInDeadlyCombat(unit) then

doZenkai(unit)

end

local super_saiyan_trigger=dfhack.script_environment('dragonball/super_saiyan_trigger')

super_saiyan_trigger.runSuperSaiyanChecks(unit.id)

if unitUndergoingSSJEmotion(unit) then

super_saiyan_trigger.runSuperSaiyanChecksExtremeEmotion(unit.id)

end

renameUnitIfApplicable(unit)

setUpNaturalTransformations(unit)

transformation.transformation_ticks(unit.id)

if not unitInCombat(unit) or unit.counters.unconscious>0 then

transformation.revert_to_base(unit.id)

end

--12 years of training, approx.

if ((dfhack.units.isDwarf(unit) and dfhack.units.isCitizen(unit)) or isAdventurer(unit)) and getPowerLevel(unit)>900000000 and not has_whis_event_called_this_round then

dfhack.run_script('dragonball/whis_event')

has_whis_event_called_this_round=true

end

if dfhack.persistent.get('DRAGONBALL_IMMORTAL/'..unit.id) then

dfhack.run_script('full-heal','-unit',unit.id,'-r')

end

end

While most of those are rather opaque, I can tell you that: unitHasCreatureClass is O(n), but only by number of creature classes, which is rarely larger than 4 or 5, where m is creature classes and very small; unitInDeadlyCombat is O(1), and extremely fast; doZenkai is O(nlogn); runSuperSaiyanChecks is O(1); unitUndergoingSSJEmotion is O(n) with the number of emotions the unit has stored, which is usually between 0 and 20; runSuperSaiyanChecksExtremeEmotion is the same as runSuperSaiyanChecks; renameUnitIfApplicable is O(n), again with creature classes; setUpNaturalTransformations is

*also* O(n) in that regard; transformation_ticks(unit.id) is O(n) with number of current transformations, which is

**never** bigger than two as of now; unitInCombat is similar to unitInDeadlyCombat; revert_to_base is O(n) with the number of current transformations; isDwarf, isCitizen, isAdventurer, getPowerLevel are all O(1).

dfhack.persistent.get is easily the worst with unit counts, being O(log n), it using std::multimap.find() and me storing a persistent index for, essentially, every single unit that can zenkai and every transformation.

In total, the complexity of that is O(nlogn), and the actual imprint on FPS is nothing compared to Dwarf Fortress's own issues on this front.