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

Swift Runtime Library

7a4968fbcd56e81f95a4f3c186141b52?s=47 Yuta Saito
December 13, 2019

Swift Runtime Library

7a4968fbcd56e81f95a4f3c186141b52?s=128

Yuta Saito

December 13, 2019
Tweet

Transcript

  1. Swift Runtime Library Θ͍Θ͍swiftc #16 @kateinoigakukun 1

  2. ϥϯλΠϜϥΠϒϥϦͱ͸ Swiftͷಈతͳݴޠ࢓༷Λαϙʔτ͢ΔͨΊͷϥΠϒϥϦɻ ෳࡶͳݴޠػೳ͸ɺૉ๿ʹίϯύΠϧ࣌ʹίʔυੜ੒͢Δͱ ίʔυαΠζͷංେԽ͢ΔͷͰɺڞ௨ϥΠϒϥϦʹ੾Γग़ͯ͠ ͍Δɻ 2

  3. ݴޠ࢓༷ͷ2ͭͷଆ໘ ϓϩάϥϛϯάݴޠͷݴޠ࢓༷Λߟ͑Δͱ͖ɺ2ͭʹ෼ྨͰ͖ Δɻ 1. ੩తͳݴޠ࢓༷ 2. ಈతͳݴޠ࢓༷ 3

  4. 1. ੩తͳݴޠ࢓༷ • จ๏ • ܕγεςϜ 4

  5. 2. ಈతͳݴޠ࢓༷ • ࣮ߦ࣌ܕγεςϜ • ྫ֎ϋϯυϦϯά • όΠφϦޓ׵ੑ 5

  6. Swiftͷಈతͳݴޠ࢓༷ͷྫ • ϝϞϦ֬อ • ARC • ࣮ߦ࣌ͷܕγεςϜ • ಈతΩϟετ •

    etc ... 6
  7. ϝϞϦ֬อ // 24 byte struct Pet { let name: String

    // 16 byte let age: Int // 8 byte } let pet = Pet(name: ..., age: ...) // malloc 24 byte 7
  8. // 8 byte class View { var point: (x: Float,

    y: Float) // 8 byte required init(...) { } func copy() -> Self { return Self(...) // malloc n byte } } // 24 byte class TextView: View { var text: String // 16 byte required init(...) { } } let view: View = TextView(...) // ͜ͷ࣌఺Ͱview͕TextViewͰ͋Δ͜ͱΛอূͰ͖ͳ͍ɻ let anotherView: View = view.copy() 8
  9. ࣮ߦ࣌ܕ৘ใͷඞཁੑ SwiftͷܕγεςϜͷαϒλΠϐϯάʹΑͬͯɺΠϯελϯεͷ ܕ͕੩తʹΘ͔ΔܕͱҰக͢Δͱ͸ݶΒͳ͍ɻ 9

  10. Type Metadata ࣮ߦ࣌ʹऔΓճ͞ΕΔܕ৘ใɻ ͍Ζ͍ΖೖͬͯΔɻ • ܕͷαΠζ • ؔ਺ςʔϒϧ • δΣωϦοΫܕύϥϝʔλ

    • Type Descriptor 10
  11. Type Descriptor δΣωϦοΫͳܕύϥϝʔλʹґଘ͠ͳ͍৘ใΛ࣋ͭɻ ಛఆͷܕΠϯελϯε͔Βಠཱ͍ͯ͠ΔͷͰෳ਺ͷType Metadata͔Βࢀর͞Εͯ࢖͍ճ͞ΕΔɻ 11

  12. σʔλߏ଄ͷςΫχοΫ 12

  13. ΦϒδΣΫτϑΝΠϧͷجຊ • TEXTηάϝϯτ • ࣮ߦ͞ΕΔػցޠ͕֨ೲ͞ΕΔ • ಡΈऔΓઐ༻ • DATAηάϝϯτ •

    άϩʔόϧม਺ͳͲ • ಡΈॻ͖Մೳ 13
  14. TEXTηάϝϯτ͸ಡΈऔΓઐ༻ͳͷͰɺಈతϥΠϒϥϦΛ࢖͏ ͱ͖΍ϓϩηεΛfork͢Δͱ͖ɺෳ਺ͷϓϩηεͰಉ͡ϝϞϦ ۭؒΛڞ༗Ͱ͖Δɻ ͭ·ΓɺΦϒδΣΫτϑΝΠϧͷ઎ΊΔTEXTηάϝϯτ͕ଟ͍ ΄͏͕Α͍ɻ 14

  15. ࠶഑ஔ ࣮ߦՄೳϑΝΠϧʹؚ·ΕΔΞυϨεࢀরΛ࣮ޮΞυϨεʹஔ ׵͢Δॲཧɻ ϦϯΫ࣌ͱϩʔυ࣌ͷ2ճൃੜ͢Δɻ 15

  16. ࠶഑ஔ • ϦϯΫ࣌ ηΫγϣϯͷઌ಄͔Βͷڑ཭΍γϯϘϧಉ࢜ͷΞυϨεͷࠩ Λܭࢉͯ͠ஔ׵͢Δɻ • ϩʔυ࣌ ઈରΞυϨεΛ࣮ࡍͷϝϞϦۭؒͷΞυϨεʹஔ׵͢Δɻ جຊɺϓϩάϥϜ͕ల։͞ΕΔϝϞϦۭؒͷઌ಄ΞυϨεΛ ଍͠߹Θ͍͚ͤͯͩ͘

    16
  17. ࠶഑ஔͷΦʔόʔϔου ϩʔυ࣌ͷ࠶഑ஔ͸ϓϩάϥϜͷىಈ଎౓ʹӨڹͯ͘͠ΔͷͰ গͳ͍΄͏͕Α͍ɻ 17

  18. Relative Pointer ϙΠϯλࣗ਎ͷΞυϨε͔Βର৅ͷΞυϨε·ͰͷΦϑηοτ Λอ࣋͢ΔϙΠϯλܗࣜɻ 18

  19. struct RelativePointer<Pointee> { var offset: Int32 mutating func pointee() ->

    Pointee { withUnsafePointer(to: &self) { [offset] pointer in let base = UnsafeRawPointer(pointer) let advanced = base.advanced(by: Int(offset)) return advanced.assumingMemoryBound(to: Pointee.self) }.pointee } } 19
  20. Relative PointerͷϝϦοτ 1. όΠφϦαΠζͷઅ໿ 2. ϩʔυ࣌࠶഑ஔΛݮΒͤΔ 3. ϝλσʔλΛTEXTηάϝϯτʹؚΊΒΕΔΑ͏ʹͰ͖Δ 20

  21. Indirect Pointer Relative Pointerͱ૊Έ߹Θͤͯ࢖ΘΕ͍ͯΔςΫχοΫͷͻͱ ͭɻ ܕͷΞϥΠϝϯτʹΑͬͯແҙຯʹͳ͍ͬͯΔΞυϨεԼҐbit Λར༻ͯ͠GOTܦ༝ͷϙΠϯλͱ௨ৗͷϙΠϯλΛಉҰͷܕͰ දݱͯ͠Δɻ 21

  22. struct RelativeIndirectablePointer<Pointee> /* where alignof(Pointee) => 2 */ { let

    offsetWithIndirectFlag: Int32 mutating func pointee() -> Pointee { let offset: Int32 if isIndirect { offset = offsetWithIndirectFlag & ~isIndirectMask } else { offset = offsetWithIndirectFlag } return withUnsafePointer(to: &self) { pointer -> UnsafePointer<Pointee> in let rawPointer = UnsafeRawPointer(pointer) var ptr = rawPointer.advanced(by: Int(offset)) if isIndirect { ptr = ptr.load(as: UnsafeRawPointer.self) } return ptr.assumingMemoryBound(to: Pointee.self) }.pointee } var isIndirect: Bool { offsetWithIndirectFlag & isIndirectMask != 0 } var isIndirectMask: Int32 { 0x01 } } 22
  23. Int Paired Pointer ࢖ΘΕͳ͍ԼҐϏοτΛͦͷ··਺஋ͱͯ͠औΓग़͢όʔδϣ ϯɻ 23

  24. struct RelativeDirectPointerIntPair<Pointee, IntTy: BinaryInteger> /* where alignof(Pointee) => 2 */

    { let offsetWithInt: Int32 mutating func pointee() -> Pointee { let offset = offsetWithInt & ~intMask return withUnsafePointer(to: &self) { pointer -> UnsafePointer<Pointee> in let rawPointer = UnsafeRawPointer(pointer) let advanced = rawPointer.advanced(by: Int(offset)) return advanced.assumingMemoryBound(to: Pointee.self) }.pointee } var value: IntTy { IntTy(offsetWithInt & intMask) } var intMask: Int32 { Int32( min( MemoryLayout<Pointee>.alignment, MemoryLayout<Int32>.alignment ) - 1 ) } } 24
  25. Symbolic Reference ϚϯάϦϯάͷछྨͷ1ͭɻ จࣈྻͷҰ෦ʹର৅ΦϒδΣΫτͷΞυϨεΛ௚઀ຒΊࠐΉɻ ઌ಄੍͕ޚจࣈͰ࢝·ΔͷͰ௨ॴͷϚϯάϧ͞Εͨจࣈྻͱ۠ ผͰ͖Δɻ 25

  26. __swift_instantiateConcreteTypeFromMangledName ܕ໊͔ΒϝλσʔλΛऔಘ͢ΔϥϯλΠϜؔ਺ɻ ಺෦ͰσϚϯάϧͯ͠ϝλσʔλΛεΩϟϯ͢ΔͷͰॏ͍ɻ 26

  27. Ҿ਺ʹ { i32, i32 }ͷߏ଄͕౉ͬͯ͘Δɻ union Input { struct {

    RelativePointer<CChar> typeName; // 4byte int32_t negativeTypeNameLength; // 4byte } nonCached; TypeMetadata *cached; // 8 byte } 27
  28. ූ߸෇͖64bit੔਺ͱͯ͠ݟͨ࣌ɺෛ਺Ͱ͋Ε͹Ωϟογϡͳ ͠ɺਖ਼਺Ͱ͋Ε͹Ωϟογϡ͋Γɺͱͯ۠͠ผͰ͖Δɻ TypeMetadata *__swift_instantiateConcreteTypeFromMangledName(Input input) { if ((int64_t)input > 0)

    { return input.cached; } else { const auto typeNameLength = -input.nonCached.negativeTypeNameLength; ... // Some heavy operations ... return ...; } } 28
  29. ·ͱΊ • ϥϯλΠϜϥΠϒϥϦʹ͸ݡ͍σʔλߏ଄ͷςΫχοΫ͕࢖ ΘΕ͍ͯΔɻ • ϙΠϯλαΠζ͕มΘͬͨΒյΕͦ͏ 29