[Swift] Method Dispatch (3): Dynamic Dispatch๋ฅผ ์ค์ฌ๋ณด์
Increasing Performance by Reducing Dynamic Dispatch - Swift Blog
Get the latest news and helpful tips on the Swift programming language from the engineers who created it.
developer.apple.com
Dynamic Dispatch
Dynamic Dispatch์ ์ฌ์ฉ์ด ๋ฌด์กฐ๊ฑด ๋์ ๊ฒ์ ์๋๋๋ค. Dynamic Dispatch ๋๋ถ์ ์ฐ๋ฆฌ๋ ์ค๋ฒ๋ผ์ด๋ฉ๋ ํ ์ ์๊ณ , ๋คํ์ฑ๋ ๋ง๋ค์ ์๊ณ ์์๋ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ผ๋๊น์. ํ์ง๋ง ๋ฐ๋์ ํด๋์ค๋ฅผ ์ฌ์ฉํด์ผํ๋ ์ํฉ์์ ๋ ์ด์ ์์ํ ์์ ํด๋์ค๊ฐ ์๋ค๋ฉด ์ด๋จ๊น์?
Dynamic Dispatch๋ ์ค์ ๊ตฌํ์ฝ๋๋ฅผ ์ฐพ์๊ฐ๊ธฐ ์ํด ํ ์ด๋ธ์ ํ์ํด์ผํ๋ ์ค๋ฒํค๋๊ฐ ๋ฐ์ํฉ๋๋ค. ๋ง์ฝ ํด๋์ค์์ ์คํํ ๋ฉ์๋๊ฐ ๋ช ํํ๋ค๋ฉด ๊ตณ์ด Dynamic Dispatch๋ฅผ ํ์ง ์์๋ ๋๊ฒ ์ฃ .
์ ํ ๊ณต์ ๋ธ๋ก๊ทธ์์ ์๋ ค์ฃผ๋ ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ ํ๋ฒ ์์๋ด ์๋ค!
class ParticleModel {
var point = ( 0.0, 0.0 )
var velocity = 100.0
func updatePoint(newPoint: (Double, Double), newVelocity: Double) {
point = newPoint
velocity = newVelocity
}
func update(newP: (Double, Double), newV: Double) {
updatePoint(newP, newVelocity: newV)
}
}
var p = ParticleModel()
for i in stride(from: 0.0, through: 360, by: 1.0) {
p.update((i * sin(i), i), newV:i*1000)
}
์ด ์ฝ๋๋ฅผ ๊ธฐ์ค์ผ๋ก Dynamic Dispatch๋ฅผ ์ค์ฌ๋ณผ๊ฑฐ์์!
๋ฐฉ๋ฒ1: ์ค๋ฒ๋ผ์ด๋ฉ์ด ํ์์๋ค๋ฉด final์ ์ฌ์ฉํ์ธ์!
final ํค์๋๋ฅผ ํด๋์ค, ๋ฉ์๋, ํ๋กํผํฐ์ ์ฌ์ฉํ๋ฉด ๋์์ด ๋ ์ด์ ์ค๋ฒ๋ผ์ด๋ฉ๋ ์ ์๋๋ก ์ ํํฉ๋๋ค. ์ด๋ ๊ฒ ์ ํ๋ ๋์๋ค์ ์ปดํ์ผ๋ฌ๊ฐ ์ฝ๊ฒ ์ด๋ค ๋ฉ์๋๋ ํ๋กํผํฐ๋ฅผ ์ ํํด์ผํ ์ง ๊ฒฐ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ Static Dispatch๋ก ์ฌ์ฉํ ์ ์๊ฒํด์ค๋๋ค.
class ParticleModel {
final var point = ( x: 0.0, y: 0.0 )
final var velocity = 100.0
final func updatePoint(newPoint: (Double, Double), newVelocity: Double) {
point = newPoint
velocity = newVelocity
}
func update(newP: (Double, Double), newV: Double) {
updatePoint(newP, newVelocity: newV)
}
}
์ด์ point, velocity ํ๋กํผํฐ์ updatePoint ๋ฉ์๋๋ final๋ก ์ค๋ฒ๋ผ์ด๋ฉ์ด ์ ํ๋์๊ธฐ ๋๋ฌธ์ Static Dispatch๋ก ๋์ํฉ๋๋ค. ๋ฐ๋ฉด์ update ๋ฉ์๋๋ ๊ทธ๋๋ก Dynamic Dispatch ์ด๊ฒ ์ฃ ? ํ์ ํด๋์ค์์ ์ฌ์ ์ํ๊ฒ๋๋ฉด ์ด๋ค ๋ฉ์๋๋ฅผ ์คํํด์ผํ ์ง ๋ถ๋ถ๋ช ํด์ง๋๊น์.
ํด๋์ค ์ ์ฒด์ final ํค์๋๋ฅผ ๋ถ์ฌ์ค ์๋ ์์ด์.
final class ParticleModel {
var point = ( x: 0.0, y: 0.0 )
var velocity = 100.0
// ...
}
์ด๋ ๊ฒ ํ๋ฉด ์ด ํด๋์ค๋ ์ด์ ์์์ด ์์ ๋ถ๊ฐ๋ฅํด์ง๋๋ค. ๋ฐ๋ผ์ ํด๋์ค ๋ด๋ถ์ ๋ชจ๋ ๋ฉ์๋์ ํ๋กํผํฐ๋ค์ด final์ ๊ฐ์ง๊ฒ๊ณผ ๋์ผํ๊ธฐ ๋๋ฌธ์ ์ด ํด๋์ค์ ๋ํด ๋ฐ์ํ๋ ๋ชจ๋ ํธ์ถ์ Static Dispatch๋ก ๋์ํฉ๋๋ค.
๋ฐฉ๋ฒ 2: private์ ์ฌ์ฉํ์ธ์!
private์ ์ฌ์ฉํ๋ฉด ํด๋์ค์ ์ ์ธ๋ถ ์ธ๋ถ์์๋ ํด๋น ๋ฉ์๋๋ ํ๋กํผํฐ๋ฅผ ๋ณผ ์์กฐ์ฐจ ์์ต๋๋ค. ์ด๋ฐ ๋ฉ์๋๋ ํ๋กํผํฐ๋ค์ ์ปดํ์ผ๋ฌ๊ฐ ์ฝ๊ฒ Static Dispatch๋ฅผ ๊ฒฐ์ ํ๋๋ก ํด์ค๋๋ค.
class ParticleModel {
private var point = ( x: 0.0, y: 0.0 )
private var velocity = 100.0
private func updatePoint(newPoint: (Double, Double), newVelocity: Double) {
point = newPoint
velocity = newVelocity
}
func update(newP: (Double, Double), newV: Double) {
updatePoint(newP, newVelocity: newV)
}
}
์ด๋ ๊ฒ ์ํ๋ ํ๋กํผํฐ๋ ๋ฉ์๋์ private์ ๋ถ์ด๊ฑฐ๋,
private class ParticleModel {
var point = ( x: 0.0, y: 0.0 )
var velocity = 100.0
// ...
}
ํด๋์ค ์ ์ฒด์ private์ ์ง์ ํ ์๋ ์์ด์. ์ด๋ฐ ์ ์ธ์ ํด๋์ค ๋ด๋ถ์ ๋ชจ๋ ํ๋กํผํฐ์ private ์ ๊ทผ ์ ์ด๋ฅผ ๋ถ์ฌํฉ๋๋ค.
๋ฐฉ๋ฒ 3: Whole Module Optimization์ ์ฌ์ฉํ๋ฉด final์ ์ถ๋ก ํ ์ ์์ต๋๋ค.
Whole Module Optimization์ ๋ชจ๋ ์ ์ฒด๋ฅผ ํ๋ฒ์ ์ปดํ์ผํ๊ฒ ๋ฉ๋๋ค. ๋ฐ๋ผ์ ๊ธฐ๋ณธ ์ ๊ทผ์ ์ด์์ธ internal๋ก ์ ์ธ๋ ํ๋กํผํฐ๋ ๋ฉ์๋๋ผ๋ ์ ์ฒด ๋ชจ๋์์ ์ค๋ฒ๋ผ์ด๋ฉ์ด ๋ฐ์ํ๋์ง ์ฝ๊ฒ ํ์ ํ ์ ์๊ฒ ๋ฉ๋๋ค. ์ปดํ์ผ๋ฌ๋ ์ค๋ฒ๋ผ์ด๋ฉ์ด ๋ฐ์ํ์ง ์๋ internal ํ๋กํผํฐ๋ ๋ฉ์๋์ ๋ํด์ final๋ก ์ถ๋ก ํ๊ธฐ ๋๋ฌธ์ Static Dispatch๋ก ๋์ํ๊ฒํ ์ ์์ต๋๋ค.
public class ParticleModel {
var point = ( x: 0.0, y: 0.0 )
var velocity = 100.0
func updatePoint(newPoint: (Double, Double), newVelocity: Double) {
point = newPoint
velocity = newVelocity
}
public func update(newP: (Double, Double), newV: Double) {
updatePoint(newP, newVelocity: newV)
}
}
var p = ParticleModel()
for i in stride(from: 0.0, through: times, by: 1.0) {
p.update((i * sin(i), i), newV:i*1000)
}
์ด ์ฝ๋์์ internal๋ก ์ ์ธ๋ point, velocity, updatePoint๋ ์ด์ ๋ชจ๋ ํ์ผ์ด ํ๋ฒ์ ์ปดํ์ผ๋๋ฉด์ ์ด ๋ฉ์๋์ ํ๋กํผํฐ๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๋ ์์ญ์ด ๋ชจ๋ ๋ด์ ์์์ ์๊ณ final๋ก ์ถ๋ก ๋ฉ๋๋ค. ๋ฐ๋ผ์ Static Dispatch๋ก ๋์ํฉ๋๋ค.
ํ์ง๋ง update ๊ฐ์ ๊ฒฝ์ฐ๋ public ์ผ๋ก ์ ์ธ๋์๊ธฐ ๋๋ฌธ์ ์ธ๋ถ ๋ชจ๋์๋ ์ ๊ทผ์ด ๊ฐ๋ฅํด final๋ก ์ถ๋ก ํ ์ ์์ต๋๋ค.
(๋ผ๊ณ ํด์์ด ๋๋๋ฐ public์ ์ธ๋ถ ๋ชจ๋์์ ์๋ธํด๋์ฑ์ ํ์ฉ์ํ์ง ์๋์..? 3๋ฒ์งธ ๋ฐฉ๋ฒ์ ๋ํด ๋ช ํํ๊ฒ ์์ ๋ค๋ฉด ์๋ ค์ฃผ์ธ์ใ ใ )
๋ง์ง๋ง ๋ฐฉ๋ฒ์ด ์ข ์ฐ์ฐํ๋ค์.. open ์ด๋ผ๋ฉด ๋ช ๋ฐฑํ๊ฒ Dynamic Dispatch์ผํ ๋ฐ..
Why can't Whole Module Optimalization infer final on public non-overridden module classes/methods in the same module?
From this article, I quote the following sentence: Use Whole Module Optimization to infer final on internal declarations. Declarations with internal access (the default if nothing is declared)...
stackoverflow.com
์ฐพ์์ต๋๋ค..ใ ใ ใ
์ ๊ธ์ด ๋์จ๊ฒ Swift2์์ ๋์์ public ์ด open ์ฒ๋ผ ์ฌ์ฉ๋ ๋์๋ค๊ณ ํ๋ค์..! open์ด ์๋ ๋๋ง final๋ก ์ถ๋ก ๋๋ค๊ณ ์๊ฐํ๋ฉด๋ ๊ฒ ๊ฐ์ต๋๋ค! open์ Swift3์์ ์๊ฐ๋์๋ค๊ณ ํฉ๋๋ค.
'๐ ์์ด-์ค-์์ค > ๐ค ์ค์ํํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift-WWDC21] ARC in Swift: Basics and beyond (0) | 2022.01.06 |
---|---|
[Swift] Method Dispatch ๋ฐํ์๋ฃ (2) | 2021.12.28 |
[Swift] Method Dispatch (2): ๋ ๋ณต์กํ ์ํฉ์ Method Dispatch (0) | 2021.12.26 |
[Swift] Method Dispatch (1): Static Dispatch vs. Dynamic Dispatch (1) | 2021.12.26 |
[๋ฉด์ ์ง๋ฌธ] ์ค์ํํธ(Swift) 75๋ฌธ 75๋ต (2) | 2021.12.24 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote