Bug 14211 – Compiler should devirtualize calls to members of final class
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-02-20T21:31:00Z
Last change time
2017-07-19T17:38:49Z
Keywords
performance, pull
Assigned to
nobody
Creator
r.sagitario
Comments
Comment #0 by r.sagitario — 2015-02-20T21:31:22Z
Compile with -m64 -inline -release -O:
class Mutex
{
void lock() {}
}
final class GCMutex : Mutex
{
//final override void lock() { super.lock(); }
}
void test()
{
GCMutex gcLock = new GCMutex;
gcLock.lock();
}
The disassembly for test() is:
_D4test4testFZv:
0000000000000000: 55 push rbp
0000000000000001: 48 8B EC mov rbp,rsp
0000000000000004: 48 8D 0D 00 00 00 lea rcx,[_D4test7GCMutex7__ClassZ]
000000000000000B: 48 83 EC 20 sub rsp,20h
000000000000000F: E8 00 00 00 00 call _d_newclass
0000000000000014: 48 83 C4 20 add rsp,20h
0000000000000018: 48 89 C1 mov rcx,rax
000000000000001B: 48 83 EC 20 sub rsp,20h
000000000000001F: 48 8B 01 mov rax,qword ptr [rcx]
0000000000000022: 48 FF 50 28 call qword ptr [rax+28h]
0000000000000026: 48 83 C4 20 add rsp,20h
000000000000002A: 5D pop rbp
000000000000002B: C3 ret
I.e. the call to lock is still made through the virtual function table although the class GCMutex is declared final.
If the commented code in GCMutex is enabled, the call is deviertualized, but not inlined.