Exploring Swift Array

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")

single array memory usage

As we can see, it consume 80.6 MB now.
Let's remove the comment and try again.

two array memory usage

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
}
single array memory usage

That consume 203.7MB memory.
And then remove the comment:

two array memory usage

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.

single class array memory usage

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.

two class array memory usage

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.

Profile data

But when I removed allocation Instrument , the result is completely different.

Profile data2

It seems to have been optimized!
So the conclusion is:

Swift is the best programming language ever!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子羞福,更是在濱河造成了極大的恐慌第租,老刑警劉巖彪笼,帶你破解...
    沈念sama閱讀 211,948評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拷获,死亡現(xiàn)場(chǎng)離奇詭異待榔,居然都是意外死亡律想,警方通過(guò)查閱死者的電腦和手機(jī)猎莲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,371評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)技即,“玉大人著洼,你說(shuō)我怎么就攤上這事《穑” “怎么了身笤?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,490評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)葵陵。 經(jīng)常有香客問(wèn)我液荸,道長(zhǎng),這世上最難降的妖魔是什么脱篙? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,521評(píng)論 1 284
  • 正文 為了忘掉前任娇钱,我火速辦了婚禮,結(jié)果婚禮上绊困,老公的妹妹穿的比我還像新娘文搂。我一直安慰自己,他們只是感情好秤朗,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,627評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布煤蹭。 她就那樣靜靜地躺著,像睡著了一般取视。 火紅的嫁衣襯著肌膚如雪硝皂。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,842評(píng)論 1 290
  • 那天作谭,我揣著相機(jī)與錄音吧彪,去河邊找鬼。 笑死丢早,一個(gè)胖子當(dāng)著我的面吹牛姨裸,可吹牛的內(nèi)容都是我干的秧倾。 我是一名探鬼主播,決...
    沈念sama閱讀 38,997評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼傀缩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼那先!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起赡艰,我...
    開(kāi)封第一講書(shū)人閱讀 37,741評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤售淡,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后慷垮,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體揖闸,經(jīng)...
    沈念sama閱讀 44,203評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,534評(píng)論 2 327
  • 正文 我和宋清朗相戀三年料身,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了汤纸。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,673評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡芹血,死狀恐怖贮泞,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情幔烛,我是刑警寧澤啃擦,帶...
    沈念sama閱讀 34,339評(píng)論 4 330
  • 正文 年R本政府宣布,位于F島的核電站饿悬,受9級(jí)特大地震影響令蛉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜狡恬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,955評(píng)論 3 313
  • 文/蒙蒙 一言询、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧傲宜,春花似錦、人聲如沸夫啊。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,770評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)撇眯。三九已至报嵌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間熊榛,已是汗流浹背锚国。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,000評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留玄坦,地道東北人血筑。 一個(gè)月前我還...
    沈念sama閱讀 46,394評(píng)論 2 360
  • 正文 我出身青樓绘沉,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親豺总。 傳聞我的和親對(duì)象是個(gè)殘疾皇子车伞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,562評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容