stevedonovan / luar

luar is a Go package for conveniently working with the luago Lua bindings. Arbitrary Go functions can be registered
MIT License
301 stars 55 forks source link

luajit #23

Closed glycerine closed 6 years ago

glycerine commented 9 years ago

Is luajit supported/workable with luar, or only lua?

Thanks!

Jason

stevedonovan commented 9 years ago

LuaJit is 5.1 compatible so it should work, would have to adjust the shared library of course. I'll have a look when I get back from my travels.

rabarar commented 9 years ago

Is luajit supported? I was having issues and would like to know the official word.

stevedonovan commented 9 years ago

It should work, because LuaJIT is 5.1 binary compatible; let me actually try this. Any particular issues?

On Mon, Jan 26, 2015 at 7:48 AM, Rob Baruch notifications@github.com wrote:

Is luajit supported? I was having issues and would like to know the official word.

— Reply to this email directly or view it on GitHub https://github.com/stevedonovan/luar/issues/23#issuecomment-71417186.

rabarar commented 9 years ago

I think the problem was around the ffi library

local ffi = require("ffi") ffi.cdef[[ int printf(const char *fmt, ...); ]] ffi.C.printf("Hello %s!", "world") The script above failed for me...

On Jan 26, 2015, at 12:56 AM, Steve J Donovan notifications@github.com wrote:

It should work, because LuaJIT is 5.1 binary compatible; let me actually try this. Any particular issues?

On Mon, Jan 26, 2015 at 7:48 AM, Rob Baruch notifications@github.com wrote:

Is luajit supported? I was having issues and would like to know the official word.

— Reply to this email directly or view it on GitHub https://github.com/stevedonovan/luar/issues/23#issuecomment-71417186.

— Reply to this email directly or view it on GitHub.

rabarar commented 9 years ago

Steve,

Just checking on the email reply -- making sure you got it..

By the way, I've thoroughly enjoyed reading your code -- you're GoToLua function creation is very cool.

Rob

On Jan 26, 2015, at 12:56 AM, Steve J Donovan notifications@github.com wrote:

It should work, because LuaJIT is 5.1 binary compatible; let me actually try this. Any particular issues?

On Mon, Jan 26, 2015 at 7:48 AM, Rob Baruch notifications@github.com wrote:

Is luajit supported? I was having issues and would like to know the official word.

— Reply to this email directly or view it on GitHub https://github.com/stevedonovan/luar/issues/23#issuecomment-71417186.

— Reply to this email directly or view it on GitHub.

stevedonovan commented 9 years ago

Sure, I'm here - will have a bit of time to look at this issue over the weekend.

Not sure how LuaJIT as a shared library resolves its ffi symbols....

On Tue, Jan 27, 2015 at 5:43 PM, Rob Baruch notifications@github.com wrote:

Steve,

Just checking on the email reply -- making sure you got it..

By the way, I've thoroughly enjoyed reading your code -- you're GoToLua function creation is very cool.

Rob

On Jan 26, 2015, at 12:56 AM, Steve J Donovan notifications@github.com wrote:

It should work, because LuaJIT is 5.1 binary compatible; let me actually try this. Any particular issues?

On Mon, Jan 26, 2015 at 7:48 AM, Rob Baruch notifications@github.com wrote:

Is luajit supported? I was having issues and would like to know the official word.

— Reply to this email directly or view it on GitHub https://github.com/stevedonovan/luar/issues/23#issuecomment-71417186.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub https://github.com/stevedonovan/luar/issues/23#issuecomment-71669170.

stevedonovan commented 9 years ago

Actually works fine - there is a subtety with printf here:

examples$ ./luar
luar 1.2 Copyright (C) 2013-2014 Steve Donovan
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> ffi = require 'ffi'
> ffi.cdef [[ int printf(const char *fmt, ...); ]]
> ffi.C.printf("hi %f %s\n",10,'hello')
hi 10.000000 hello

In a varargs case, any number is passed as a double - so you get odd stuff when you use %d.

tolbrino commented 9 years ago

Just fyi, I've used luar with luajit for some time without issues.

glycerine commented 6 years ago

Yep, Luar works fine with LuaJIT. As Steve says, LuaJIT is 5.1 binary compatible.

glycerine commented 6 years ago

Hmm... There's a massive slowdown that happens when running LuaJIT with Luar installed. I'm seeing about a 10x slowdown. Going to re-open this issue.

@stevedonovan @aarzilli Do you have any idea why Luar would be slowing down LuaJIT so much? I suspect the replacing the ipairs and pairs may have something to do with it, but I have not had a chance to look into it. I see the comment in the code below about a performance issue from the past.

https://github.com/stevedonovan/luar/blob/master/luar.go#L168

    Register(L, "", Map{
        "pairs": ProxyPairs,
        "type":  ProxyType,
    })
    // 'ipairs' needs a special case for performance reasons.
    RegProxyIpairs(L, "", "ipairs")
glycerine commented 6 years ago

No change when I comment out the pairs and ipairs, but I get a massive return of speed, back to usual LuaJIT perf, when I comment out the ProxyType override.

    Register(L, "", Map{
        "pairs": ProxyPairs,
        "type":  ProxyType,  // <<< looks like this is the culprit.
    })
    // 'ipairs' needs a special case for performance reasons.
    RegProxyIpairs(L, "", "ipairs")
glycerine commented 6 years ago

Can the "type()" override with ProxyType() be safely omitted perhaps, or is it essential?

Ambrevar commented 6 years ago

Can you provide examples and benchmarks of code where you experience a slow-down?

ProxyType enhances the original type. From the documentation:

It behaves like Lua's "type" except for proxies for which it returns
'table<TYPE>', 'string<TYPE>' or 'number<TYPE>' with TYPE being the go type.

It is optional and you can easily turn it off during the initialization of Luar.

glycerine commented 6 years ago

It is optional and you can easily turn it off during the initialization of Luar.

but will Luar break with it off? I haven't experienced any breaks, but then again, I haven't tried everything. Does Luar reflect-based translation depend upon getting the ProxyType back correctly, is the real question.

I observed the massive slowdown on my matrix multiplication test: https://github.com/gijit/gi/blob/master/pkg/compiler/mat_test.go#L70

addendum: I realize that's not a micro benchmark like you were probably hoping for. It just does alot of type() checking, again and again, while reading and writing matrix entries.

Ambrevar commented 6 years ago

ProxyType is a purely cosmetic enhancement, it won't break anything when it's off.

glycerine commented 6 years ago

ProxyType is a purely cosmetic enhancement, it won't break anything when it's off.

Awesome. Thank you @Ambrevar.

Ambrevar commented 6 years ago

Regarding your example, can you provide something more minimal and dependency-free?

glycerine commented 6 years ago

Regarding your example, can you provide something more minimal and dependency-free?

No. Not easily. Just make a bunch of type() calls in a loop. that should do it.

Ambrevar commented 6 years ago

But your example does not contain any call to type, does it? I don't see anything in Luar's codebase that would slow down the execution if type isn't called on the Lua side.

glycerine commented 6 years ago

But your example does not contain any call to type, does it?

It does, its just hidden under many many layers.

I'm just calling type() alot, so paying for the C->go transition cost every time magnifies the contrast with just staying in C land with the native type().

Ambrevar commented 6 years ago

Alright, then I guess you got your answer here.

Keep in mind that Luar is here for convenience only: anything it does induces a performance cost. Don't use Luar in tight loops, stick to Golua instead.

glycerine commented 6 years ago

I suppose a cache on the C side could help considerably. But that would be future feature work, and deserves its own ticket. I'll close this out. Thanks everyone for the help and input.