lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I made some changes as described previously.

Firstly using C stack variables for the index, step and limit, which
are internal variables and hence invisible to Lua code.
Secondly I now have 4 different forloop blocks - int with step > 0,
int with step <= 0, float with step > 0, float with step <= 0.
The problem I encountered was how to branch from the loop body to the
appropriate loop control block.
In the end I found that LLVM supports a computed goto operation - so
that during forprep I can save the label of the forloop block that
needs to be used  - the body can simply do a goto to the right forloop
block. The alternative is to make 4 copies of the loop body - but this
I hesitate to do.

So all these changes - did they help? Not a great deal I am afraid.
Results are below.

I haven't found a way to determine which bit is costing the most time.
I also haven't investigated the machine code being generated by LLVM.
My gut feel is that without additional type information, a limitation
of the traditional compilation approach is that we cannot avoid
expensive branches.
So if at compile time one can establish that the loop is integer / and
the step is > 0 then possibly the branching can be avoided. Perhaps
Luajit is doing this optimization.

There are some other areas to explore. For example the "external" loop
variable could be avoided if the compiler could enforce a readonly
property on the internal variable.

Anyway - I need to stay away from this problem until I have completed
implementing other bytecodes as this is an absorbing issue that can
easily consume a lot of time. Also, in a real world scenario this may
not be such an issue.

C:\github\ravi\ravi-tests>..\build\Debug\lua.exe fornum_test1.lua
1000000000.0
time taken      2.766

C:\github\ravi\ravi-tests>..\build\Debug\lua.exe fornum_test1.lua
1000000000.0
time taken      2.765

C:\github\ravi\ravi-tests>..\build\Debug\lua.exe fornum_test1.lua
1000000000.0
time taken      2.765

C:\github\ravi\ravi-tests>\luajit\luajit.exe fornum_test1.lua
1000000000
time taken      0.313

C:\github\ravi\ravi-tests>\luajit\luajit.exe fornum_test1.lua
1000000000
time taken      0.312

C:\github\ravi\ravi-tests>\luajit\luajit.exe fornum_test1.lua
1000000000
time taken      0.313

C:\github\ravi\ravi-tests>\lua-5.3.0\src\build\Release\lua.exe fornum_test1.lua
1000000000.0
time taken      9.219

C:\github\ravi\ravi-tests>\lua-5.3.0\src\build\Release\lua.exe fornum_test1.lua
1000000000.0
time taken      9.187

C:\github\ravi\ravi-tests>\lua-5.3.0\src\build\Release\lua.exe fornum_test1.lua
1000000000.0
time taken      9.188