Upgrade to Pro — share decks privately, control downloads, hide ads and more …

@_specialized なお話し

53e469a19bcb4584c87789d237128ca0?s=47 matuyuji
December 26, 2016

@_specialized なお話し

Swift の @_specialized について

53e469a19bcb4584c87789d237128ca0?s=128

matuyuji

December 26, 2016
Tweet

Transcript

  1. ͪΐͬͱಛผͳ͓࿩ ؔ੢ϞόΠϧΞϓϦݚڀձ  !NBUVZVKJ

  2. @matuyuji safx-dev.blogspot.jp

  3. None
  4. !@TQFDJBMJ[F

  5. None
  6. 6TF!@TQFDJBMJ[FUP EJSFDUUIFDPNQJMFSUP TQFDJBMJ[FHFOFSJDT

  7. !@TQFDJBMJ[F public class Game<T : Pingable> : Playable { var

    t : T public init (_ v : T) {t = v} @_specialize(Int) public func play() { for _ in 0...100_000_000 { t = t.ping() } } }
  8. 20x faster

  9. public protocol Pingable { func ping() -> Self } public

    protocol Playable { func play() } extension Int : Pingable { public func ping() -> Int { return self + 1 } } public class Game<T : Pingable> : Playable { var t : T public init (_ v : T) {t = v} @_specialize(Int) public func play() { for _ in 0...100_000_000 { t = t.ping() } } } Game(10).play() Application.swift Framework.swift
  10. !@TQFDJBMJ[F w @_specialize͸ίϯύΠϥ΁ͷώϯτ w ࢦఆͨ͠ܕͰಛԽͰ͖ΔίʔυΛੜ੒ w ܕνΣοΫͯ͠Ұகͨ͠ͱ͖ʹ͸ɺಛԽͨ͠ ίʔυΛ࢖͏Α͏ʹ͍ͯ͠Δ

  11. $ diff -u =(nm -naj Framework.build/Framework.swift.o) \ =(nm -naj FrameworkSp.build/Framework.swift.o)

    $ diff -u =(nm -na Framework.build/Framework.swift.o | head -n 25) \ =(nm -na FrameworkSp.build/Framework.swift.o | head -n 25 ) \ | swift demangle --- /tmp/zshPdqiBd 2016-12-26 16:11:46.000000000 +0900 +++ /tmp/zshdhTiwz 2016-12-26 16:11:46.000000000 +0900 @@ -21,5 +21,5 @@ 00000000000000e0 T _type metadata accessor for Framework.Game 0000000000000110 T _rt_swift_allocObject 0000000000000120 T _Framework.Game.play () -> () -0000000000000220 T _Framework.Game.__deallocating_deinit -0000000000000260 T _Framework.Game.deinit +0000000000000270 T _Framework.Game.__deallocating_deinit +00000000000002b0 T _Framework.Game.deinit
  12. 8IPMF.PEVMF 0QUJNJ[BUJPO w ίϯύΠϥ͸ࣗಈͰܕಛԽͨ͠ίʔυΛੜ੒ w ௨ৗ͸ಉ͡ϑΝΠϧ্ʹ͋Δඇopenͳ΋ͷ w -whole-module-optimization -wmo 

    Ͱɺಉ͡Ϟδϡʔϧ্ʹ͋Δඇopenͳ΋ͷ
  13. 3FGFSFODFT w 8SJUJOH)JHI1FSGPSNBODF4XJGU$PEF
 IUUQTHJUIVCDPNBQQMFTXJGUCMPCNBTUFSEPDT0QUJNJ[BUJPO5JQTSTU w 8IPMF.PEVMF0QUJNJ[BUJPOJO4XJGU
 IUUQTTXJGUPSHCMPHXIPMFNPEVMFPQUJNJ[BUJPOT w TBGY4XJGU4QFDJBMJ[F4BNQMF