WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved] Strange sluggishness of Lua in x86 TinyCore  (Read 3501 times)

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 661
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #15 on: September 16, 2022, 12:14:59 PM »
@Rich

Hello, isn't the -Os option in the gcc line for small size ?
Have you tried to use -Ofast ??

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #16 on: September 16, 2022, 12:16:31 PM »
gcc 10 -O3
Code: [Select]
tc@box:/tmp/lua-5.3.6/src$ ls -l liblua.a lua luac
-rw-r--r--    1 tc       staff       401796 Sep 16 22:14 liblua.a
-rwxr-xr-x    1 tc       staff       319660 Sep 16 22:14 lua
-rwxr-xr-x    1 tc       staff       209232 Sep 16 22:14 luac
tc@box:/tmp/lua-5.3.6/src$ time ./lua -e 'for i=1,1000000 do end'
real    0m 1.18s
user    0m 1.18s
sys     0m 0.00s

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #17 on: September 16, 2022, 12:19:48 PM »
gcc 10 -Ofast
Code: [Select]
tc@box:/tmp/lua-5.3.6/src$ ls -l liblua.a lua luac
-rw-r--r--    1 tc       staff       401020 Sep 16 22:18 liblua.a
-rwxr-xr-x    1 tc       staff       319112 Sep 16 22:18 lua
-rwxr-xr-x    1 tc       staff       209196 Sep 16 22:18 luac
tc@box:/tmp/lua-5.3.6/src$ time ./lua -e 'for i=1,1000000 do end'
real    0m 1.20s
user    0m 1.19s
sys     0m 0.00s

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 661
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #18 on: September 16, 2022, 12:20:30 PM »
@jazzbiker
*lol*

You have to insert all your values into some type of spreadsheet, to get some grip of all variants.

Have you tried to use clang ??
I don't know if someone have make an extension of clang.

But i have seen some youtuber struggle with compiling clang.
Making LLVM and so on.
Make his own Linux dist...https://t2sde.org/ for many processors architecture.
Like it alot.

Here's the links:
https://www.youtube.com/user/renerebe/videos
https://www.youtube.com/c/MoreReneRebe/videos
« Last Edit: September 16, 2022, 12:32:46 PM by patrikg »

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #19 on: September 16, 2022, 12:25:12 PM »
@patrikg
And the title of the spreadsheet will be "Don't use gcc 10 optimizations" :) :) :)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #20 on: September 16, 2022, 12:38:16 PM »
Hi jazzbiker
TC12 x86 lua-5.3.6 gcc, no optimization.
src/Makefile
Code: [Select]
CFLAGS= -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS) ...
fast, still slower than gcc 11 in TC13 x86 with -Os.
Since you show  -Os  and  -O2  slow down the code, it makes me wonder what values  SYSCFLAGS
and  MYCFLAGS  are set to.

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #21 on: September 16, 2022, 01:47:45 PM »
Since you show  -Os  and  -O2  slow down the code, it makes me wonder what values  SYSCFLAGS
and  MYCFLAGS  are set to.
@Rich
Here are lines copied from the build output:
Code: [Select]
gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_LINUX    -c -o lbaselib.o lbaselib.c
gcc -std=gnu99 -o lua   lua.o liblua.a -lm -Wl,-E -ldl -lreadline
just from vanilla make linux

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #22 on: September 16, 2022, 02:10:24 PM »
Have you tried to use clang ??
No, gcc and tcc (like it). Planning to try https://github.com/michaelforney/cproc, frontend for https://c9x.me/compile/
I don't know if someone have make an extension of clang.
clang.tcz I see in repo. Isn't it?
Make his own Linux dist...https://t2sde.org/ for many processors architecture.
Thanks for the link! One more link in reply - https://github.com/oasislinux/oasis.

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 661
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #23 on: September 16, 2022, 02:31:20 PM »
@jazzbiker sorry but am using arch for my desktop.

Not using tc for my desktop, so i don't know so much about x86_64 and i386.

But have in the past used tc for my rpi. 

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #24 on: September 16, 2022, 03:49:28 PM »
TC12 x86 gcc 10 lua-5.3.6

Applying -fno-unwind-tables and -fno-asynchronous-unwind-tables reverts the strange increase of binary size if -O2 used (see above)

gcc -O2
Code: [Select]
tc@box:/tmp/lua-5.3.6/src$ ls -l liblua.a lua luac
-rw-r--r--    1 tc       staff       359420 Sep 17 01:27 liblua.a
-rwxr-xr-x    1 tc       staff       288128 Sep 17 01:27 lua
-rwxr-xr-x    1 tc       staff       189412 Sep 17 01:27 luac
tc@box:/tmp/lua-5.3.6/src$ time ./lua -e 'for i=1,1000000 do end'
real    0m 1.19s
user    0m 1.16s
sys     0m 0.01s

gcc -O2 no unwind tables
Code: [Select]
tc@box:/tmp/lua-5.3.6/src$ ls -l liblua.a lua luac
-rw-r--r--    1 tc       staff       280756 Sep 17 01:30 liblua.a
-rwxr-xr-x    1 tc       staff       210304 Sep 17 01:30 lua
-rwxr-xr-x    1 tc       staff       144356 Sep 17 01:30 luac
tc@box:/tmp/lua-5.3.6/src$ time ./lua -e 'for i=1,1000000 do end'
real    0m 1.17s
user    0m 1.16s
sys     0m 0.00s

gcc -O0 no unwind tables
Code: [Select]
tc@box:/tmp/lua-5.3.6/src$ ls -l liblua.a lua luac
-rw-r--r--    1 tc       staff       302524 Sep 17 01:31 liblua.a
-rwxr-xr-x    1 tc       staff       238120 Sep 17 01:31 lua
-rwxr-xr-x    1 tc       staff       170024 Sep 17 01:31 luac
tc@box:/tmp/lua-5.3.6/src$ time ./lua -e 'for i=1,1000000 do end'
real    0m 0.11s
user    0m 0.10s
sys     0m 0.00s

tcc
Code: [Select]
tc@box:/tmp/lua-5.3.6/src$ ls -l liblua.a lua luac
-rw-r--r--    1 tc       staff       359332 Sep 17 01:34 liblua.a
-rwxr-xr-x    1 tc       staff       243008 Sep 17 01:34 lua
-rwxr-xr-x    1 tc       staff       170488 Sep 17 01:34 luac
tc@box:/tmp/lua-5.3.6/src$ time ./lua -e 'for i=1,1000000 do end'
real    0m 0.11s
user    0m 0.11s
sys     0m 0.00s

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #25 on: September 17, 2022, 12:48:25 AM »
TC12 x86 gcc 10 lua-5.4.4

vanilla "make linux-readline" with -O2
Code: [Select]
tc@box:/tmp/lua-5.4.4$ time src/lua -e 'for i=1,1000000 do end'
real    0m 0.05s
user    0m 0.05s
sys     0m 0.00s
tc@box:/tmp/lua-5.4.4$ ls -l src/liblua.a src/lua src/luac
-rw-r--r--    1 tc       staff       406946 Sep 17 10:22 src/liblua.a
-rwxr-xr-x    1 tc       staff       331708 Sep 17 10:22 src/lua
-rwxr-xr-x    1 tc       staff       228748 Sep 17 10:22 src/luac

MYCFLAGS= -fno-unwind-tables -fno-asynchronous-unwind-tables
Code: [Select]
tc@box:/tmp/lua-5.4.4$ time src/lua -e 'for i=1,1000000 do end'
real    0m 0.07s
user    0m 0.06s
sys     0m 0.00s
tc@box:/tmp/lua-5.4.4$ ls -l src/liblua.a src/lua src/luac
-rw-r--r--    1 tc       staff       319930 Sep 17 10:29 src/liblua.a
-rwxr-xr-x    1 tc       staff       245692 Sep 17 10:29 src/lua
-rwxr-xr-x    1 tc       staff       179596 Sep 17 10:29 src/luac

CFLAGS= -Os -Wall -Wextra -DLUA_COMPAT_5_3 $(SYSCFLAGS) $(MYCFLAGS)
MYCFLAGS= -fno-unwind-tables -fno-asynchronous-unwind-tables
Code: [Select]
tc@box:/tmp/lua-5.4.4$ time src/lua -e 'for i=1,1000000 do end'
real    0m 0.07s
user    0m 0.07s
sys     0m 0.00s
tc@box:/tmp/lua-5.4.4$ ls -l src/liblua.a src/lua src/luac
-rw-r--r--    1 tc       staff       265170 Sep 17 10:31 src/liblua.a
-rwxr-xr-x    1 tc       staff       203164 Sep 17 10:31 src/lua
-rwxr-xr-x    1 tc       staff       144480 Sep 17 10:31 src/luac

As we can see everything is all right in Lua 5.4.

Code: [Select]
tc@box:/tmp/lua-5.4.4$ src/luac -l -
for i=1,1000000 do end

main <stdin:0,0> (7 instructions at 0x9510490)
0+ params, 4 slots, 1 upvalue, 4 locals, 1 constant, 0 functions
        1       [1]     VARARGPREP      0
        2       [1]     LOADI           0 1
        3       [1]     LOADK           1 0     ; 1000000
        4       [1]     LOADI           2 1
        5       [1]     FORPREP         0 0     ; exit to 7
        6       [1]     FORLOOP         0 1     ; to 6
        7       [1]     RETURN          0 1 1   ; 0 out

In Lua 5.3 implementation of FORPREP or FORLOOP bytecodes includes some stuff making gcc 10 optimizations insane. Maybe this can be explored and allow to find what exactly cause the trouble. But gcc 11 works better, at least concerning the described circumstances.

@Rich, what's Your opinion, should we dive deeper? Will it be useful?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #26 on: September 17, 2022, 08:01:16 AM »
Hi jazzbiker
I played around a bit with this under TC10 x86, gcc 8.2, lua 5.3.6. I created a build script from:
http://tinycorelinux.net/12.x/x86/tcz/src/lua/compile_lua
I attached a copy to this post. The script has comments added.
After it builds lua, it shows its size before and after running  sstrip lua. Then it runs your benchmark:
Code: [Select]
-rwxr-xr-x 1 tc staff 213056 Sep 17 10:33 lua
-rwxr-xr-x 1 tc staff 180252 Sep 17 10:33 lua
real    0m 0.02s
user    0m 0.01s
sys     0m 0.00s
tc@E310:~/BuildLua$
This result was with optimization set to  -O1. Settings of -O0 and -Og show similar results.

To figure out whats happening would probably require figuring out which optimizations -O2, -O3, and -Os share
that are not present in -O0, -O1, and -Og. Then try disabling them one at a time to see what changes.
Personally, I have doubts its worth the effort.

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 933
Re: Strange sluggishness of Lua in x86 TinyCore
« Reply #27 on: September 17, 2022, 10:00:27 AM »
Hi Rich,

Thank You for the handy script :)

To figure out whats happening would probably require figuring out which optimizations -O2, -O3, and -Os share that are not present in -O0, -O1, and -Og. Then try disabling them one at a time to see what changes.

Ok, this will work nice. If the trouble is caused by one of optimization methods. Not by some combination of them :) . I see 40+ optimization various options being turned on after -O1 :( .

Personally, I have doubts its worth the effort.

Probably, yes. The problem is not in Lua code. It is written in absolutely plain C. Lua even has build target "c89". No pragmas, __attribute__ and another stuff alike. It means that absolutely plain C code can make gcc crazy, and we don't know in what way :( . Not very comfortable to know such things, especially encountering that nearly all extensions in TinyCore are threatened.

Anyway, I want to ask You to mark the topic as solved.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: [Solved] Strange sluggishness of Lua in x86 TinyCore
« Reply #28 on: September 17, 2022, 02:23:15 PM »
Hi jazzbiker
Thank You for the handy script :) ...
You are quite welcome. Just be aware since I was only looking at building the interpreter I
placed an exit command before it builds lua.so.

Quote
... Anyway, I want to ask You to mark the topic as solved.
Done.  :)