WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

Array optimisation for closures

Open anufis opened this issue 7 years ago • 1 comments

Given example creates 5 real arrays and 2 unit arrays (using ech separate array for each doAfter closure even though they are members of same abstract class and can't overlap (sorry if I miss something)) even though it should be possible to use only 3 real arrays and 1 unit array in implementation. It's not a problem in smaller projects but in big projects this would easily create 100+ global arrays of same type even though every closure uses only 1-3 different variables of same type.

import ClosureTimers

init
    let r1 = GetRandomReal(0., 1.)
    let r2 = GetRandomReal(0., 1.)
    unit u = null
    let r3 = GetRandomReal(0., 1.)
    
    doAfter(2.) ->
        print(r1.toString())
    
    doAfter(3.) ->
        print(r2.toString())
        print(u.getName())
    
    doAfter(4.) ->
        print(r3.toString())
        print(r2.toString())
        print(r1.toString())
        print(u.getName())`

Sorry if I was unclear.

anufis avatar Nov 28 '18 10:11 anufis

Good idea. In principle we could do this sharing of fields for all classes, not only closures.

Only drawback would be that some bugs could have even stranger consequences. For example:

class A
class B extends A
    int x
class C extends A
    int y

init
    let b = new B()

    // ... destroy b somewhere ...

    let c = new C()  // index of b might get reused
    c.y = 42
    print(b.x) // might print 42 although variable x was never set to 42

peq avatar Nov 29 '18 20:11 peq