Slide 1

Slide 1 text

Optimizing Swift Collections Károly Lőrentey @lorentey

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

NSOrderedSet

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

1 4 8 9 11 12 14 16 17

Slide 11

Slide 11 text

1 4 8 9 11 12 14 16 17 5

Slide 12

Slide 12 text

1 4 5 8 9 11 12 14 16 17

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

40x

Slide 19

Slide 19 text

40x

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

9 1 8 11 16 5 12 4 14 17 left right

Slide 22

Slide 22 text

x a b y c d Empty Tree Black node Red node

Slide 23

Slide 23 text

.empty .node(color, a, x, b) x a b y c d

Slide 24

Slide 24 text

9 1 8 11 16 5 12 4 14 17 4 2 3 5 1 8 9 a b 6 7

Slide 25

Slide 25 text

9 1 8 11 16 5 12 4 14 17 4 2 3 5 1 8 9 a b 6 7

Slide 26

Slide 26 text

9 1 8 11 16 5 12 4 14 17

Slide 27

Slide 27 text

9 1 8 11 16 5 12 4 14 17

Slide 28

Slide 28 text

9 1 8 11 16 5 12 4 14 17

Slide 29

Slide 29 text

9 1 8 11 16 5 12 4 14 17

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

z x y a b c d x z y a b c d z y x a b c d x y z a b c d

Slide 39

Slide 39 text

x z y a b c d z x y a b c d x z y a b c d z y x a b c d x y z a b c d

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

2.5x

Slide 46

Slide 46 text

2.5x

Slide 47

Slide 47 text

2.5x ???

Slide 48

Slide 48 text

9 11 12 5 8 1 4 16 14 17 t t.insert(10)

Slide 49

Slide 49 text

9 11 12 5 8 1 4 16 14 17 t 10 t.insert(10)

Slide 50

Slide 50 text

9 11 12 5 8 1 4 16 14 17 t 10 t.insert(10)

Slide 51

Slide 51 text

9 11 12 5 8 1 4 16 14 17 t 10 t.insert(10)

Slide 52

Slide 52 text

9 11 12 5 8 1 4 16 14 17 t 10 t.insert(10)

Slide 53

Slide 53 text

9 11 12 5 8 1 4 16 14 17 t 10 11ʹ t.insert(10)

Slide 54

Slide 54 text

9 11 12 5 8 1 4 16 14 17 t 10 11ʹ 12ʹ t.insert(10)

Slide 55

Slide 55 text

9 11 12 5 8 1 4 16 14 17 t 10 11ʹ t.inserting(10) 9ʹ 12ʹ t.insert(10)

Slide 56

Slide 56 text

9 12 11 5 8 1 4 16 14 17 t 10 11ʹ 12ʹ t.inserting(10) 9ʹ t.insert(10)

Slide 57

Slide 57 text

9 12 11 5 8 1 4 16 14 17 t 10 11ʹ 12ʹ 9ʹ t.insert(10)

Slide 58

Slide 58 text

12 11 5 8 1 4 16 14 17 t 10 11ʹ 12ʹ 9ʹ t.insert(10)

Slide 59

Slide 59 text

11 5 8 1 4 16 14 17 t 10 11ʹ 12ʹ 9ʹ t.insert(10)

Slide 60

Slide 60 text

5 8 1 4 16 14 17 t 10 11ʹ 12ʹ 9ʹ t.insert(10)

Slide 61

Slide 61 text

5 8 1 4 16 14 17 t 10 11ʹ 12ʹ 9ʹ t.insert(10)

Slide 62

Slide 62 text

5 8 1 4 16 14 17 t 10 11ʹ 12ʹ 9ʹ 3 allocations 3 deallocations 10 atomic refCount ops t.insert(10)

Slide 63

Slide 63 text

9 11 12 5 8 1 4 16 14 17 t.insert(10) t

Slide 64

Slide 64 text

9 11 12 5 8 1 4 16 14 17 t.insert(10) 10 t

Slide 65

Slide 65 text

9 11 12 5 8 1 4 16 14 17 t.insert(10) 10 t 10

Slide 66

Slide 66 text

9 11 12 5 8 1 4 16 14 17 t.insert(10) 10 1 allocation No deallocations No atomic refCount ops t 10

Slide 67

Slide 67 text

9 11 12 5 8 1 4 16 14 17 t.insert(10) 10 1 allocation No deallocations No atomic refCount ops t 10

Slide 68

Slide 68 text

COW

Slide 69

Slide 69 text

opy C Write on

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

COWTree root: COWNode color: value: 10 left: right: COWNode COWNode

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

!

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

4.5x

Slide 84

Slide 84 text

4.5x ???

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

O(n) Linear Time

Slide 88

Slide 88 text

O(1) Constant Time O(n) Linear Time

Slide 89

Slide 89 text

O(1) Constant Time O(n) Linear Time 4096

Slide 90

Slide 90 text

4096

Slide 91

Slide 91 text

Ints 4096

Slide 92

Slide 92 text

4096 × 8 bytes

Slide 93

Slide 93 text

4096 × 8 bytes = 32 kB

Slide 94

Slide 94 text

4096 × 8 bytes = 32 kB = size of L1 cache

Slide 95

Slide 95 text

1 2 3 4

Slide 96

Slide 96 text

1 2 3 4 5

Slide 97

Slide 97 text

1 2 3 4 5 4

Slide 98

Slide 98 text

1 2 3 4 5 4

Slide 99

Slide 99 text

1 2 3 4 5 4 7

Slide 100

Slide 100 text

1 2 3 4 5 4 7 8 9

Slide 101

Slide 101 text

1 2 3 4 5 4 7 8 9

Slide 102

Slide 102 text

1 2 3 4 5 4 7 8 9

Slide 103

Slide 103 text

1 2 3 4 5 4 7 8 9 10

Slide 104

Slide 104 text

1 2 3 4 5 4 7 8 9 10 11 12

Slide 105

Slide 105 text

1 2 3 4 b 4 7 8 9 10 11 12 5 a c

Slide 106

Slide 106 text

1 2 3 4 b 4 7 8 9 10 11 12 5 a c

Slide 107

Slide 107 text

1 2 3 4 b 4 7 8 9 10 11 12 5 a c

Slide 108

Slide 108 text

1 2 3 4 b 4 7 8 9 10 11 12 5 a c

Slide 109

Slide 109 text

1 2 3 4 b 4 7 8 9 10 11 12 5 a c

Slide 110

Slide 110 text

9 c 1 2 3 4 b 4 7 8 1 1 1 5a

Slide 111

Slide 111 text

9 c d e f g h i 1 2 3 4 b 4 7 8 1 1 1 5a 34 7 8 1 2 b 4 1 2 b 4 1 3 47 8 1 2 b 4 1 2 b 4 1 2 Z 4 1 j

Slide 112

Slide 112 text

9 c d e f g h i 1 2 3 4 b 4 7 8 1 1 1 5a 34 7 8 1 2 b 4 1 2 b 4 1 3 47 8 1 2 b 4 1 2 b 4 1 2 Z 4 1 j

Slide 113

Slide 113 text

9 c d e f g h i 1 2 3 4 b 4 7 8 1 1 1 5a 34 7 8 1 2 b 4 1 2 b 4 1 3 47 8 1 2 b 4 1 2 b 4 1 2 Z 4 1 j B-tree

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

3.5x 4.5x 2.5x

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

1 2 34 54 7 8 9 1 2 34 54 7 8 9 Reuse an existing Collection ♻

Slide 126

Slide 126 text

1 2 34 54 7 8 9 1 2 34 54 7 8 9 Reuse an existing Collection ♻ Functional programming 
 with algebraic data types λ

Slide 127

Slide 127 text

1 2 34 54 7 8 9 1 2 34 54 7 8 9 Reuse an existing Collection ♻ Imperative rewrite with
 COW optimization # Functional programming 
 with algebraic data types λ

Slide 128

Slide 128 text

9 c de f gh i j 1 2 34 54 7 8 9 1 2 34 54 7 8 9 Reuse an existing Collection ♻ Cache-aware data structures and unsafe operations ⚙ Imperative rewrite with
 COW optimization # Functional programming 
 with algebraic data types λ

Slide 129

Slide 129 text

Reuse an existing Collection ♻

Slide 130

Slide 130 text

import BTree

Slide 131

Slide 131 text

import BTree

Slide 132

Slide 132 text

22m 40s SortedArray NSOrderedSet AlgebraicTree COWTree BTree/1024 Time spent inserting 3 million random values

Slide 133

Slide 133 text

22m 40s SortedArray NSOrderedSet AlgebraicTree COWTree BTree/1024 1.8s 7s 22s 14m 54s 22m 36s Time spent inserting 3 million random values

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

Thank You!

Slide 136

Slide 136 text

Thank You! lorentey

Slide 137

Slide 137 text

Thank You! lorentey @lorentey

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

Acknowledgements Cow picture courtesy of freegreatpicture.com Puppy pictures courtesy of Floppy the Beagle