A glimpse of my talk at SwiftBengaluru Meetup organised at WeWork, EGL, Bangalore.
The learning and take-aways are very simple and easy to engrain in our programming practices.
should be invoked in response to a message.” Keeping this definition here just as my respect towards people who have work done this before. You can ignore this complex definition.
about identifying method implementations The idea in dispatch is following some function pointer to see which method to actually invoke, or object to invoke it on. "Binding" is the idea that the method is "bound" to a particular instance (or class of instances) & that's how you identify it. Binding vs Dispatch
to execute when invoking a method. It’s something that happens every time a method is called, and not something that you tend to think a lot about. - Static - Table - Message
fewest number of assembly instructions. - The compiler can perform all sorts of smart tricks, like inlining code, inserting proper GOTOs, etc Static Dispatch
the class declaration. - This array is generally referred to as virtual table, dispatch table or vtable - Swift uses the term witness table. Table Dispatch
with a different function pointer for every method that the class has overridden. - As subclasses add new methods to the class, those methods are appended to the end of this array. Subclassing in Table dispatch
of the table with a different function pointer for every method that the class has overridden. - As subclasses add new methods to the class, those methods are appended to the end of this array. class ParentClass { func method1() {} func method2() {} } class ChildClass: ParentClass { override func method2() {} func method3() {} }
techniques like KVO, UIAppearance. - Allows developers to modify the dispatch behavior at runtime (via swizzling) - Object contains a hidden pointer to it’s type. - Types in-turn contain a hidden pointer to their super types. Message Dispatch
the runtime will crawl the class hierarchy to determine which method to invoke. - This is done using an “isa” pointer stored in every class and object - As These are just pointers, you can apply all sort of pointer arithmetic and perform runtime black-magic class ParentClass { func method1() {} func method2() {} } class ChildClass: ParentClass { override func method2() {} func method3() {} }
Swift needs and try to perform as much as work at compile-time. This includes optimizing method dispatch. (Guess why your swift project compilation takes forever.) - Writing descriptive code with proper access-specifier gives the compiler enough information to make assumptions and optimizations (like Devirtualization optimization ) - If you don’t, compiler will still try to go out of its way to verify and optimize the code. Swift - Observe, Improvise, Overcome
by using suitable access specifiers or visibility control. - Use private, fileprivate, final appropriately to restrict the dynamic dispatch when not needed. Extension - Static Dispatch - One of the primary reason we cannot have stored properties in extensions. - Unlike classes/structs, the extension method will sport whatever access-specifier you defined for extension. - So all methods of a public extension will be public (and not internal unlike classes)
to overuse it. dynamic - Makes it visible go Objc runtime AND - Forced to use ObjC Dynamic Table Dispatch - Use only when needed. - Eg: If you want to override methods in extensions. @objc - Makes it visible go Objc runtime - No Effect on dispatch - Don’t hesitate to overuse it. @nonobjc - Makes a Swift declaration unavailable in Objective-C - Enforces Swift Visibility and Access specifiers
generics. - Interface builder is generics-illitrates - Enter Generics - Generics are resolved at compile-time - Type Safe + predictable code = Happy Compiler Inheritance
build is taking on a method level? You can use this Swift flag to find out which methods are taking more time to compile. Under Target > Build Settings > Other Swift Flags -Xfrontend -debug-time-function-bodies Mac App If you are a visual person, you can install the Swift build time Analyzer Mac app that helps you visualize this data. Bonus
are completely abstracted by us. But we can surely take charge and control that with minimal efforts and practice. Don’t pay (in compile time and runtime ) for the dynamism that you don’t even need. Conclusion