www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14809] New: Avoid costly dynamic cast from all

https://issues.dlang.org/show_bug.cgi?id=14809

          Issue ID: 14809
           Summary: Avoid costly dynamic cast from all class/interface
                    upcasts
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: performance
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: k.hara.pg gmail.com

This is a possible optimization for the common interface casts. But it requires
a breaking of not well known small assumption.

In git-head (f78b25c9fb00bf024cd1d6e394f696bba4f2187b), issue 1747 and issue
2013 has been fixed properly. With the latest dmd:

interface IA {}
interface IB {}

interface IC : IB, IA {}

interface ID : IC {}
interface IE : IC {}

// C instance layout with -m32:
// ofs:   8   12  16  20
//        IB, IA  IB, IA
//        IC      IC
class C : ID,     IE
{
}

void main()
{
    C c = new C();

    ID id = c;  // class to base interface: static cast
    IE ie = c;  // class to base interface: static cast

    IC ic1 = id;    // intreface to 1st base interface: static cast
    IC ic2 = ie;    // class to base interface: static cast
    assert(ic1 !is ic2);
    // ic1 and ic2 are not identical, but they points same vtbl.
    // therefore any member function call via them will work.

    IA ia1 = ic1;   // interface to 2nd base interface: dynamic cast!
    IA ia2 = ic2;   // interface to 2nd base interface: dynamic cast!
    assert(ia1 is ia2);
    // By the dynamic cast, two IA will be identical.
    // However, like to the ic case, it's inherently unnecessary.
}

If we can agree to allow to fail the last assert(ia1 is ia2), we can remove
costly dynamic cast from all class/interface upcasts.

--
Jul 19 2015