AnnulusGames / Lua-CSharp

High performance Lua interpreter implemented in C# for .NET and Unity
MIT License
184 stars 8 forks source link

Optimize: Reduce and merge unnecessary move instructions #48

Open Akeit0 opened 2 weeks ago

Akeit0 commented 2 weeks ago

Optimization sample 101 Instructions to 68 Instructions

local function advance(bodies, nbody, dt)
    for i = 1, nbody do
        local bi = bodies[i]
        local bix, biy, biz, bimass = bi.x, bi.y, bi.z, bi.mass
        local bivx, bivy, bivz = bi.vx, bi.vy, bi.vz
        for j = i + 1, nbody do
            local bj = bodies[j]
            local dx, dy, dz = bix - bj.x, biy - bj.y, biz - bj.z
            local dist2 = dx * dx + dy * dy + dz * dz
            local mag = sqrt(dist2)
            mag = dt / (mag * dist2)
            local bm = bj.mass * mag
            bivx = bivx - (dx * bm)
            bivy = bivy - (dy * bm)
            bivz = bivz - (dz * bm)
            bm = bimass * mag
            bj.vx = bj.vx + (dx * bm)
            bj.vy = bj.vy + (dy * bm)
            bj.vz = bj.vz + (dz * bm)
        end
        bi.vx = bivx
        bi.vy = bivy
        bi.vz = bivz
        bi.x = bix + dt * bivx
        bi.y = biy + dt * bivy
        bi.z = biz + dt * bivz
    end
end
Instructions --------------------------------------------------
[0]     (2,12)          LOADK     3 0
[1]     (2,15)          MOVE      4 1
[2]     (2,4)           LOADK     5 0
[3]     (2,4)           FORPREP   3 96
[4]     (3,19)          MOVE      7 0
[5]     (3,25)          GETTABLE  7 7 3
[6]     (4,38)          MOVE      8 7
[7]     (4,40)          GETTABLE  8 8 257
[8]     (4,44)          MOVE      9 7
[9]     (4,46)          GETTABLE  9 9 258
[10]    (4,50)          MOVE      10 7
[11]    (4,52)          GETTABLE  10 10 259
[12]    (4,56)          MOVE      11 7
[13]    (4,58)          GETTABLE  11 11 260
[14]    (5,33)          MOVE      12 7
[15]    (5,35)          GETTABLE  12 12 261
[16]    (5,40)          MOVE      13 7
[17]    (5,42)          GETTABLE  13 13 262
[18]    (5,47)          MOVE      14 7
[19]    (5,49)          GETTABLE  14 14 263
[20]    (6,18)          ADD       15 3 256
[21]    (6,23)          MOVE      16 1
[22]    (6,8)           LOADK     17 0
[23]    (6,8)           FORPREP   15 54
[24]    (7,23)          MOVE      19 0
[25]    (7,29)          GETTABLE  19 19 15
[26]    (8,37)          MOVE      20 19
[27]    (8,39)          GETTABLE  20 20 257
[28]    (8,35)          SUB       20 8 20
[29]    (8,49)          MOVE      21 19
[30]    (8,51)          GETTABLE  21 21 258
[31]    (8,47)          SUB       21 9 21
[32]    (8,61)          MOVE      22 19
[33]    (8,63)          GETTABLE  22 22 259
[34]    (8,59)          SUB       22 10 22
[35]    (9,29)          MUL       23 20 20
[36]    (9,39)          MUL       24 21 21
[37]    (9,34)          ADD       23 23 24
[38]    (9,49)          MUL       24 22 22
[39]    (9,44)          ADD       23 23 24
[40]    (10,24)         GETTABUP  24 0 264
[41]    (10,29)         MOVE      25 23
[42]    (10,24)         CALL      24 2 2
[43]    (11,28)         MUL       25 24 23
[44]    (11,21)         DIV       25 2 25
[45]    (11,12)         MOVE      24 25
[46]    (12,23)         MOVE      25 19
[47]    (12,25)         GETTABLE  25 25 260
[48]    (12,31)         MUL       25 25 24
[49]    (13,30)         MUL       26 20 25
[50]    (13,24)         SUB       26 12 26
[51]    (13,12)         MOVE      12 26
[52]    (14,30)         MUL       26 21 25
[53]    (14,24)         SUB       26 13 26
[54]    (14,12)         MOVE      13 26
[55]    (15,30)         MUL       26 22 25
[56]    (15,24)         SUB       26 14 26
[57]    (15,12)         MOVE      14 26
[58]    (16,24)         MUL       26 11 24
[59]    (16,12)         MOVE      25 26
[60]    (17,20)         MOVE      26 19
[61]    (17,22)         GETTABLE  26 26 261
[62]    (17,32)         MUL       27 20 25
[63]    (17,26)         ADD       26 26 27
[64]    (17,12)         MOVE      27 19
[65]    (17,14)         SETTABLE  27 261 26
[66]    (18,20)         MOVE      26 19
[67]    (18,22)         GETTABLE  26 26 262
[68]    (18,32)         MUL       27 21 25
[69]    (18,26)         ADD       26 26 27
[70]    (18,12)         MOVE      27 19
[71]    (18,14)         SETTABLE  27 262 26
[72]    (19,20)         MOVE      26 19
[73]    (19,22)         GETTABLE  26 26 263
[74]    (19,32)         MUL       27 22 25
[75]    (19,26)         ADD       26 26 27
[76]    (19,12)         MOVE      27 19
[77]    (19,14)         SETTABLE  27 263 26
[78]    (6,8)           FORLOOP   15 -55
[79]    (21,16)         MOVE      15 12
[80]    (21,8)          MOVE      16 7
[81]    (21,10)         SETTABLE  16 261 15
[82]    (22,16)         MOVE      15 13
[83]    (22,8)          MOVE      16 7
[84]    (22,10)         SETTABLE  16 262 15
[85]    (23,16)         MOVE      15 14
[86]    (23,8)          MOVE      16 7
[87]    (23,10)         SETTABLE  16 263 15
[88]    (24,24)         MUL       15 2 12
[89]    (24,19)         ADD       15 8 15
[90]    (24,8)          MOVE      16 7
[91]    (24,10)         SETTABLE  16 257 15
[92]    (25,24)         MUL       15 2 13
[93]    (25,19)         ADD       15 9 15
[94]    (25,8)          MOVE      16 7
[95]    (25,10)         SETTABLE  16 258 15
[96]    (26,24)         MUL       15 2 14
[97]    (26,19)         ADD       15 10 15
[98]    (26,8)          MOVE      16 7
[99]    (26,10)         SETTABLE  16 259 15
[100]   (2,4)           FORLOOP   3 -97
[101]   (2,4)           RETURN    0 1
Constants --------------------------------------------------
[0]     1
[1]     x
[2]     y
[3]     z
[4]     mass
[5]     vx
[6]     vy
[7]     vz
[8]     sqrt
UpValues --------------------------------------------------
[0]     _ENV    0       0

Instructions --------------------------------------------------
[0]     (2,12)          LOADK     3 0
[1]     (2,15)          MOVE      4 1
[2]     (2,4)           LOADK     5 0
[3]     (2,4)           FORPREP   3 63
[4]     (3,25)          GETTABLE  7 0 3
[5]     (4,40)          GETTABLE  8 7 257
[6]     (4,46)          GETTABLE  9 7 258
[7]     (4,52)          GETTABLE  10 7 259
[8]     (4,58)          GETTABLE  11 7 260
[9]     (5,35)          GETTABLE  12 7 261
[10]    (5,42)          GETTABLE  13 7 262
[11]    (5,49)          GETTABLE  14 7 263
[12]    (6,18)          ADD       15 3 256
[13]    (6,23)          MOVE      16 1
[14]    (6,8)           LOADK     17 0
[15]    (6,8)           FORPREP   15 38
[16]    (7,29)          GETTABLE  19 0 15
[17]    (8,39)          GETTABLE  20 19 257
[18]    (8,35)          SUB       20 8 20
[19]    (8,51)          GETTABLE  21 19 258
[20]    (8,47)          SUB       21 9 21
[21]    (8,63)          GETTABLE  22 19 259
[22]    (8,59)          SUB       22 10 22
[23]    (9,29)          MUL       23 20 20
[24]    (9,39)          MUL       24 21 21
[25]    (9,34)          ADD       23 23 24
[26]    (9,49)          MUL       24 22 22
[27]    (9,44)          ADD       23 23 24
[28]    (10,24)         GETTABUP  24 0 264
[29]    (10,29)         MOVE      25 23
[30]    (10,24)         CALL      24 2 2
[31]    (11,28)         MUL       25 24 23
[32]    (11,21)         DIV       24 2 25
[33]    (12,25)         GETTABLE  25 19 260
[34]    (12,31)         MUL       25 25 24
[35]    (13,30)         MUL       26 20 25
[36]    (13,24)         SUB       12 12 26
[37]    (14,30)         MUL       26 21 25
[38]    (14,24)         SUB       13 13 26
[39]    (15,30)         MUL       26 22 25
[40]    (15,24)         SUB       14 14 26
[41]    (16,24)         MUL       25 11 24
[42]    (17,22)         GETTABLE  26 19 261
[43]    (17,32)         MUL       27 20 25
[44]    (17,26)         ADD       26 26 27
[45]    (17,14)         SETTABLE  19 261 26
[46]    (18,22)         GETTABLE  26 19 262
[47]    (18,32)         MUL       27 21 25
[48]    (18,26)         ADD       26 26 27
[49]    (18,14)         SETTABLE  19 262 26
[50]    (19,22)         GETTABLE  26 19 263
[51]    (19,32)         MUL       27 22 25
[52]    (19,26)         ADD       26 26 27
[53]    (19,14)         SETTABLE  19 263 26
[54]    (6,8)           FORLOOP   15 -39
[55]    (21,10)         SETTABLE  7 261 12
[56]    (22,10)         SETTABLE  7 262 13
[57]    (23,10)         SETTABLE  7 263 14
[58]    (24,24)         MUL       15 2 12
[59]    (24,19)         ADD       15 8 15
[60]    (24,10)         SETTABLE  7 257 15
[61]    (25,24)         MUL       15 2 13
[62]    (25,19)         ADD       15 9 15
[63]    (25,10)         SETTABLE  7 258 15
[64]    (26,24)         MUL       15 2 14
[65]    (26,19)         ADD       15 10 15
[66]    (26,10)         SETTABLE  7 259 15
[67]    (2,4)           FORLOOP   3 -64
[68]    (2,4)           RETURN    0 1
Constants --------------------------------------------------
[0]     1
[1]     x
[2]     y
[3]     z
[4]     mass
[5]     vx
[6]     vy
[7]     vz
[8]     sqrt
UpValues --------------------------------------------------
[0]     _ENV    0       0
Akeit0 commented 2 weeks ago

This is not done.

Akeit0 commented 2 weeks ago

I think almost all pattern merging is done. Now the compiler returns almost the same results as luac except for the EQ - JMP pattern.