This is the first time I write a blog and I am not a native English speaker. So you are welcome to point out anything that not good enough.
At the beginning I would like to recommend you to watch this great WWDC video.
There is an example the apple engineers made about photoshop. They said that after changing the person's shirt color,
Even though I have two distinct documents, the old state and the new state, the only new data that I have had to consume as a result of that is the tiles contained in this person's shirt.
These is really amazing at first glance. But can we really just create a bunch of tile object and append them to an array then expect every time we change a few tiles , the only thing get copied is the tiles we changed ?
Unfortunately the little experiment I made proved that is actually not that easy. My code is below:
var array1 = [Int]()
for i in 0 ... 9999999 {
array1.append(i)
}
var array2 = array1
//array2[0] = 1
print("done")
And we can set up a breakpoint at print("done")
As we can see, it consume 80.6 MB now.
Let's remove the comment and try again.
Now we can see the memory usage has grow to 156.9MB , which is roughly twice of 80.6MB (because our array is not all the thing that can consume memory.)
Actually this make sense because if we are going to achieve the goal that coping only changed tiles. We need to put pointers in the array. And because now nearly all device are 64bit which means the address is 64bit and we need to take care of it's release, so we need to use some bits (Assumedly 32 bits) for reference counting. It gonna consume more than twice the memory anyway even before we change the array2, so maybe swift automatically optimized that?
So let make the tile larger by replacing Int by
struct Test {
var a: Int = 1
var b: Int = 2
var c: Int = 3
var d: Int = 4
var e: Int = 5
var f: Int = 6
var g: Int = 7
var h: Int = 8
var i: Int = 9
var j: Int = 10
var k: Int = 11
var l: Int = 12
var m: Int = 13
var n: Int = 14
var o: Int = 15
var p: Int = 16
var q: Int = 17
var r: Int = 18
var s: Int = 19
var t: Int = 20
var u: Int = 21
var v: Int = 22
var w: Int = 23
var x: Int = 24
var y: Int = 25
var z: Int = 26
}
That consume 203.7MB memory.
And then remove the comment:
That consume 402.1MB memory. They still copy the whole array.
__So is there no way out ? __
__Actually, there is. __
When I said pointers and reference counting, some of you may thought isn's that a class? Yeah, arrays save class object's reference (which is actually pointer) and the actually object the pointer point to is at least compose of reference count and actual content, there is also isa pointer for Swift class for the sake of interacting with OC.
So let's change the struct to class.
At first these consume a little more than struct version. Which is trivial because the class is large enough so we can near ignore memory overhead of class.
Then we remove the comment.
It only grow to 236.6MB, which is a significant improvement. And we can make this class immutable so it can have value semantic. Furthermore we can even wrap this class in a struct and implement copy-on-write behavior introduced by the video I mentioned at the beginning, so you need not to invoke the super long init method just to change a property (image the pain when we just want to get a copy of CGRect whose x is different from the existing one ).
Is this the end of the story?
No, I thought it would be a lot slower because the class in array has to be allocated one by one while struct can be allocated by piles thanks to exponential growth strategy
of Array. And when I use the allocation Instrument to profile, it seems to be true.
But when I removed allocation Instrument , the result is completely different.
It seems to have been optimized!
So the conclusion is: