Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization problem: eliminate temporary variables #52

Closed
00001H opened this issue Apr 29, 2022 · 1 comment
Closed

Optimization problem: eliminate temporary variables #52

00001H opened this issue Apr 29, 2022 · 1 comment

Comments

@00001H
Copy link

00001H commented Apr 29, 2022

This code(again):

CENTER = foundation1
if uradar(enemy,any,any,distance,0,1,rslt)
    if len(rslt.x - @unit.x,rslt.y - @unit.y) < 20
        end()
    end
end

if ulocate(damaged, dmgx, dmgy, dmgbuilding)
    approach(dmgx, dmgy, 5)
    target(dmgx, dmgy, true)
else
    approach(CENTER.x + 10 * sin(@tick), CENTER.y + 10 * cos(@tick), 2)
end

compiled, the last 3 lines are:

set __tmp27 2
ucontrol approach __tmp21 __tmp26 __tmp27 0 0
end

That is redundant: we can eliminate __tmp27 and run ucontrol approach __tmp21 __tmp26 2 0 0 instead.

Summing up: ANY not-changed temporary variables should be eliminated

PS: I know Java, and I can help make a PR(and make commits) if you want(altho I have very little time)

@cardillan
Copy link
Owner

cardillan commented Jan 5, 2023

This is indeed happening and can be quite burdensome especially when drawing. For example, this code snippet

color(255,80,80,255)
rect(x, y3, 14, 8)
color(0,0,0,255)
rect(x + 2, y + 5, 8, 4)
rect(x12, y3, 2, 2)
rect(x12, y + 9, 2, 2)
color(255,80,80,255)
rect(x + 4, y + 6, 2, 2)

even with optimization turned on produces the following:

set __tmp0 255
set __tmp1 80
set __tmp2 80
set __tmp3 255
draw color __tmp0 __tmp1 __tmp2 __tmp3 0 0
set __tmp4 14
set __tmp5 8
draw rect x y3 __tmp4 __tmp5 0 0
set __tmp6 0
set __tmp7 0
set __tmp8 0
set __tmp9 255
draw color __tmp6 __tmp7 __tmp8 __tmp9 0 0
op add __tmp11 x 2
op add __tmp13 y 5
set __tmp14 8
set __tmp15 4
draw rect __tmp11 __tmp13 __tmp14 __tmp15 0 0
set __tmp16 2
set __tmp17 2
draw rect x12 y3 __tmp16 __tmp17 0 0
op add __tmp19 y 9
set __tmp20 2
set __tmp21 2
draw rect x12 __tmp19 __tmp20 __tmp21 0 0
set __tmp22 255
set __tmp23 80
set __tmp24 80
set __tmp25 255
draw color __tmp22 __tmp23 __tmp24 __tmp25 0 0
op add __tmp27 x 4
op add __tmp29 y 6
set __tmp30 2
set __tmp31 2
draw rect __tmp27 __tmp29 __tmp30 __tmp31 0 0

That not only slows the execution down, it also pushes the code closer to 1000 instructions limit.

Fortunately, there's a very helpful workaround: assign all literals passed to functions to a variable:

K0 = 0
K2 = 2
K8 = 8
K14 = 14
K80 = 80
K255 = 255

// ...

color(K255,K80,K80,K255)
rect(x, y3, K14, K8)
color(K0,K0,K0,K255)
rect(x + 2, y + 5, K8, K4)
rect(x12, y3, K2, K2)
rect(x12, y + 9, K2, K2)
color(K255,K80,K80,K255)
rect(x + 4, y + 6, K2, K2)

We get this:

set K0 0
set K2 2
set K8 8
set K14 14
set K80 80
set K255 255
draw color K255 K80 K80 K255 0 0
draw rect x y3 K14 K8 0 0
draw color K0 K0 K0 K255 0 0
op add __tmp7 x 2
op add __tmp9 y 5
draw rect __tmp7 __tmp9 K8 K4 0 0
draw rect x12 y3 K2 K2 0 0
op add __tmp11 y 9
draw rect x12 __tmp11 K2 K2 0 0
draw color K255 K80 K80 K255 0 0
op add __tmp13 x 4
op add __tmp15 y 6
draw rect __tmp13 __tmp15 K2 K2 0 0

(The optimization will even remove unused variables, so there's no particular need to remove variables for literals that aren't used anymore.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants