May 03, 2012
No, I intend to call these methods very frequently and I can't afford any performance loss.

What I expect to have is something like a virtual table entry. I tied looking at the virtual tables and searching for the method delegate's .funcptr in the vtable, but didn't find it.

Having that vtable entry would allow me to

On Thu, May 3, 2012 at 11:23 PM, Timon Gehr <timon.gehr@gmx.ch> wrote:
> On 05/03/2012 09:18 PM, Gor Gyolchanyan wrote:
>>
>> So, you're saying, that the foo function actually takes the *this, which we ass manually, extracts the real foo method and calls it? AFAIK, that shouldn't be the case. The delegate extracts the A's foo and call to the delegate should be a direct call to A.foo, not a virtual call.
>>
>
> I just gave an alternative to Alex' solution, but it seems both do not actually do what you are after.
>
> Is this good enough?
>
> auto fn = function(A a){return a.foo();}
>
> fn(a);
> fn(b);



-- 
Bye,
Gor Gyolchanyan.
May 03, 2012
Le 03/05/2012 22:22, Gor Gyolchanyan a écrit :
> That workaround is pretty obvious, but I can't afford to make an extra
> call every time. This event system is supposed to be ultra-fast. Isn't
> there a way to get to the vtable etry itself?

Well :
1/ Such a trivial thing is surely inlined by any compiler with optimizations on.
2/ You can afford a virtual dispatch, but can't afford a function call ?

I do think you are in case of premature optimizations here.
May 03, 2012
Good point. That raises the question: How should I make the fastest possible dynamic dispatch of this kind?

On Fri, May 4, 2012 at 12:57 AM, deadalnix <deadalnix@gmail.com> wrote:
> Le 03/05/2012 22:22, Gor Gyolchanyan a écrit :
>
>> That workaround is pretty obvious, but I can't afford to make an extra call every time. This event system is supposed to be ultra-fast. Isn't there a way to get to the vtable etry itself?
>
>
> Well :
> 1/ Such a trivial thing is surely inlined by any compiler with optimizations
> on.
> 2/ You can afford a virtual dispatch, but can't afford a function call ?
>
> I do think you are in case of premature optimizations here.



-- 
Bye,
Gor Gyolchanyan.
May 03, 2012
On Thu, 03 May 2012 16:22:56 -0400, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> wrote:

> That workaround is pretty obvious, but I can't afford to make an extra
> call every time. This event system is supposed to be ultra-fast. Isn't
> there a way to get to the vtable etry itself?

Well, you can use the ABI to determine the __vtbl location, but I'm not sure if there's a way in compile-time to get the __vtbl index to use.  I suppose you could iterate over the vtbl entries and find the function that matches.  It would be O(n) to construct the call function, but O(1) to call it.

-Steve
May 03, 2012
On Thu, 03 May 2012 16:57:01 -0400, deadalnix <deadalnix@gmail.com> wrote:

> Le 03/05/2012 22:22, Gor Gyolchanyan a écrit :
>> That workaround is pretty obvious, but I can't afford to make an extra
>> call every time. This event system is supposed to be ultra-fast. Isn't
>> there a way to get to the vtable etry itself?
>
> 1/ Such a trivial thing is surely inlined by any compiler with optimizations on.

I wouldn't count on it.  You can't inline a lambda that is used somewhere it's not declared.

> 2/ You can afford a virtual dispatch, but can't afford a function call ?

In my experience, virtual calls are nearly as cheap as normal calls.  But doubling the function call setup/teardown/call is not insignificant.  It all depends on how much time is spent in the function being called.

> I do think you are in case of premature optimizations here.

This is very likely, it all depends on what you are doing *inside* the call.

-Steve
May 03, 2012
Basically I want to make a dynamic typing subsystem in D. I want to
take a given object of a static type, construct a dynamic interface
for it and later use that interface to event handling.
To construct a dynamic interface, I need to extract the virtual
methods without resolving them prematurely.

On Fri, May 4, 2012 at 1:11 AM, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> On Thu, 03 May 2012 16:57:01 -0400, deadalnix <deadalnix@gmail.com> wrote:
>
>> Le 03/05/2012 22:22, Gor Gyolchanyan a écrit :
>>>
>>> That workaround is pretty obvious, but I can't afford to make an extra call every time. This event system is supposed to be ultra-fast. Isn't there a way to get to the vtable etry itself?
>>
>>
>> 1/ Such a trivial thing is surely inlined by any compiler with optimizations on.
>
>
> I wouldn't count on it.  You can't inline a lambda that is used somewhere it's not declared.
>
>
>> 2/ You can afford a virtual dispatch, but can't afford a function call ?
>
>
> In my experience, virtual calls are nearly as cheap as normal calls.  But doubling the function call setup/teardown/call is not insignificant.  It all depends on how much time is spent in the function being called.
>
>
>> I do think you are in case of premature optimizations here.
>
>
> This is very likely, it all depends on what you are doing *inside* the call.
>
> -Steve



-- 
Bye,
Gor Gyolchanyan.
May 04, 2012
class Foo
{
	void test() { }
}

void main(string[] args)
{
	auto f = new Foo();
	stderr.writeln(f.__vptr[6]);
	auto del = (&f.test);
	stderr.writeln(del.funcptr);
}
May 04, 2012
On 2012-05-03 20:46, Gor Gyolchanyan wrote:
> I need to get a pointer to a virtual method, which is in turn a
> function pointer, being set by virtual method binding.
> Can anyone, please, tell me how to get it? Taking the delegate of the
> method won't do, because I need it to behave exactly as a virtual
> method call, except I pass the "this" explicitly.
> I need this in an event handling mechanism I'm making. You derive from
> the Sink class, passing your static type to the constructor, which
> scans your virtual methods, that conform to specific requirements and
> extracts them into an array, which later uses to dispatch the incoming
> events.
> It will feel much like a run-time virtual template method.

You can use two delegates:

class Foo
{
    void bar ()
    {
        writeln("Foo");
    }

    void delegate () resolveVirtualCall ()
    {
        return &bar;
    }

    static void forwardVirtualCall (Foo object)
    {
        void delegate () delegate () dg;
        dg.ptr = cast(void*) object;
        dg.funcptr = &resolveVirtualCall;
        dg()();
    }
}

class Bar : Foo
{
    void bar ()
    {
        writeln("Bar");
    }
}

void main()
{
    Foo b = new Bar;
    Foo.forwardVirtualCall(b);
}

Will print "Bar". If "resolveVirtualCall" is not used and "bar" is used directly instead, it will print "Foo".

-- 
/Jacob Carlborg
May 04, 2012
Thanks, I'll look into it.

On Fri, May 4, 2012 at 11:25 AM, Jacob Carlborg <doob@me.com> wrote:
> On 2012-05-03 20:46, Gor Gyolchanyan wrote:
>>
>> I need to get a pointer to a virtual method, which is in turn a
>> function pointer, being set by virtual method binding.
>> Can anyone, please, tell me how to get it? Taking the delegate of the
>> method won't do, because I need it to behave exactly as a virtual
>> method call, except I pass the "this" explicitly.
>> I need this in an event handling mechanism I'm making. You derive from
>> the Sink class, passing your static type to the constructor, which
>> scans your virtual methods, that conform to specific requirements and
>> extracts them into an array, which later uses to dispatch the incoming
>> events.
>> It will feel much like a run-time virtual template method.
>
>
> You can use two delegates:
>
> class Foo
> {
>    void bar ()
>    {
>        writeln("Foo");
>    }
>
>    void delegate () resolveVirtualCall ()
>    {
>        return &bar;
>    }
>
>    static void forwardVirtualCall (Foo object)
>    {
>        void delegate () delegate () dg;
>        dg.ptr = cast(void*) object;
>        dg.funcptr = &resolveVirtualCall;
>        dg()();
>    }
> }
>
> class Bar : Foo
> {
>    void bar ()
>    {
>        writeln("Bar");
>    }
> }
>
> void main()
> {
>    Foo b = new Bar;
>    Foo.forwardVirtualCall(b);
> }
>
> Will print "Bar". If "resolveVirtualCall" is not used and "bar" is used directly instead, it will print "Foo".
>
> --
> /Jacob Carlborg



-- 
Bye,
Gor Gyolchanyan.
May 04, 2012
On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote:
> I need to get a pointer to a virtual method, which is in turn a
> function pointer, being set by virtual method binding.
> Can anyone, please, tell me how to get it? Taking the delegate of the
> method won't do, because I need it to behave exactly as a virtual
> method call, except I pass the "this" explicitly.
> I need this in an event handling mechanism I'm making. You derive from
> the Sink class, passing your static type to the constructor, which
> scans your virtual methods, that conform to specific requirements and
> extracts them into an array, which later uses to dispatch the incoming
> events.
> It will feel much like a run-time virtual template method.

If I understand you correctly, you could use something like this

template methodToFunction(A, string method)
{
     auto methodToFunction(A a, ParameterTypeTuple!(mixin("A." ~ method)) p)
     {
         return mixin("&a." ~ method)(p);
     }
}

If you use it like this:

class Foo
{
    int m;

    void bar(int a, string b)
    {
        writefln("%s\t%s\t%s", m, a, b);
    }
}

class Child: Foo
{
    void bar(int a, string b)
    {
        writeln("child");
    }
}

void main()
{
    auto foo = new Foo;
    foo.m = 1;
    auto child = new Child;
    auto bar = &methodToFunction!(Foo, "bar");
    bar(foo, 2, "3");
    bar(child, 2, "3");
}

It prints:

1	2	3
child

It won't work correctly with overloading, though. It could
be made to work with overloading, but then you would
have to pass all the parameter types of the method to
the methodToFunction template.