Last active
March 2, 2018 01:29
-
-
Save omochi/07dc59b0ab616d382b5530f8ea6991ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swiftc a.swift -emit-sil -O > ao.sil | |
protocol Animal { | |
func speak() | |
} | |
func callSpeak<T: Animal>(_ t: T) { | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
t.speak() | |
} | |
struct Cat : Animal { | |
func speak() { | |
print("nya") | |
} | |
} | |
final class SpeakInvoker<T: Animal> { | |
let animal: Animal | |
init(animal: Animal) { | |
self.animal = animal | |
} | |
func invokeSpeak() { | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
animal.speak() | |
} | |
} | |
let cat = Cat() | |
callSpeak(cat) | |
let invoker = SpeakInvoker<Cat>(animal: cat) | |
invoker.invokeSpeak() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.speakの呼び出しが多いとspecializeされる → ディスパッチ回数が多いなら特殊化の恩恵が大きいから、という考え方?
struct, final classだとspecializeされる、classだとされない → classの場合、どうせvtableがあるから無駄?2段を1段にする効果はありそうだけどな・・・?