Bug 24335 – Class Downcast

Status
NEW
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-01-12T01:37:25Z
Last change time
2024-12-13T19:32:34Z
Keywords
industry, performance
Assigned to
No Owner
Creator
Walter Bright
See also
https://issues.dlang.org/show_bug.cgi?id=24332
Moved to GitHub: dmd#20382 →

Comments

Comment #0 by bugzilla — 2024-01-12T01:37:25Z
deadalnix writes: Currently, we use a linear algorithm to downcast and we reach this algorithm via a call int he runtime. This prevent the optimizer from doing anything about it, in addition to be slow. The problem is similar to 1/. The whole check can be made trivial if we let the compiler prebake some data for us, namely an array of primary parents. Let's see what it looks like in the first example, when b is not final. class A {} class B : A {} auto foo(A a) { return cast(B) a; } Which we can do as follow: auto foo(A a) { // The primaries generated by the compiler for us. auto tidA = typeid(A); assert(tidA.primaries = [tidA]); auto tidB = typeid(B); assert(tidB.primaries = [tidA, tidB]); auto t = typeid(a); auto depth = tidB.primaries.length - 1; if (t.primary.length <= depth) { return null; } if (t.primary[depth] == tidB) { return a; } return null; } This is starting to look good, now we have general downcast in a form that is not only really fast to perform, but also completely transparent to the optimizer. https://forum.dlang.org/post/[email protected]
Comment #1 by bugzilla — 2024-01-16T05:08:35Z
Partial implementation: https://github.com/dlang/dmd/pull/16040 Does not include the table.
Comment #2 by robert.schadek — 2024-12-13T19:32:34Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20382 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB