Slide 1

Slide 1 text

Sequence, Collection and Array in Swift Pofat @ COSCUP 2020

Slide 2

Slide 2 text

Hi, I am Pofat Podcaster @ [weak self] @weak_self iOS Developer @PofatTseng pofat

Slide 3

Slide 3 text

Swift

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Platform Support • Apple: iOS, macOS, watchOS, iPadOS and tvOS • Linux: Ubuntu, CentOS, Amazon Linux2 and etc..

Slide 7

Slide 7 text

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/

Slide 8

Slide 8 text

Build Swift!

Slide 9

Slide 9 text

Requirement • Ninja/cmake • Xcode • Power • Env: Swift 5.2.4-RELEASE

Slide 10

Slide 10 text

Build Swift

Slide 11

Slide 11 text

GYB Tuple.swift.gyb

Slide 12

Slide 12 text

Tuple.swift

Slide 13

Slide 13 text

Swift-Doc • https://github.com/SwiftDocOrg/swift-doc •

Slide 14

Slide 14 text

VS Code Setup for Swift Install Xcode • Install Xcode (The correct way to install Xcode)

Slide 15

Slide 15 text

VS Code Setup for Swift Install VS Code and NPM • Install VS Code • Install VS Code command line tool • Install node and npm

Slide 16

Slide 16 text

VS Code Setup for Swift Build and install SourceKit-LSP for VS Code

Slide 17

Slide 17 text

Happy Tracing

Slide 18

Slide 18 text

Debug Swift Using VS Code • Open cloned Swift source code root folder • Create configuration launch.json

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Standard Library

Slide 21

Slide 21 text

Fundamental Architecture Swift Standard library Foundation

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Standard Library Core • Data types: Int, Optional, Bool, Array, … • Protocols: Sequence, Hashable, CustomStringConvertible, … • Functions: +, +=, …

Slide 24

Slide 24 text

+= Is a function IntegerTypes.swift

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Compile Swift Swift Frontend IRGen LLVM Swift code SIL LLVM IR Machine code

Slide 27

Slide 27 text

BuiltIn BuiltIn LLVM IR Standard Library Type Method BuiltIn.Type BuiltIn.Method

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

Example: Bool

Slide 30

Slide 30 text

Protocol-Oriented Programming

Slide 31

Slide 31 text

Why Protocol? • Contract of interfaces • Cross-cutting concern • Better way to share code than inheritance

Slide 32

Slide 32 text

Protocol Hashable.swift

Slide 33

Slide 33 text

Protocols in Standard Library • Identity: Sequence, Collection, … • Ability: Hashable, Equatable, … • Able to convert to: CustomStringConvertible, …

Slide 34

Slide 34 text

Sequence

Slide 35

Slide 35 text

Sequence • Definition: A sequence is a list of values that you can step through one at a time. • Iterator step through the whole sequence

Slide 36

Slide 36 text

Sequence Protocol Sequence.swift

Slide 37

Slide 37 text

IteratorProtocol Sequence.swift

Slide 38

Slide 38 text

Sequence Protocol Hierarchy

Slide 39

Slide 39 text

Reduce SequenceAlgorithms.swift

Slide 40

Slide 40 text

Map by Reduce

Slide 41

Slide 41 text

FlatMap SequenceAlgorithms.swift

Slide 42

Slide 42 text

What if the sequence is infinite in length?

Slide 43

Slide 43 text

Map a large sequence will lead to…

Slide 44

Slide 44 text

Lazy Sequence LazySequence.swift

Slide 45

Slide 45 text

LazySequenceProtocol LazySequence.swift Map.swift

Slide 46

Slide 46 text

LazyMapSequence Map.swift

Slide 47

Slide 47 text

Being Lazy Map.swift

Slide 48

Slide 48 text

Revise FileSequenceHandler

Slide 49

Slide 49 text

Collection

Slide 50

Slide 50 text

Collection Collection.swift

Slide 51

Slide 51 text

IndexingIterator Collection.swift

Slide 52

Slide 52 text

IndexingIterator Collection.swift

Slide 53

Slide 53 text

Collection Families

Slide 54

Slide 54 text

Collection Algorithms Shuffle CollectionAlgorithms.swift

Slide 55

Slide 55 text

Array

Slide 56

Slide 56 text

Array Sequence Collection MutableCollection BidirectionalCollection RangeReplaceableCollection RandomAccessCollection Array

Slide 57

Slide 57 text

Array ContinuousArray struct Array _buffer struct _ArrayBuffer struct _BridgeStorage _storage _rawValue BuiltIn.BridgeObject

Slide 58

Slide 58 text

Copy On Write Array.swift

Slide 59

Slide 59 text

Copy Array Array.swift

Slide 60

Slide 60 text

Copy Array If buffer is full ArrayShared.swift

Slide 61

Slide 61 text

Copy Array Grow strategy ArrayShared.swift

Slide 62

Slide 62 text

How Array Grows Exactly Double…? ܻ㺔氂ࣁ蝡螲 薹朰ஃ蝡螲

Slide 63

Slide 63 text

String

Slide 64

Slide 64 text

Let’s Read String!

Slide 65

Slide 65 text

String String.swift

Slide 66

Slide 66 text

String Conformance • Protocol hierarchy

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

_StringStorage StringStorage.swift

Slide 69

Slide 69 text

_StringStorage Creation StringStorage.swift

Slide 70

Slide 70 text

_StringStorage Creation Flow • String.init(decoding:as:) -> • String._fromCodeUnits(codeUnits:encoding:) -> • String._slowFromCodeUnits(_:encoding:repair:) StringStorage.swift

Slide 71

Slide 71 text

StringCreate.swift

Slide 72

Slide 72 text

StringCreate.swift

Slide 73

Slide 73 text

StringCreate.swift

Slide 74

Slide 74 text

Real Creation of UTF8 String StringCreate.swift

Slide 75

Slide 75 text

_SmallString SmallString.swift

Slide 76

Slide 76 text

_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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

String Equatable StringComparable.swift

Slide 79

Slide 79 text

String Equatable String Compare StringComparison.swift

Slide 80

Slide 80 text

String Equatable String Guts StringGuts.swift StringObject.swift

Slide 81

Slide 81 text

Exercises • What’s the flow of initializing a large string • How does String mutation work? (e.g. append)

Slide 82

Slide 82 text

String Index • String index is not Int, but StringIndex. A position of a character or code unit. StringIndex.swift

Slide 83

Slide 83 text

String Index A closer look

Slide 84

Slide 84 text

String.count O(n) • String is only BidirectionalCollection, not RandomAccessCollection. StringCharacterView.swift

Slide 85

Slide 85 text

String.count distance(from:to:) BidirectionalCollection.swift

Slide 86

Slide 86 text

String.count formIndex StringCharacterView.swift

Slide 87

Slide 87 text

Exercises • How can we make String conform to RandomAccessCollection?

Slide 88

Slide 88 text

Guts _Storage Native Small/Large

Slide 89

Slide 89 text

๜茐֢ኧPofat Tseng蕣֢牧犥獺አCC নݷ秂纈 4.0 㾴褬 ദ稗礿稠朰ڊ牐 Thank you!