Method Dispatch in Swift | Static | Dynamic | Message

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my channel i code i am pallav and here on this channel i upload videos related to ios programming every sunday today it's about dispatch methods in swift you might have heard about static dispatch dynamic dispatch differences between them comparison and performance front etc and today we are going to have a detailed look at all of them by the end of the video you will not only have a good knowledge about the dispatch methods in swift but answers to many interview questions also so let's have a look we will first understand that what is a dispatch method what are its types what kind of dispatch method swift uses and how can we play with them and then we will bring theory to the implementation by writing a test case to measure the performance of static dispatch and dynamic dispatch so let's start from very basic how does a program execute when we write some code and compile it compiler turns the code written in high level language to machine level language or what we call as executable code and as a part of this process compiler tries to keep a track of the location of the functions that we are using in our code to do so compiler uses an array of function to pointers this array is also known as witness table before proceeding further let's first understand what is method dispatch so dispatch means sending sending something somewhere try to relate this to method dispatch method dispatch tells your application where to find the method in the memory before it gets executed in cpu it is about how a program selects which instructions to execute when invoking a method and it happens every time when we call a method though we pay very less attention to it but by the end of the video when you will see the difference in the performance we will give it a thought to optimize the performance we have got different types of dispatch methods like table dispatch direct dispatch or message dispatch and we will look at each of them but let's first see this video it will help us understanding the concept mr roy runs an i.t company and he got projects from two firms x and y offices of both the firms are situated in the same mall he asks his two employees jon and sam to go to these firms and collect the requirements jon for x and sam for y john was knowing the location of x so he directly goes there and collects the requirements sam was not knowing the location so he first goes to reception asks for the location of why and then goes there for requirement gathering jon was fast and comparatively sam was slow what john did was a direct dispatch because he knew the location of the shop he directly went to the shop and did the job it was pretty fast what sam did was a table dispatch he was not knowing about the location of the shop so he searched it first in the map or asked the reception about it and then went to the shop so john was faster than sam or should i say that direct dispatch is faster than table dispatch now let's understand these terms in detail direct dispatch or static dispatch is the fastest dispatch method of all because compiler is already aware about the location of the function it directly executes it in fact compiler also makes some tweaks for the optimization but this is only possible in those scenarios where there is no possibility of method being changed override i mean so in other words static dispatch or direct dispatch is only possible in those scenarios where there is no inheritance so we can conclude it as the value type data structures are having the static dispatch or direct dispatch so if we compare between classes and destructs instructs are having the static dispatch so the downside is static dispatch restricts subclassing but the bright side is its execution is very fast now comes table dispatch or dynamic dispatch this happens where subclassing or inheritance is possible so for reference type data structures for example class compiler maintains an array or what we call as witness table of function pointers and whenever a method is invoked its address is searched in the witness table and then the invocation happens whenever subclassing happens a copy of this witness table is made for the subclass and the newly added methods by subclass is appended in this witness table in case subclass overrides the method of superclass the witness table for the child class is updated with the new location of the function assume that this is the witness table for a parent class having two methods method one and method two this is the address for method one and this is the address for method two now some class subclasses the parent class say child class so this entire witness table will be replicated for the child class method 1 and method 2 and because method 2 is being overridden by child class the address for method 2 is changed here the method 3 was the method added by child class and hence it was appended in the witness table of the child class this was just to give you an idea of how table dispatch works the downside is it is comparatively slower than the direct dispatch or static dispatch but the bright side is it allows subclassing hence it is not as restrictive as direct dispatch the third type of dispatch is message dispatch it is even more dynamic than the table dispatch because it provides the flexibility of changing the dispatch behavior at the runtime i know that it is a bit complex to understand but you can relate it with method swizzling method swizzling is possible only because of method dispatch the flexibility to change the dispatch behavior at runtime in fact the major players of cocoa like kvu and codata are working on message dispatch only this is how a method dispatch works if you notice there is no offset for functions if you remember that there was a function offset in case of table dispatch this one because of this offset it was easy to find the address of the function that is to be invoked but in case of message dispatch there is no function offset so the entire class hierarchy is crawled to determine which function is to be executed it is slowest of all but most dynamic of all now we have an understanding of dispatch methods so let's quickly look that how swift decides which dispatch method is to be used and when there are few factors on which you have decides which dispatch method is to be used and the first one is location whether the method has been declared in the initial declaration or any of its extension so if the data structure that is being used if it is value type then irrespective of the initial declaration or the extension the dispatch method that will be used will be static or direct if it is a reference based type for example class in the initial declaration it will be a table dispatch but in the extension it will be a static dispatch and if it is a protocol then the same concept as that of reference type or class that will be applied that in the initial declaration the dispatch method would be table and in the extension it will be static so the crux is whenever a method is declared in the extension it will have the static dispatch type the second factor is mentioning of the keywords so if you have explicitly declared your class as final then the static dispatch will be used because once you have declared it as final it can't be inherited so once you have restricted the subclassing static dispatch will be used second keyword dynamic so if you declare any of your methods with the keyword dynamic message dispatch will be used it moves it to the objectives runtime and then the message dispatch is used you would be thinking that all this theory is fine we got it that what is static dispatch what is dynamic dispatch we agreed that static dispatch is fast dynamic dispatch is slow and we got the logical reasoning to but what is the proof how can we confirm it so let's dive into the code so here i have a class sam and a class john as we saw in our example and we assume that john is working in the fashion of static dispatch and sam is working in the fashion of dynamic dispatch so i have declared this class with final keyword to make it use of the static dispatch they are having nothing else but this method do the job and there's one print statement saying job done we have one more class named dispatch method tester where we are doing nothing but just executing these two functions say test john and test sam and we are just calling their respective methods on their respective objects so let's move to our test cases and here i am having these three objects the one is sut what we call is system under test that this is my dispatch method tester the class that we made for testing and the other two objects for sam and john and the setup method i have done nothing much but just initialize these three objects and in this method test performance example where we will be mentioning the performance of static dispatch and dynamic dispatch these two lines are commented as of now so what i'll do is i'll just uncomment the first line and we'll run the test case for test sam and sam to just to reconfirm sam will be using the dynamic dispatch the table dispatch so let's run the test cases and here i'll just click on this cutter okay so it shows that it is 0.00 second and what i'll do is that i'll set a baseline will set a criteria for comparing the performance so i'll edit it and accept it this was and now i run the test case for john let's see the difference in the performance so as you can see it says that it is 64 percent better i agree that we had nothing in our methods but just a print statement but still this 64 percent is a huge difference and even in the methods having a lot of code this will reduce but it might go down up to 15 20 but even that is a significant percentage so what we just confirmed is that the static dispatch the direct dispatch is way more faster than the dynamic dispatch and we just measured the performance of the static dispatch versus the dynamic dispatch if you want to know more about the dispatch methods if you want to go into the detail i'll put some links in the description they are really good articles i got the inspiration of making this video after reading one of them so go through them if you want to go into the detail that's pretty much for this video a new video comes out every sunday so stay tuned
Info
Channel: iCode
Views: 2,947
Rating: 4.9490447 out of 5
Keywords: iOS, Swift, Method Dispatch, Dispatch Method, Static Dispatch, Dynamic Dispatch, Message Dispatch, Table Dispatch, Direct Dispatch, struct vs class, witness table, vtable, performance in Swift, Method Dispatch in Swift, Objective C, final keyword, dynamic keyword, final class, Interview Question, iOS Interview, Swift Interview
Id: Qbam_n4-ebg
Channel Id: undefined
Length: 11min 38sec (698 seconds)
Published: Sat Apr 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.