Help request: class, inheritance and elegance

@Jmv38 - I’ve made some bench, I was curious to see I the fastest one was the one I think.
The basic I do is take a chain of class inheritance from A to I and call a function on the last (I) that bubble up (with *.super:fn) to the root (A).

number of calls to *.super:fn = iters * (number of class inheritance - 1) - ie: 100 iters with 5 class = 400 calls to *.super:fn

== swap __index : self.super:fn() ==
5 class inheritance
>   100 iters: 0.02 sec
>  1000 iters: 0.18 sec
> 10000 iters: 1.77 sec
10 class inheritance
>   100 iters: 0.04 sec
>  1000 iters: 0.33 sec
> 10000 iters: 3.26 sec

== switch flag : self.super:fn() ==
5 class inheritance
>   100 iters: 0.01 sec
>  1000 iters: 0.18 sec
> 10000 iters: 1.79 sec
10 class inheritance
>   100 iters: 0.03 sec
>  1000 iters: 0.32 sec
> 10000 iters: 3.21 sec

== super as global : super:fn() ==
5 class inheritance
>   100 iters: 0.02 sec
>  1000 iters: 0.23 sec
> 10000 iters: 2.34 sec
10 class inheritance
>   100 iters: 0.04 sec
>  1000 iters: 0.43 sec
> 10000 iters: 4.30 sec

== no super : UpClass.fn(self) ==
5 class inheritance
>   100 iters: 0.00 sec
>  1000 iters: 0.01 sec
> 10000 iters: 0.03 sec
10 class inheritance
>   100 iters: 0.00 sec
>  1000 iters: 0.00 sec
> 10000 iters: 0.05 sec

With in mind the big overhead of properties and method access for the swap __index and switch flag, finaly the global super is not so bad…

@Jmv38 - I’ve re-updated the posts to handle cases where a call is made to a super method that is not the same as the current function. ie:

function B:fn()
    self.super:fn2()
end

Good point. I didnt even notice this was not supported :">
Btw, when you say global super:fn() you mean super.fn(obj) , right?

@Jmv38 - Nop, I mean

function B:fn
    super:fn()
end

Btw, your benchmark doesnt show so bad figures, but we lack of reference figures. Could you run it with the lighter solution to see how much we lose from it? By ‘lighter’ i mean:

local super = A
B = class(A)
B:fn()
   Super.fn(self)
End

this one shoul be much much faster?

@Jmv38 - Sure, this is the fastest one. I’ve updated the results.

Thanks @Toffer . Wow! That is x100 improvement… I’ll stick to that one then. It is strange that such a simple thing cannot be written efficiently. Thanks a lot for all your help in this challenge.