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

Sequence, Collection and Array in Swift

Pofat
August 02, 2020

Sequence, Collection and Array in Swift

My talk at COSCUP 20 about how Swift constructs its collection data structure architecture using protocols.

Also a quick guide to Swift source code building and overall understanding.

Pofat

August 02, 2020
Tweet

More Decks by Pofat

Other Decks in Programming

Transcript

  1. About Swift • Goal: Safe, fast and expressive with modern

    approach • Apache 2.0 license with a Runtime Library Exception • Swift project is governed by a core team that drives the strategic direction by working with the community.
  2. Swift Projects • Swift compiler: https://swift.org/compiler-stdlib/ • The standard library

    bundled as part of the language • Core libraries that provide higher-level functionality: https://swift.org/core- libraries/ • The LLDB Debugger with Swift REPL: https://swift.org/lldb/ • Swift Package Manager: https://swift.org/package-manager/ • Xcode playground support
  3. Platform Support • Apple: iOS, macOS, watchOS, iPadOS and tvOS

    • Linux: Ubuntu, CentOS, Amazon Linux2 and etc..
  4. Swift Evolution • Manifesto: Describe the proposed roadmap and goal

    for major projects. • Proposal: Describe the design proposal for new features or changes • Status of proposals can be traces at https://apple.github.io/swift-evolution/
  5. VS Code Setup for Swift Install Xcode • Install Xcode

    (The correct way to install Xcode)
  6. VS Code Setup for Swift Install VS Code and NPM

    • Install VS Code • Install VS Code command line tool • Install node and npm
  7. Debug Swift Using VS Code • Open cloned Swift source

    code root folder • Create configuration launch.json
  8. Standard Library Core • Standard library: Core, Runtime and SDK

    overlays • Definitions of all data types, protocol, functions, etc. • All written in Swift • Use GYB for repetitive codes
  9. Standard Library Core • Data types: Int, Optional, Bool, Array,

    … • Protocols: Sequence, Hashable, CustomStringConvertible, … • Functions: +, +=, …
  10. Attributes • @inlinable : Make it inline when compile with

    -O • @_transparent : Must be inline even when compile with -None • @usableFromInline : Temporary change scope during inline stage • @_fixed_layout : Processed at SIL stage. It tells compiler that access properties by offset is possible without looking up metadata. • @frozen : Only allowed in library evolution mode. Marked enum or struct can’t change their declaration by adding, removing, or reordering an enumeration’s cases or a structure’s stored instance properties.
  11. Protocols in Standard Library • Identity: Sequence, Collection, … •

    Ability: Hashable, Equatable, … • Able to convert to: CustomStringConvertible, …
  12. Sequence • Definition: A sequence is a list of values

    that you can step through one at a time. • Iterator step through the whole sequence
  13. String Hierarchy String _guts: _StringGuts _StringGuts _object: StringObject _StringObject _countAndFlagBits:

    UInt64 _object: Builtin.BridgeObject rawBits: RawBitPattern discriminatedObjectRawBits: UInt64 _StringStorage _realCapacityAndFlags: UInt64 _countAnd ags: CountAnd ags _ShardStringStorage _countAnd ags: CountAnd ags Maybe Maybe
  14. _SmallString SmallString.swift RawBitPattern.0 RawBitPattern.1 0 1 2 3 4 5

    6 7 8 9 A B C D E F Discriminator UTF8 Code Point 56 57 58 59 60 61 62 63 Used Count isForeign 0 isSmall 1 ASCII 0/1 isImmortal 1
  15. String Initialize Small String String _StringGuts _StringObject _SmallString Raw bytes

    after transcode Init with buffer Return smallString smallString Init with smallString Init with _StringObject Return _StringObject Return _StringGuts Init with guts
  16. Exercises • What’s the flow of initializing a large string

    • How does String mutation work? (e.g. append)
  17. String Index • String index is not Int, but StringIndex.

    A position of a character or code unit. StringIndex.swift