Looking for help optimizing

Weep

Godspeed to the sound of the pounding
Reaction score
400
It may be a lot to ask, since this is a pretty long bit of code, but I'm looking to optimize the snot out of this. It's a spell I originally made in GUI (yes, really) and it's pretty damaging to the FPS (though it's about 40% faster than in GUI already).

So far I've done what I know about how to streamline things - linked lists, mostly.

Would someone be willing to look through and suggest performance improvements? Also, if there are faults, feel free to point those out, too. :p

I've omitted the globals/configuration since they are mostly boring or self-explanatory. Bear in mind it requires a unit auto-indexer.

JASS:
    //====================================================================
    
    private struct UnitData
        real X
        real Y
        real facing
        real height
        
        debug static integer count = 0
        
        thistype prev = 0
        thistype next = 0
        
        method update takes unit u returns thistype
            if GetUnitTypeId(u) != 0 or GetUnitAbilityLevel(u, 'Aloc') > 0 then
                set .X = GetUnitX(u)
                set .Y = GetUnitY(u)
                set .facing = GetUnitFacing(u)
                set .height = GetUnitFlyHeight(u)
            endif
            return this
        endmethod
        
        debug static method create takes nothing returns thistype
        debug   set count=count+1
        debug   return thistype.allocate()
        debug endmethod
        debug   private method onDestroy takes nothing returns nothing
        debug   set count=count-1
        debug endmethod
    endstruct
    
    private keyword Stream
    
    //===================================================================
    // Struct with information and methods pertaining to each missile.
    private struct Missile extends array
        private unit fx
        private effect sfx
        private real time
        private UnitData headData
        private UnitData tailData
        
        thistype prev
        thistype next
        Stream parent
        
        static method moveHead takes thistype this returns nothing
            local real tempReal = 0.
            local real tempFactor = 0.5
            local real xf = 0.
            local real yf = 0.
            local real dist = SquareRoot((.parent.targetData.X - .headData.X)*(.parent.targetData.X - .headData.X) + (.parent.targetData.Y - .headData.Y)*(.parent.targetData.Y - .headData.Y))
            
            if dist <= SPEED[.parent.level] and .time >= TIME_MIN[.parent.level] then
            //  call .damage()
                call UnitApplyTimedLife(.fx, 'BTLF', 1.)
                call GroupAddUnit(.parent.dead, .fx)
                if .parent.headMissile.prev > 0 then
                    set .prev.next = 0
                    set .parent.headMissile = .prev
                    call thistype.moveHead(.parent.headMissile)
                endif
            else
                set .tailData = .tailData.next
                call .tailData.prev.destroy()
                set .headData.next = UnitData.create().update(.fx)
                set .headData.next.prev = .headData
                set .headData = .headData.next
                
                if .time >= TIME_MIN[.parent.level] then
                    if IsUnitType(.parent.target, UNIT_TYPE_FLYING) then
                        set tempReal = .parent.targetData.height - 100. + HEIGHT_DIFF[.parent.streamType]
                    else
                        set tempReal = .parent.targetData.height + HEIGHT_DIFF[.parent.streamType]
                    endif
                    call SetUnitFlyHeight(.fx, .headData.height + (SPEED[.parent.level]/(HEIGHT_ADJUSTMENT_RATE*dist))*(tempReal - .headData.height), 0.)
                endif
                
                if .time >= 10000. or (dist <= PROXIMITY[.parent.level] and .time >= TIME_MIN[.parent.level]) then
                    if .time < 10000. then
                        set .time = 10000.
                    endif
                    
                    set tempReal = Atan2(.parent.targetData.Y - .headData.Y, .parent.targetData.X - .headData.X)
                    
                    set xf = (1. - tempFactor)*(.headData.X + RMinBJ(SPEED[.parent.level], dist) * Cos(tempReal)) + tempFactor*(.headData.X + RMinBJ(SPEED[.parent.level], dist) * Cos(.headData.facing*bj_DEGTORAD))
                    set yf = (1. - tempFactor)*(.headData.Y + RMinBJ(SPEED[.parent.level], dist) * Sin(tempReal)) + tempFactor*(.headData.Y + RMinBJ(SPEED[.parent.level], dist) * Sin(.headData.facing*bj_DEGTORAD))
                    
                    call SetUnitFacing(.fx, bj_RADTODEG*Atan2(.parent.targetData.Y - yf, .parent.targetData.X - xf))
                else
                    set xf = .headData.X + SPEED[.parent.level] * Cos(.headData.facing*bj_DEGTORAD)
                    set yf = .headData.Y + SPEED[.parent.level] * Sin(.headData.facing*bj_DEGTORAD)
                    
                    set tempReal = Atan2(.parent.targetData.Y - yf, .parent.targetData.X - xf)
                    if .time >= TIME_MAX[.parent.level] then
                        call SetUnitFacing(.fx, tempReal*bj_RADTODEG)
                    else
                        call SetUnitFacing(.fx, GetRandomReal(-1.*WANDER[.parent.level], WANDER[.parent.level]) + tempReal*bj_RADTODEG)
                    endif
                endif
                call SetUnitX(.fx, xf)
                call SetUnitY(.fx, yf)
            endif
            set .time = .time + MOVEMENT_PERIOD
        endmethod
        
        method moveTail takes nothing returns nothing
            local real tempReal = 0.
            local real xf = 0.
            local real yf = 0.
            
            set .tailData = .tailData.next
            call .tailData.prev.destroy()
            set .headData.next = UnitData.create().update(.fx)
            set .headData.next.prev = .headData
            set .headData = .headData.next
            
            call SetUnitX(.fx, .next.tailData.X)
            call SetUnitY(.fx, .next.tailData.Y)
            call SetUnitFacing(.fx, .next.tailData.facing)
            if .time >= TIME_MIN[.parent.level] then
                call SetUnitFlyHeight(.fx, .next.tailData.height, 0.)
            endif
            set .time = .time + MOVEMENT_PERIOD
        endmethod
        
        static method moveDead takes nothing returns nothing
            local thistype this = thistype[GetUnitId(GetEnumUnit())]
            local real tempReal = 0.
            local real tempFactor = 0.5
            local real dist = SquareRoot((.parent.targetData.X - .headData.X)*(.parent.targetData.X - .headData.X) + (.parent.targetData.Y - .headData.Y)*(.parent.targetData.Y - .headData.Y))
            
            if dist <= SPEED[.parent.level] then
                call SetUnitX(.fx, .parent.targetData.X)
                call SetUnitY(.fx, .parent.targetData.Y)
                if IsUnitType(.parent.target, UNIT_TYPE_FLYING) then
                    set tempReal = .parent.targetData.height - 100. + HEIGHT_DIFF[.parent.streamType]
                else
                    set tempReal = .parent.targetData.height + HEIGHT_DIFF[.parent.streamType]
                endif
                call SetUnitFlyHeight(.fx, tempReal, 0.)
            endif
        endmethod
        
        static method create takes Stream parent returns thistype
            local integer i = 1
            local UnitData prevData = 0
            
            local thistype this = 0
            local unit u = CreateUnit(GetOwningPlayer(parent.caster), FX_DUMMY_UNIT_ID(IsUnitType(parent.caster, UNIT_TYPE_FLYING)), parent.casterData.X, parent.casterData.Y, parent.angle)
            set this = thistype[GetUnitId(u)]
            
            set .fx = u
            set .parent = parent
            set .time = 0
            set .prev = 0
            set .next = 0
            
            if IsUnitType(.parent.caster, UNIT_TYPE_FLYING) then
                call SetUnitFlyHeight(.fx, .parent.casterData.height - 100 + HEIGHT_DIFF[.parent.streamType], 0)
                if not IsUnitType(.parent.target, UNIT_TYPE_FLYING) then
                    call IssueImmediateOrder(.fx, "ravenform")
                endif
            else
                if IsUnitType(.parent.target, UNIT_TYPE_FLYING) then
                    call IssueImmediateOrder(.fx, "ravenform")
                else
                    call SetUnitFlyHeight(.fx, .parent.casterData.height + HEIGHT_DIFF[.parent.streamType], 0)
                endif
            endif
            
            call SetUnitX(.fx, .parent.casterData.X)
            call SetUnitY(.fx, .parent.casterData.Y)
            
            //FX
            call SetUnitScale(.fx, MISSILE_SCALE[.parent.streamType], 1., 1.)
            set .sfx = AddSpecialEffectTarget(MISSILE_FX[.parent.streamType], .fx, "overhead")
            
            //Data
            set prevData = UnitData.create().update(.fx)
            
            set .headData = prevData
            loop
                exitwhen i >= RATIO
                set .tailData = UnitData.create().update(.fx)
                
                set prevData.prev = .tailData
                set .tailData.next = prevData
                set prevData = .tailData
                set i = i+1
            endloop
            
            return this
        endmethod
        
        private static method onDeath takes nothing returns boolean
            local thistype this = thistype(GetUnitId(GetTriggerUnit()))
            
            local integer i = 0
            local UnitData previous = 0
            if IsUnitInGroup(.fx, .parent.dead) then
                loop
                    exitwhen i >= RATIO
                    set previous = .headData.prev
                    call .headData.destroy()
                    set .headData = previous
                    set i = i+1
                endloop
                debug call BJDebugMsg(I2S(UnitData.count))
                
                call ShowUnit(.fx, false)
                call GroupRemoveUnit(.parent.dead, .fx)
                call DestroyEffect(.sfx)
                call RemoveUnit(.fx)
                
                if this == .parent.tailMissile then
                    call .parent.destroy()
                endif
            endif
            return false
        endmethod
        
        private static method endMorph takes nothing returns boolean
            local thistype this = thistype[GetUnitId(GetTriggerUnit())]
            if GetSpellAbilityId() == FX_DUMMY_MORPH_ABILITY and GetTriggerUnit() == .fx then
                call SetUnitScale(.fx, MISSILE_SCALE[.parent.streamType], 1., 1.)
            endif
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_FINISH)
            call TriggerAddCondition(t, Condition(function thistype.endMorph))
            set t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
            call TriggerAddCondition(t, Condition(function thistype.onDeath))
        endmethod
    endstruct
    
    
    //===================================================================
    // Struct with information and methods pertaining to each stream of missiles.
    private struct Stream
        unit caster
        UnitData casterData
        integer level
        real angle
        
        unit target
        UnitData targetData
        
        integer streamType
        group dead
        private static boolean array deadInit
        Missile headMissile = 0
        Missile tailMissile = 0
        private timer movement
    
        thistype prev
        
        private static method moveAll takes nothing returns nothing
            local thistype this = thistype(GetTimerData(GetExpiredTimer()))
            local Missile m = 0
            
            call .casterData.update(.caster)
            call .targetData.update(.target)
            
            if .headMissile > 0 then
                call Missile.moveHead(.headMissile)
                set m = .headMissile.prev
                loop
                    exitwhen m == 0
                        if m.next == 0 then
                        endif
                    call m.moveTail()
                    set m = m.prev
                endloop
            endif
            call ForGroup(.dead, function Missile.moveDead)
        endmethod
        
        method fire takes nothing returns Missile
            set .tailMissile.prev = Missile.create(this)
            set .tailMissile.prev.next = .tailMissile
            set .tailMissile = .tailMissile.prev
            return .tailMissile
        endmethod
        
        static method create takes unit caster, integer level, unit target, real angle, thistype prev returns thistype
            local thistype this = thistype.allocate()
            set .caster = caster
            set .casterData = UnitData.create().update(caster)
            set .level = level
            set .target = target
            set .targetData = UnitData.create().update(target)
            set .angle = angle
            set .prev = prev
            
            if not deadInit[this] then
                set dead = CreateGroup()
                set deadInit[this] = true
            endif
            
            set .streamType = STREAM_TYPE(level)
            set .headMissile = .fire()
            
            set .movement = NewTimer()
            call SetTimerData(.movement, this)
            call TimerStart(.movement, MOVEMENT_PERIOD, true, function thistype.moveAll)
            return this
        endmethod
        
        private method onDestroy takes nothing returns nothing
            call ReleaseTimer(.movement)
            call GroupClear(.dead)
            call .casterData.destroy()
            call .targetData.destroy()
        endmethod
    endstruct
    
    
    //===================================================================
    // Struct with information and methods pertaining to the caster itself.
    private struct Cast extends array
        Stream lastStream
        private timer firing
        
        private static method fireAll takes nothing returns nothing
            local Stream this = thistype(GetTimerData(GetExpiredTimer())).lastStream
            loop
                exitwhen this == 0
                call this.fire()
                set this = this.prev
            endloop
        endmethod
        
        private static method start takes unit caster, unit target returns nothing
            local integer i = 1
            local thistype this = thistype[GetUnitId(caster)]
            local Stream prev = 0
            
            local real X = GetUnitX(caster)
            local real Y = GetUnitY(caster)
            local integer level = GetUnitAbilityLevel(caster, BASE_ABILITY)
            local real angle = bj_RADTODEG*Atan2(GetUnitY(target) - Y, GetUnitX(target) - X)
            
            loop
                exitwhen i > STREAM_COUNT[level]
                
                set .lastStream = Stream.create(caster, level, target, (ANGLES[2*level - 1]*I2R(i - 1) +  ANGLES[2*level]*I2R(STREAM_COUNT[level] - i))/I2R(STREAM_COUNT[level] - 1) + angle, prev)
                
                set prev = .lastStream
                set i = i+1
            endloop                         
            set .firing = NewTimer()
            call SetTimerData(.firing, this)
            call TimerStart(.firing, EMISSION_RATE, true, function thistype.fireAll)
            
            //FX
        endmethod
        
        private method stop takes nothing returns nothing
            call ReleaseTimer(.firing)
        endmethod
        
        private static method onEnd takes nothing returns boolean
            if GetSpellAbilityId() == BASE_ABILITY then
                call thistype[GetUnitId(GetTriggerUnit())].stop()
            endif
            return false
        endmethod
        
        private static method onCast takes nothing returns boolean
            if GetSpellAbilityId() == BASE_ABILITY then
                call thistype.start(GetTriggerUnit(), GetSpellTargetUnit())
            endif
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
            call TriggerAddCondition(t, Condition(function thistype.onCast))
            set t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_ENDCAST)
            call TriggerAddCondition(t, Condition(function thistype.onEnd))
        endmethod
    endstruct
 

Bribe

vJass errors are legion
Reaction score
67
RMinBJ(SPEED[.parent.level], dist)

You call this four times back-to-back. Set it to a variable instead of calling it again.

.next.tailData
.parent.targetData
.headData
.fx

You reference these a lot. Set them to a local variable to save on array searches, so instead of referencing a double-array search a bunch of times, it is only referenced once, then stored into a local.

Instead of using TimerUtils, you should be using a linked-list to iterate through all active instances.
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
RMinBJ(SPEED[.parent.level], dist)

You call this four times back-to-back. Set it to a variable instead of calling it again.

.next.tailData
.parent.targetData
.headData
.fx

You reference these a lot. Set them to a local variable to save on array searches, so instead of referencing a double-array search a bunch of times, it is only referenced once, then stored into a local.
Nice observations, thanks. There are also a few things in the "pre-final" code that I could inline by hand to save function calls.

Instead of using TimerUtils, you should be using a linked-list to iterate through all active instances.
I mostly already am. Granted, you couldn't tell since I didn't post the init, but there'll be at most around 12-18 streams to a couple hundred missiles, which are iterated by linked list. I could cut that down by a factor of 4-6 by grouping, but I'm not sure if it would really impact performance...
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top