Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid costly dynamic cast from all class/interface upcasts #19017

Open
dlangBugzillaToGithub opened this issue Jul 19, 2015 · 0 comments
Open

Comments

@dlangBugzillaToGithub
Copy link

Kenji Hara (@9rnsr) reported this on 2015-07-19T12:45:33Z

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

Description

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant