final class performance


(Fabian Ehrentraud) #1

Somewhere I read that we should always write "final class" where possible, as it is more performant. Is this really the case, especially for non-library modules? Can't the compiler infer whether a class has no subclasses when compiling? Writing "final" just for performance reasons feels wrong.

-- Fabian


(Alex Hoppen) #2

AFAIK, Swift applies the same optimisations as for final classes if it can guarantee that the class will not be subclassed. That is, if the class is either private or internal and you have whole module optimisation enabled. If your class, however, is public and not final, there is no way the compiler can assure that it will not be subclassed in a different module and thus cannot perform optimisations like inlining method implementations.

– Alex

···

On 23 Jun 2016, at 09:16, Fabian Ehrentraud via swift-evolution <swift-evolution@swift.org> wrote:

Somewhere I read that we should always write "final class" where possible, as it is more performant. Is this really the case, especially for non-library modules? Can't the compiler infer whether a class has no subclasses when compiling? Writing "final" just for performance reasons feels wrong.

-- Fabian
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution