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

class func v. static func

class func v. static func

class func v. static func on Swift4
Presented at Mobile Act NAGOYA #11 ( https://mobileact.connpass.com/event/90198/ )

Yuto Mizutani

June 22, 2018
Tweet

More Decks by Yuto Mizutani

Other Decks in Technology

Transcript

  1. SILͷੜ੒ $ swiftc -emit-silgen class-func.swift > class-func.sil $ swiftc -emit-silgen

    static-func.swift > static-func.sil ΞηϯϒϦੜ੒͸ɼ͜͏ɻ $ swiftc -emit-assembly class-func.swift > class-func.s $ swiftc -emit-assembly static-func.swift > static-func.s
  2. $ diff class-func.sil static-func.sil ※1 @@ -7,8 +7,9 @@ import

    SwiftShims // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): - %2 = metatype $@thick Main.Type // users: %4, %3 - %3 = class_method %2 : $@thick Main.Type, #Main.say!1 : (Main.Type) -> () -> (), $@convention(method) (@thick Main.Typ () // user: %4 + %2 = metatype $@thick Main.Type // user: %4 + // function_ref static Main.say() + %3 = function_ref @_T04main4MainC3sayyyFZ : $@convention(method) (@thick Main.Type) -> () // user: %4 %4 = apply %3(%2) : $@convention(method) (@thick Main.Type) -> () %5 = integer_literal $Builtin.Int32, 0 // user: %6 %6 = struct $Int32 (%5 : $Builtin.Int32) // user: %7 @@ -117,7 +118,6 @@ bb0(%0 : $Main): } // end sil function '_T04main4MainCACycfc' sil_vtable Main { - #Main.say!1: (Main.Type) -> () -> () : _T04main4MainC3sayyyFZ // static Main.say() #Main.init!initializer.1: (Main.Type) -> () -> Main : _T04main4MainCACycfc // Main.init() #Main.deinit!deallocator: _T04main4MainCfD // Main.__deallocating_deinit } ※1: લޙͷίʔυΛදࣔͤ͞ΔͨΊɼdiffʹ͸gitΛར༻͍ͯ͠·͢ɻ
  3. $ diff class-func.sil static-func.sil ※1 @@ -7,8 +7,9 @@ import

    SwiftShims // main sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 { bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>): - %2 = metatype $@thick Main.Type // users: %4, %3 - %3 = class_method %2 : $@thick Main.Type, #Main.say!1 : (Main.Type) -> () -> (), $@convention(method) (@thick Main.Typ () // user: %4 + %2 = metatype $@thick Main.Type // user: %4 + // function_ref static Main.say() + %3 = function_ref @_T04main4MainC3sayyyFZ : $@convention(method) (@thick Main.Type) -> () // user: %4 %4 = apply %3(%2) : $@convention(method) (@thick Main.Type) -> () %5 = integer_literal $Builtin.Int32, 0 // user: %6 %6 = struct $Int32 (%5 : $Builtin.Int32) // user: %7 @@ -117,7 +118,6 @@ bb0(%0 : $Main): } // end sil function '_T04main4MainCACycfc' sil_vtable Main { - #Main.say!1: (Main.Type) -> () -> () : _T04main4MainC3sayyyFZ // static Main.say() #Main.init!initializer.1: (Main.Type) -> () -> Main : _T04main4MainCACycfc // Main.init() #Main.deinit!deallocator: _T04main4MainCfD // Main.__deallocating_deinit } class_method function_ref ※1: લޙͷίʔυΛදࣔͤ͞ΔͨΊɼdiffʹ͸gitΛར༻͍ͯ͠·͢ɻ
  4. ௐ΂ͯΈΔ What is the difference between static func and class

    func in Swift? Swiftͷclass funcͱstatic funcͷҧ͍ static func is same as final class func static func ͸ final class func ͱಉٛ
  5. SILͷੜ੒ $ swiftc -emit-silgen \ final-class-func.swift > final-class-func.sil $ swiftc

    -emit-silgen static-func.swift > static-func.sil ΞηϯϒϦੜ੒͸ɼ͜͏ɻ $ swiftc -emit-assembly \ final-class-func.swift > final-class-func.s $ swiftc -emit-assembly static-func.swift > static-func.s
  6. XcodebuildͰͷϏϧυ $ xcrun swiftc -Onone class-func.swift $ xcrun swiftc -Onone

    final-class-func.swift $ xcrun swiftc -Onone static-func.swift $ xcrun swiftc -wmo -O class-func.swift $ xcrun swiftc -wmo -O final-class-func.swift $ xcrun swiftc -wmo -O static-func.swift ࠷దԽͳ͠ ϦϦʔεϏϧυ
  7. ·ͱΊ • 同一条件 (class内) の場合,動作速度はclass func > static func。※ •

    (class内では) final class funcとstatic funcは同一。 • Xcodebuildを利用すると,コンパイラによりclass funcと static funcは同一のバイナリを出力する。
  8. ࢀߟ ຊεϥΠυ͸ɼQiitaهࣄ class func v. static func in Swift4 ͱಉҰͷ಺༰Ͱ͢ɻ

    What is the difference between static func and class func in Swift? - Stack Overflow Swiftͷclass funcͱstatic funcͷҧ͍ ʲSwiftʳclassͱstaticͷڍಈͷҧ͍Λ੔ཧ͢Δ - Qiita Merge pull request #15151 from ikesyo/stdlib-public-operator-static-func - GitHub apple/swift Swift ؔ਺ͷ࠶ؼݺͼग़͠͸࠷దԽ͞Ε͍ͯΔ͔ LLVM - dev.classmethod.jp Swift Intermediate Language (SIL) - GitHub apple/swift [Swift]ಈతσΟεύονΛݮΒ͢͜ͱͰύϑΥʔϚϯεΛվળ class func vs static func - Reddit Taming Swift compiler bugs Swift࣮ߦϑΝΠϧΛٯΞηϯϒϧͯ͠ɼ࠷దԽ۩߹Λਖ਼֬ʹ೺Ѳ͢Δํ๏ - Qiita Swiftͷfinalʹ͍ͭͯ - Qiita SwiftͷfinalɾprivateɾWhole Module OptimizationΛཧղ͠Dynamic DispatchΛݮΒͯ͠ɼύϑΥʔϚϯεΛ޲্͢Δ - Qiita Increasing Performance by Reducing Dynamic Dispatch - developer.apple.com/swift Writing High-Performance Swift Code - GitHub apple/swift Swift Compiler Performance - GitHub apple/swift vtableͷத਎ΛݟͯΈΔ Ծ૝ؔ਺ςʔϒϧ - wikipedia LLVM Programmer’s Manual ϏϡʔςΟϑϧΞʔΩςΫνϟ RubyͰͷϝλϓϩάϥϛϯάʢಈతσΟεύονͱಈతϝιουʣ