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

--- Comment #0 from 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);

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

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to