0x7CFE / llst

LLVM powered Little Smalltalk.
Other
93 stars 10 forks source link

Speed up the addition of smallints #29

Open kpp opened 11 years ago

kpp commented 11 years ago

Temp int + const int is faster than const int + temp int:

    '1+1' printNl.
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| 1+1] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| 1+1] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| 1+1] ] ;printNl .
    '' printNl.

    'x+x' printNl.
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| x+x] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| x+x] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| x+x] ] ;printNl .
    '' printNl.

    'x+1' printNl.
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| x+1] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| x+1] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| x+1] ] ;printNl .
    '' printNl.

    '1+x' printNl.
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| 1+x] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| 1+x] ] ;printNl .
    Timer millisecondsToRun: [ 1 to: 1000000 do: [:x| 1+x] ] ;printNl .
    '' printNl.
1+1
677
678
679

x+x
674
673
684

x+1
650
648
653

1+x
695
696
697
0x7CFE commented 10 years ago

What is the actual proposal here? Reorder the operands or patch the code inserting more efficient instruction?

Anyway, I don't think it's a very important optimization :) P.S.: What are the timings for the equivalent JIT execution?

kpp commented 10 years ago

The problem is: there is a huge difference in performance between 'x+1' and '1+x'. Take a look at the bytecode below:

METHOD Undefined
main  
    " initialize classes "
    Char initialize.

    " x+1 "
    [:x| x+1].
    '' printNl.

    " 1+x "
    [:x| 1+x].

    File fileIn: '../image/disasm.st'.
    Undefined methods at: #main; disassemble.

    ^nil.
!
0000 PushLiteral 0
0001 MarkArguments 1
0002 SendMessage initialize
0003 DoSpecial popTop
0004 PushBlock
0007     PushTemporary 2
0008     PushConstant 1
0009     SendBinary +
0010     DoSpecial stackReturn
0011 DoSpecial popTop
0012 PushLiteral 2
0013 MarkArguments 1
0014 SendMessage printNl
0015 DoSpecial popTop
0016 PushBlock
0019     PushConstant 1
0020     PushTemporary 2
0021     SendBinary +
0022     DoSpecial stackReturn
0023 DoSpecial popTop
0024 PushLiteral 4
0025 PushLiteral 5
0026 MarkArguments 2
0027 SendMessage fileIn:
0028 DoSpecial popTop
0029 PushLiteral 7
0030 MarkArguments 1
0031 SendMessage methods
0032 PushLiteral 9
0033 MarkArguments 2
0034 SendMessage at:
0035 DoSpecial duplicate
0036 MarkArguments 1
0037 SendMessage disassemble
0038 DoSpecial popTop
0039 PushConstant nil
0040 DoSpecial stackReturn
0041 DoSpecial popTop
0042 DoSpecial selfReturn