Copy on Write in Swift | iOS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my channel i code i am pallav and on this channel i upload videos related to ios programming every sunday and today we are going to see a beautiful concept copy and write this is one of those interview questions which is generally asked as a follow-up question after the questions like classes versus structs or reference type versus value type this question generally pops up as a follow-up question so today we will be looking at what is copy and write why is it needed copy and write in action so for this we will be writing some piece of code to actually verify that how copy and write works then we will see some inbuilt types by swift which are having copy and write implemented for them and then later in this video we will implement copy and write for our custom objects we will be writing code for this so let's understand that what is copy and write copy and write simply says that if two variables are supposed to hold exactly same values then both of them should be pointing at the same object unless mutation is needed as an example assume that two friends live in a flat and they have only one tv now till the time they are watching the same channel same program they can watch it together they need a different tv only when one of them wants to see a different program and in programming context we can say that when an object is created and assigned to a variable and then this variable is assigned to a different variable both of these variables will be pointing to the same object unless the mutation is required so here both a and b are pointing to the same memory location they are pointing to the same object having the values 1 and 2. now a copy of this object will be created only when a write operation is needed so when a ask for appending the element 3 a copy is created so precisely we are avoiding to make the copy of this object unless a write operation is required this is and if the question is that why is it needed the obvious answer is to enhance the performance so just like buying another tv till the time it is needed was a wastage of money in the same way making of another object till the time it is actually required till the time a right operation or mutation is needed it is the wastage of the memory so swift optimize this by delaying the making of copies unless they are actually required and now let's see this in action so for that i'm already having a saved code snippet for demonstrating copy and write so let's use it here we are having an array having two elements one and two we assign this array to array 1 and then we assign array 1 to array 2 so we are having two variables and as per the concept of copy and write we are assuming that both of these variables arr1 and array 2 should point at the same memory location should point at the same object because till this point no write operation has been performed and to verify this we are using this method with unsafe buffer pointer so this method takes a closure and returns a pointer to which that variable is pointing and here in the closure we are just printing the address and we are doing this for both array1 and array2 to verify that whether they are pointing to the same object or a new object has been created so let's give it a run and as we can see in the logs both of these values are exactly same so they are pointing at the same object which means that copy and write has been implemented for array now swift has implemented the copy and write for arrays and dictionaries but not for the custom objects so let's verify that by another piece of code and for that let's create a struct so essentially we are creating a custom object so let's say some struct and let's just put a name where name type string and to verify this i'm having another save code snippet so let's just use it that is address of struct so this function it takes an unsafe pointer so basically we will be sending the address we will be passing the address of our struct to this method it just converts it into integer and then we are printing it through the format specifier p so let's create the object of this struct substruct and for this i'm having this save code snippet okay so we are creating an object of our struct substruct we assigned it to struct object one and then assign the extract object went to struct object two just like we did with the array and now we are printing the address of both struct object one and instead object two or we can say that we are printing the address of the object to whom struct object one and direct object two are pointing now let us give it a run and we see that unlike the arrays these addresses these values are not same so this means that even if we have assigned struct object venture struct object 2 and have performed no write operation yet there are two different objects existing in the memory so this is a kind of wastage of the memory and this should be optimized just like the swift has done for arrays and dictionaries let's try to write code for it for implementing copy and write on our custom objects we'll use the code suggested by apple so that is in the official documentation of the of the swift on that is available on github i'll put the link of it in the description so we'll be using it and i'm having that as as one of this my saved code snippets i'll just use it over here okay so let's first look at the code that what are we having we have a class named ref and generics have been implemented here so it will just hold any object that will be passed to it say d and then we have this struct named box here also we have implemented the generic so we will be passing or any custom object to this and this is having a reference of this class ref so whenever a read operation will be performed or essentially this get block will be executed the value of our struct stored as this ref will be returned and whenever a write operation will be performed or essentially the set block will be executed we will first check that whether we are having any strong references for that object if yes we will create a new object and will return it otherwise we will return the same object so through this we are delaying the making of the copies just like swift is doing for arrays and you can find this code on this github documentation of swift by apple here if you search box you'll find this code so i just took it from here i'll put the link in the description and now let's try and use it so instead of using directly struct obj one and struct obj2 here let's do one thing and then instead of this let's make an object of our box and let's pass our object our custom object here now we want to see that whether struct object 1 and struct object 2 are pointing at the same address or not so for that let's do one thing that let's print the address of the values that that it is holding and let's give it a run and now we see that we are having the same values which was not the case earlier so precisely both of these variables are pointing to the same object a new object has not been created because the write operation is not performed yet so essentially we have implemented the copy and write for our custom objects so that's pretty much for this video a new video comes out every sunday so stay tuned and stay safe let's write better code together happy coding
Info
Channel: iCode
Views: 1,710
Rating: undefined out of 5
Keywords: swift, ios, optimisation, copy on write, copy on write for custom object, swift interview question, iOS interview question, iOS app development, memory managment, memory managment in swift, copy in swift, iOS interview, swift interview, latest swift interview, iOS interview 2021, struct vs classes, value type vs reference type
Id: MnemjnAGse4
Channel Id: undefined
Length: 8min 11sec (491 seconds)
Published: Sat Apr 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.