<h1><center> Modernizing Grand Central Dispatch Usage(8) </center></h1> ###### tags: `๐Ÿ’ป WWDC ์Šคํ„ฐ๋””`, `๐Ÿ’ป TIL`, `GCD` ###### date: `2024-01-10T15:12:33.284Z` > [color=#724cd1][name=๋ฐ๋ฆญ] > [Modernizing Grand Central Dispatch Usage - wwdc17](https://developer.apple.com/videos/play/wwdc2017/706/) > WWDC 2017 Session ์ค‘ ํ•˜๋‚˜์ธ `Modernizing Grand Central Dispatch Usage`์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž # ๊ฐœ์š” > 34:05๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘์ด๋‹ค. (7)์—์„œ๋Š” Unified Queue Identity์— ๋Œ€ํ•ด ํ•™์Šตํ–ˆ๋‹ค. ## One Identity to Find Them All > ...and in the kernel bind them ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.06.01](https://hackmd.io/_uploads/ry7NdWhda.png) ```swift let S1 = DispatchSource.makeReadSource( fileDecriptor: fd, queue, EQ ) S1.setEventHanlder { ... } S1.activate() ``` ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.09.02](https://hackmd.io/_uploads/S1YJFbnd6.png) makeResource ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ Source๋ฅผ ์ƒ์„ฑํ•ด์„œ Hanlder, Property ๋“ฑ ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋“ค์„ ์„ค์ •ํ•œ๋‹ค. activate๋œ ์ˆœ๊ฐ„ Utility๋Š” QOS๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Source์— ๋Œ€ํ•œ Hanlder๊ฐ€ ํ•ญ์ƒ ์‹คํ–‰๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. -> Queue Hierarchy์—์„œ ์ƒ์†๋˜๊ธฐ ๋•Œ๋ฌธ ๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ์‹œ์Šคํ…œ์„ ํ†ตํ•ด Handler๊ฐ€ EQ์˜ ์‹คํ–‰ mutual exclusion context์—์„œ ์‹คํ–‰๋  ๊ฒƒ์ž„์„ ์•Œ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.10.53](https://hackmd.io/_uploads/ByDUY-3u6.png) ์ด์ œ ์ข€ ์ „์— ์–˜๊ธฐํ–ˆ๋˜ sync unified indentity๋กœ Source๋ฅผ ๋“ฑ๋กํ•˜๊ฒ ๋‹ค. ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.11.38](https://hackmd.io/_uploads/HyVFYZ2O6.png) ํŠธ๋ฆฌ ์ƒ์— ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง€๋Š” UI QOS Source๋ฅผ ๋ณด๋ฉด, ๋‹ค๋ฃจ๋Š” ๋ฐฉ์‹์€ ์ฒซ ๋ฒˆ์งธ ๊ฒƒ๊ณผ ๋งค์šฐ ๋น„์Šทํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ์—ฌ๊ธฐ์„œ Event Hanlder๋ฅผ ์„ค์ •ํ•  ๋•Œ ์‹ค์ œ๋กœ ์›ํ•˜๋Š” QOS๋ฅผ ์ง€์ •ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ``` If we look at the higher UI QOS source that we have on the tree, the way we treat it is very similar to the first one, except that when you're setting the event handler here you're specifying the QOS that you actually want. ``` ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.15.20](https://hackmd.io/_uploads/SymwcWn_6.png) activate ๋  ๋•Œ ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ•˜๋Š” ์ง€ ๋ณด์ž. ์ด ๋•Œ๋Š” ์Šค๋ƒ…์ƒท์„ ์ฐ์„ ๋•Œ์ธ๋ฐ, ์ด์ „ ๊ณ„์ธต ๊ตฌ์กฐ์—์„œ Utility QOS๋ฅผ ์–ป์—ˆ์„ ๋–„์™€ ๋‹ฌ๋ฆฌ ์—ฌ๊ธฐ์„œ๋Š” Hint(?)๋ฅผ ์–ป๋Š”๋‹ค. ## Too Much of a Good Thing - Repeatedly waiting for exclusive access to contended resources - Repeatedly switching between independent operations - Repeatedly bouncing an operation between threads ์ด์ „์— ์„ค์ •ํ•œ ๋‚ด์šฉ์€ ๋™์ผํ•œ execution context์—์„œ ๋‘ ์†Œ์Šค๋ฅผ ๋ชจ๋‘ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปค๋„์—์„œ ๋™์ผํ•œ unified identity๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๋ฒˆ์งธ Source๋ฅผ ๋‹ค์‹œ ๋“ฑ๋กํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์‹ค์ œ๋กœ ์šฐ๋ฆฌ๊ฐ€ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋งค์šฐ ๋ณต์žกํ•œ identity๋Š” ์ด์ „์— ๋ฐฐํฌํ•œ ๋ฒ„์ „์—์„œ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๋กœ, old thread์—์„œ ๋ฒ—์–ด๋‚˜๋Š” ๋ฌธ์ œ๋ฅผ ๋งํ•œ๋‹ค. ## Without Unified Identity > In macOS Sierra and iOS 10 ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.21.02](https://hackmd.io/_uploads/rku3iWhda.png) ## Leveraging Ownership and Unified Identity ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.22.09](https://hackmd.io/_uploads/Skol2-hOp.png) -> ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ์Šค๋ ˆ๋“œ ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.23.07](https://hackmd.io/_uploads/HkSVnbnda.png) -> iOS 11๋ถ€ํ„ฐ ํ•ด๊ฒฒ ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.24.38](https://hackmd.io/_uploads/Byeqnbhua.png) ์Šค๋ ˆ๋“œ๊ฐ€ EQ๋กœ ๋ถˆ๋ฆฌ๊ฒŒ ๋ฐ”๋€Œ์—ˆ๋‹ค. Unified Identity์˜ ํ•ต์‹ฌ์ธ๋ฐ ์Šค๋ ˆ๋“œ์™€ EQ๊ฐ€ ๋™์ผํ•œ ๊ฐ์ฒด๋กœ ๋˜์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปค๋„์€ ์‹ค์ œ๋กœ ํ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๊ณ , CPU ํŠธ๋ž™์— ๋ฐ˜์˜๋œ๋‹ค. ๋” ์ด์ƒ ์ด๋ฒคํŠธ๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š๊ณ  queue๊ฐ€ ์‹คํ–‰๋  ๋ฟ์ด๋‹ค.. ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.26.36](https://hackmd.io/_uploads/Hyvb6Whd6.png) ์ด ์ด๋ฏธ์ง€๋Š” ์Šค๋ ˆ๋“œ์— ๋ณด๋ฅ˜ ์ค‘์ธ ์ด๋ฒคํŠธ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ‘œ์‹œํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ ์ ˆํ•œ ์‹œ์ ์ด ๋˜๋ฉด ์ฒซ ๋ฒˆ์งธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์™„๋ฃŒ๋œ ์งํ›„ ์ด๋ฒคํŠธ๋ฅผ ๋Œ€๊ธฐ์—ด์—์„œ ์ œ๊ฑฐํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปค๋„์—์„œ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ดํŽด๋ณด๊ณ  ํ•ด๋‹น ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๊ณ„์ธต ๊ตฌ์กฐ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์™œ ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ์ง„ํ–‰๋ ๊นŒ? -> ์ด๊ฑธ ํ†ตํ•ด ๋Ÿฐํƒ€์ž„ ๋™์ž‘์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. **The runtime uses every possible hint to optimize behavior** ## Modernizing Existing Code - No dispatch object mutation after activation - Project your target queue hierarchy ### No Mutation Past Activation > Set the properties of inactivate objects before activation - Source handlers - Target queues Activation์ดํ›„์— mutation์ด ์—†๋‹ค๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ dispatch ๊ฐ์ฒด์— ์–ด๋–ค ์ข…๋ฅ˜์˜ property๊ฐ€ ์žˆ์„ ๋•Œ ์ด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ activate ๋˜์ž๋งˆ์ž ํ•ด๋‹น property์˜ mutating์„ ์ค‘์ง€ํ•ด์•ผ ํ•จ์„ ๋งํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•œ๋‹ค. ```swift let mySource = DispatchSource.makeReadSource( fileDesriptor: fd, queue: myQueue ) mySource.setEventHandler(qos: .userInteractive { ... } mySource.setCancelHandler { close(fd) } mySource.activate() ``` `mySource.activate()`์ด ํ›„์—๋Š” ๊ฐ์ฒด์˜ mutating์„ ์ค‘๋‹จํ•˜๋ผ ๊ทธ ๋ง์ด๋‹ค! ![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-01-10 21.33.43](https://hackmd.io/_uploads/Syz2CW2ua.png) -> ๋ฌธ์ œ์˜ ์›์ธ์ด ๋œ๋‹ค. activate ์‹œ๊ฐ„์— property์˜ ์Šค๋ƒ…์ƒท์„ ์ฐ๊ณ  ๋‚˜์ค‘์— ํ•ด๋‹น ์Šค๋ƒ…์ƒท์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒฐ์ •์„ ๋‚ด๋ฆด ๊ฒƒ์ด๊ธฐ ๋–„๋ฌธ์ด๋‹ค. ### Effects of Queue Graph Mutation - Priority and onwership snapshots can become stale - Defeats priority inversion avoidance - Defeats direct handoff opimization - Defeats event delivery optimization - System frameworks may create sources on your behalf - XPC connections are like sources Target Queue Hierarchy๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ํ•ด๋‹น ์Šค๋ƒ…์ƒท์ด ๋ถ€์‹คํ•˜๊ฒŒ ๋ Œ๋”๋ง๋˜์–ด GCD์˜ ์šฐ์„ ์ˆœ์œ„ ์—ญ์ „ ๋ฐฉ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜(priority inversion avoidanace algorithm), Dispatch sync๋ฅผ ์œ„ํ•œ ์ง์ ‘์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์€ ๋งค์šฐ ์ค‘์š”ํ•œ ์ตœ์ ํ™”๊ฐ€ ๋ฌดํšจํ™”๋œ๋‹ค. 43:02