Giter VIP home page Giter VIP logo

Comments (1)

cardillan avatar cardillan commented on May 23, 2024

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.)

from mindcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.