Uncommon OOP in Lua: right or wrong?

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Uncommon OOP in Lua: right or wrong?

Rebel Neurofog
Hi, everyone!

I've been using pretty uncommon method of OOP in Lua.
Here's the description: http://neurofog.nomrhis.net/uncommon-lua-oop
It seems natural and effective for Lua by design but I've never seen
this way of OOP on Internet.
So I wonder, is it wrong and undesirable or simply uncommon but pretty fine.
Please let me know if this technic is already described somewhere else.

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Rob Kendrick-2
On Fri, Sep 24, 2010 at 10:08:17PM +0400, Rebel Neurofog wrote:
> So I wonder, is it wrong and undesirable or simply uncommon but pretty fine.
> Please let me know if this technic is already described somewhere else.

It looks like you're simply using closures for your methods.  Which is
fine, but pretty wasteful.  Your "common" approach is also wasteful
though, because it creates duplicate method functions for every
instance.

B.

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Petite Abeille
In reply to this post by Rebel Neurofog

On Sep 24, 2010, at 8:08 PM, Rebel Neurofog wrote:

> So I wonder, is it wrong and undesirable or simply uncommon but pretty fine.

Nothing wrong with it, using closures is a fine, if expensive, way of doing things.

> Please let me know if this technic is already described somewhere else.

Programming in Lua
Part II. Tables and Objects
Chapter 16. Object-Oriented Programming
16.4 - Privacy

http://www.lua.org/pil/16.4.html


Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Chris Babcock
In reply to this post by Rebel Neurofog

By 'fast' in "It's all quite self-explained. We've got all the common OOP tricks in pretty clean and fast manner," do you mean that execution speed is faster by some measure (subjective or otherwise) or that development seems faster? The context suggests the latter.

The reason I ask is that I've got only a short time to get my feet wet with Lua working on scripts external to my application before I have to start getting my hands dirty with the object oriented constructs. I can make the Java folks among the volunteers happy if private methods are no more expensive to implement than the default.

Signoff
Chris

My waffle iron beat up your smart phone.

On Sep 24, 2010 11:10 AM, "Rebel Neurofog" <[hidden email]> wrote:

Hi, everyone!

I've been using pretty uncommon method of OOP in Lua.
Here's the description: http://neurofog.nomrhis.net/uncommon-lua-oop
It seems natural and effective for Lua by design but I've never seen
this way of OOP on Internet.
So I wonder, is it wrong and undesirable or simply uncommon but pretty fine.
Please let me know if this technic is already described somewhere else.

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Rebel Neurofog
In reply to this post by Rebel Neurofog
Thanks a lot!

> http://www.lua.org/pil/16.4.html

local self = {
   balance = initialBalance,
   LIM = 10000.00,
}

Why put all these variables into table?

> Nothing wrong with it, using closures is a fine, if expensive, way of doing things.

Expensive for memory and cheaper for speed.
I think, this is cool alternative to 'object:method (args)' stuff.
Anyway, shouldn't it be at http://lua-users.org/wiki/ObjectOrientedProgramming

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Petite Abeille-2-2

On Sep 24, 2010, at 8:44 PM, Rebel Neurofog wrote:

> Thanks a lot!

You are welcome.

>
>> http://www.lua.org/pil/16.4.html
>
> local self = {
>   balance = initialBalance,
>   LIM = 10000.00,
> }
>
> Why put all these variables into table?

Just because one can?

> Expensive for memory and cheaper for speed.

Good, good. As long as it works for you :)

> I think, this is cool alternative to 'object:method (args)' stuff.

I think OOP is a passing fade as well :P

> Anyway, shouldn't it be at http://lua-users.org/wiki/ObjectOrientedProgramming

Sure.

http://lua-users.org/cgi-bin/wiki.pl?action=edit&id=ObjectOrientedProgramming






Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Jonathan Castello-2
In reply to this post by Rebel Neurofog
On Fri, Sep 24, 2010 at 11:44 AM, Rebel Neurofog
<[hidden email]> wrote:
>> Nothing wrong with it, using closures is a fine, if expensive, way of doing things.
>
> Expensive for memory and cheaper for speed.

You can mitigate the cost a little by creating the functions
themselves outside the constructor function, and creating functions
inside that simply call those functions. You save the cost of
recreating each function's bytecode by saving the functions in one
place; comparably, the "thunk" functions are a relatively minor
overhead as they simply do the work of passing the specific object to
the functions:

--
local function add(self, a, b)
  return a, b
end

function Calculator()
  local self = {}

  self.add = function(...)
    return add(self, ...)
  end
end
--

~Jonathan

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Rebel Neurofog
In reply to this post by Rebel Neurofog
I've made some benchmarking. This may help you to decide which way
fits your requipments.

Take a look: http://lua-users.org/wiki/ObjectOrientationClosureApproach

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Wesley Smith
On Fri, Sep 24, 2010 at 2:20 PM, Rebel Neurofog <[hidden email]> wrote:
> I've made some benchmarking. This may help you to decide which way
> fits your requipments.
>
> Take a look: http://lua-users.org/wiki/ObjectOrientationClosureApproach

I would say the most common approach f is to use metatables to hold
the functions instead of generating them for each object.  For
example: http://www.mat.ucsb.edu/projects/luaAV/browser/trunk/extra/modules/label/init.lua
.  It will use far less memory if you're creating lots of objects and
won't use closures for self like you're kind of doing but overriding
in a strange way.

wes

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Alexander Gladysh
In reply to this post by Rebel Neurofog
On Sat, Sep 25, 2010 at 01:20, Rebel Neurofog <[hidden email]> wrote:
> I've made some benchmarking. This may help you to decide which way
> fits your requipments.

> Take a look: http://lua-users.org/wiki/ObjectOrientationClosureApproach

You should probably add metatable-based approach to the benchmark.

Also you should measure object creation and method calls separately. For different kinds of objects one may be more important than another.

See here for example benchmark:

http://github.com/agladysh/luamarca/blob/master/bench/factory.lua

To run, clone repository, cd to it and run

./run_benchmark.sh bench/factory.lua 1e6

Alexander.
Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Wesley Smith
Here's yet another technique that uses dot syntax but doesn't have the
memory problems of closures.  It's likely to be slightly slower, but
not by much.


-- simulate a closure with __call
local closure = {}
function closure:__call(...)
        return self.f(self.self, ...)
end
setmetatable(closure, closure)

-- class table
local M = {}
M.__index = function(t, k)
        closure.self = t
        closure.f = M[k]
        return closure
end

-- instance functions
function M:something(val)
        print("something", val)
        self.something_else()
        return "return val"
end

function M:something_else()
        print("something_else")
end

-- create an instance
local m = setmetatable({}, M)

-- call it
print(m.something("a value", 23))

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Patrick Mc(avery
In reply to this post by Rebel Neurofog
> I've made some benchmarking. This may help you to decide which way
> fits your requipments.

What tools are used for the benchmarking execution time?(on Linux
perhaps?)I have a silly OO scheme I would like to test too-Patrick

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Petite Abeille
In reply to this post by Rebel Neurofog

On Sep 24, 2010, at 11:20 PM, Rebel Neurofog wrote:

> I've made some benchmarking. This may help you to decide which way
> fits your requipments.
>
> Take a look: http://lua-users.org/wiki/ObjectOrientationClosureApproach

Here is another benchmark:

http://lua-users.org/wiki/ObjectBenchmarkTests

And as Alexander mentioned, you could perhaps factor in the object creation time as well.




Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Rebel Neurofog
In reply to this post by Patrick Mc(avery
> What tools are used for the benchmarking execution time?(on Linux perhaps?)I
> have a silly OO scheme I would like to test too-Patrick

Yeah, that way GNU/Linux with original lua-5.1.4 (no LuaJIT)
It seems that people are interested in more complex benchmarking.
I'm not very handy with OOP with metatables though.
Metatables are cool for bindings, but I rarely use metatables in pure Lua.

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Edgar Toernig
In reply to this post by Jonathan Castello-2
Jonathan Castello wrote:
>
> You can mitigate the cost a little by creating the functions
> themselves outside the constructor function, and creating functions
> inside that simply call those functions.

Just slows things down.

> You save the cost of recreating each function's bytecode by saving
> the functions in one place;

The bytecode is only created once when loading the chunk.  Creating a
new closure does *not* copy or even recreate the bytecode.

Ciao, ET.

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Jonathan Castello-2
On Mon, Sep 27, 2010 at 1:03 PM, E. Toernig <[hidden email]> wrote:
> The bytecode is only created once when loading the chunk.  Creating a
> new closure does *not* copy or even recreate the bytecode.

Oh... you're right. I forgot about that.

~Jonathan

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

David Heiko Kolf
In reply to this post by Jonathan Castello-2
Jonathan Castello schrieb:
> You can mitigate the cost a little by creating the functions
> themselves outside the constructor function, and creating functions
> inside that simply call those functions. You save the cost of
> recreating each function's bytecode by saving the functions in one
> place; comparably, the "thunk" functions are a relatively minor
> overhead as they simply do the work of passing the specific object to
> the functions:

I was worried about duplicated bytecode myself, but it looks like Lua
doesn't copy the bytecode when creating a new closure, it just links to
the upvalues and has a pointer to a single "function prototype"
containing the bytecode and the constants.

Source (Lua 5.1.4):
lvm.c, Lines 719 - 738
lobject.h, Lines 228 - 312
lfunc.c, Lines 33 - 41

...assuming I understood the code correctly.

So you wouldn't need to use small wrapper closures. (But I think I did
this myself in some Lua and JavaScript projects as well).

- David

Reply | Threaded
Open this post in threaded view
|

Re: Uncommon OOP in Lua: right or wrong?

Roberto Ierusalimschy
> I was worried about duplicated bytecode myself, but it looks like Lua
> doesn't copy the bytecode when creating a new closure, it just links to
> the upvalues and has a pointer to a single "function prototype"
> containing the bytecode and the constants.
>
> Source (Lua 5.1.4):
> lvm.c, Lines 719 - 738
> lobject.h, Lines 228 - 312
> lfunc.c, Lines 33 - 41
>
> ...assuming I understood the code correctly.

You did.

-- Roberto