lua-users home
lua-l archive

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


On Sat, Jul 18, 2020 at 11:43 AM JeanHeyd Meneide
<phdofthehouse@gmail.com> wrote:
>
> Dear Ya'll,
>
>      Just wanted to chime in with a little fun tidbit!
>
>      Compiler Explorer (the "Godbolt It" website for compiling all
> manner of C, C++, and other kinds of programming language code) now
> supports linking libraries, and that means you can run Lua 5.3.5 or
> Lua 5.4.0 code on it. This includes using the C API to have fun:
>
>      https://godbolt.org/z/nqeYY8
>
>      (It also has sol2, my library, there as well :D !)
>
>      This can make it easier to give examples and tiny snippets of
> working Lua C API code to folks!
>
> Best Wishes,
> ThePhD

I stumbled upon a minor missed optimization in luaL_dostring by
playing with that. It's expanded as a macro to "(luaL_loadstring(L, s)
|| lua_pcall(L, 0, LUA_MULTRET, 0))". The missed optimization is that
GCC optimizes for the failure path of luaL_loadstring for some reason,
so the conditional forward branch is taken when it succeeds. It should
instead optimize for the success path, setting up the conditional
forward branch to be taken when it fails instead. (On x86 and x64 at
least, conditional forward branches are more efficient when the
not-taken path is the common one.) If we wanted to, we could give a
hint to GCC with macros like the Linux kernel's "likely" and
"unlikely", so that luaL_dostring would instead expand to
"(__builtin_expect(luaL_loadstring(L, s), 0) || lua_pcall(L, 0,
LUA_MULTRET, 0))".

Joseph C. Sible