Bug 24332 – Improve downcast to final classes

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

Comments

Comment #0 by bugzilla — 2024-01-11T22:56:46Z
deadalnix writes: class A {} final class B : A {} auto foo(A a) { return cast(B) a; } This generates a call into the runtime, making things completely opaque to the optimizer. The algorithm used by the runtime is linear. But there is a dumb constant time solution. because B is final, a is either an instance of B, or it is not (as in, it cannot be an instance of a subclass of B). Therefore, we expect the codegen to look like the following instead: auto foo(A a) { return typeid(a) is typeid(B) ? a : null; } This obviously won't compile but you get the idea. Not only this is constant time, but very simple too, and visible to the optimizer, which means the check can be folded by the opitimizer after other transforms, for instance inlining. https://forum.dlang.org/post/[email protected] Here we go: https://github.com/snazzy-d/sdc/blob/master/test/llvm/downcast.d Except the interface part.
Comment #1 by bugzilla — 2024-01-13T00:12:57Z
See https://github.com/dlang/dmd/pull/16027 for partial implementation (without inlining)
Comment #2 by robert.schadek — 2024-12-13T19:32:31Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20380 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB