www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6615] New: Implement optimization for downcast to final class

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6615

           Summary: Implement optimization for downcast to final class
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: timon.gehr gmx.ch


--- Comment #0 from timon.gehr gmx.ch 2011-09-06 16:36:04 PDT ---
Consider this benchmark:

import std.stdio, std.datetime;

class A{}
final class B:A{}
A x;
static this(){x=new B;}

B f1(){ return cast(B)x; }

T fastCast(T,R)(R x){return typeid(x) is typeid(T)?cast(T)cast(void*)x:null;}

B f2(){ return fastCast!(B)(x); }

void main() {
    auto a=benchmark!(f1,f2)(1000000);
    writeln(a[0].to!("seconds",double));
    writeln(a[1].to!("seconds",double));
}


f2 is about 30x faster on my machine, compiled with dmd -O -release -inline.
(DMD 2.054). If the benchmark is changed so that the cast does not succeed, the
difference is even larger.
This means DMD should probably optimize downcasts to final classes to a simple
typeid pointer comparison. That is safe, because final classes have no
children.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 06 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6615



--- Comment #1 from timon.gehr gmx.ch 2011-09-06 16:44:26 PDT ---
hm, ok. It seems that druntime actually does shortcut and most of the
difference boils down to the fact that the druntime function cannot be
inlined...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6615


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |performance
                 CC|                            |andrej.mitrovich gmail.com
         OS/Version|Linux                       |All


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-02-08
14:29:25 PST ---
(In reply to comment #1)
 It seems that druntime actually does shortcut

Where in druntime is casting for classes defined btw? I assumed I would see an opCast function somewhere. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 08 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6615


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #3 from bearophile_hugs eml.cc 2013-02-08 14:46:40 PST ---
See also Issue 5559

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 08 2013