dibyendumajumdar / ravi

Ravi is a dialect of Lua, featuring limited optional static typing, JIT and AOT compilers
http://ravilang.github.io/
Other
1.16k stars 60 forks source link

Ravi Performance x LuaJIT Performance #243

Closed An0nyMooUS closed 2 years ago

An0nyMooUS commented 2 years ago
local function x()
  local j=0.0
  for i=1,1000000000 do
    j = j+1.0
    end
  return j
end

if ravi then
  ravi.optlevel(3)
  ravi.compile(x)
  --ravi.dumpir(x)
end

if jit then
  -- LuaJIT warmup
  --x()
end

local t1 = os.clock()
local y = x(); 
local t2 = os.clock()
print(y)

assert(y == 1000000000.0)
print("time taken ", t2-t1); 

LuaJIT:

1000000000
time taken      0.804

Ravi:

1000000000.0
time taken      2.376

Why Ravi is slow? Its correct? I think JIT is not enabled 🤔 (my frist time using this)

dibyendumajumdar commented 2 years ago

hi, I am curious what you see with this:

local function x()
  local j: number=0.0
  for i=1,1000000000 do
    j = j+1.0
    end
  return j
end
An0nyMooUS commented 2 years ago

hi, I am curious what you see with this:

local function x()
  local j: number=0.0
  for i=1,1000000000 do
      j = j+1.0
      end
  return j
end

Same time. (~2.5s)

dibyendumajumdar commented 2 years ago

hmm. It was half the time in my test, but I didn't check LuaJIT. The current version of Ravi uses MIR JIT - that don't optimize as well as LLVM. With LLVM this test would be 0 secs as it gets rid of the loop.

An0nyMooUS commented 2 years ago

hmm. It was half the time in my test, but I didn't check LuaJIT. The current version of Ravi uses MIR JIT - that don't optimize as well as LLVM. With LLVM this test would be 0 secs as it gets rid of the loop.

why LLVM backend is removed? LLVM is better than MIR jit. i thnk 🤔

dibyendumajumdar commented 2 years ago

Its too big - 20 x bigger than Ravi in binary size. Doesn't make sense

An0nyMooUS commented 2 years ago

Its too big - 20 x bigger than Ravi in binary size. Doesn't make sense

yep. i thnk so. but for performance is good.

i tryed AOT compile, same time. (~2s) (MSVC 2022 - flags: /Ox /Oi)

dibyendumajumdar commented 2 years ago

But you can try a different option

local x = compiler.load([[
        local j:number =0.0
        for i=1,1000000000 do
                j = j+1.0
        end
        return j
        ]]
        )

local t1 = os.clock()
local y = x();
local t2 = os.clock()
                                        --    print(y)
                                        --
assert(y == 1000000000.0)
print("time taken ", t2-t1);
dibyendumajumdar commented 2 years ago

Its too big - 20 x bigger than Ravi in binary size. Doesn't make sense

yep. i thnk so. but for performance is good.

i tryed AOT compile, same time. (~2s) (MSVC 2022 - flags: /Ox /Oi)

Try will clang or gcc. Also use the version I suggested

An0nyMooUS commented 2 years ago

But you can try a different option

local x = compiler.load([[
        local j:number =0.0
        for i=1,1000000000 do
                j = j+1.0
        end
        return j
        ]]
        )

local t1 = os.clock()
local y = x();
local t2 = os.clock()
                                        --    print(y)
                                        --
assert(y == 1000000000.0)
print("time taken ", t2-t1);

Oh! Nice! (0.777s)

Why is this not standard? For every lua file, i need compiler.load (string...)?

Hm....

JIT compiles a chunk of code and returns a closure on the stack representing the compiled output.

this should be standard 🤔 do not you think?

An0nyMooUS commented 2 years ago

Hm... compiler.load not works for all files.

https://raw.githubusercontent.com/MaHuJa/CC-scripts/master/sha256.lua (Var args not supported)

An0nyMooUS commented 2 years ago
    static lua_State* state = nullptr;

    if (!state)
    {
        state = luaL_newstate();
        luaL_openlibs(state);

        raviV_setjitenabled(state, true);
        raviV_setminexeccount(state, 0); // Force compile MIR JIT
        raviV_setoptlevel(state, 3);
    }

    if ((luaL_loadfile(state, filePath.c_str()) != LUA_OK))
    {
            printf("[Uncompressed Buffer Fail] %s\n", lua_tostring(state, -1));
        return false;
    }
    else
        lua_pcall(state, 0, 0, 0); // Same time. (2.5s)
    ...........
dibyendumajumdar commented 2 years ago

Hm... compiler.load not works for all files.

https://raw.githubusercontent.com/MaHuJa/CC-scripts/master/sha256.lua (Var args not supported)

Yeah, this is work in progress new compiler. It doesn't support some features.

But, note that for general case, LuaJIT is a better solution; Ravi can do well if you your code is heavy in numerics and you are willing to annotate with types.