Slide 1

Slide 1 text

The ownership in iOS Mixleap #11 in Osaka

Slide 2

Slide 2 text

@_natpenguin merpay iOS Software Engineer

Slide 3

Slide 3 text

@_natpenguin merpay iOS Software Engineer .filter { $0 == } .map { $0 + } + [ , ✨ ] =

Slide 4

Slide 4 text

What is ownership?

Slide 5

Slide 5 text

It’s a management system for resource

Slide 6

Slide 6 text

It’s abstract word…

Slide 7

Slide 7 text

Ownership system is already used

Slide 8

Slide 8 text

Rust/C++/Swift… Ownership system is already used

Slide 9

Slide 9 text

Why we need ownership system?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

- Speed

Slide 12

Slide 12 text

- Safety - Speed

Slide 13

Slide 13 text

- Safety - Speed - Concurrency

Slide 14

Slide 14 text

Safety

Slide 15

Slide 15 text

- Access conflict to Data Rust prevents these in compiling

Slide 16

Slide 16 text

- Access conflict to Data Rust prevents these in compiling - Using after releasing resource

Slide 17

Slide 17 text

fn main() { let x = vec![1, 2, 3]; let y = x; println!(“x[0] is {}", x[0]); } This code will be compiling error

Slide 18

Slide 18 text

fn main() { let x = vec![1, 2, 3]; take(x); println!(“x[0] is {}", x[0]); } fn take(v: Vec) { // do something } This code will be compiling error

Slide 19

Slide 19 text

fn main() { let mut x = &100; let mut y = &mut x; *y += 1; println!(“y is {}", y); } This code will be compiling error

Slide 20

Slide 20 text

It’s a difficult

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Manifesto https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md

Slide 23

Slide 23 text

Q. What does swift want by using it?

Slide 24

Slide 24 text

A. Improving performance Q. What does swift want by using it?

Slide 25

Slide 25 text

- Making extra copies at runtime Problem Point

Slide 26

Slide 26 text

- Making extra copies at runtime - Referencing to unique resource Problem Point

Slide 27

Slide 27 text

- Making extra copies at runtime - Referencing to unique resource - Overhead of reference count Problem Point

Slide 28

Slide 28 text

Point - Prevents simultaneously accessing to Data

Slide 29

Slide 29 text

Point - Prevents simultaneously accessing to Data - Give programmers more control

Slide 30

Slide 30 text

Point - Prevents simultaneously accessing to Data - Give programmers more control - Add types that can’t be implicitly copied

Slide 31

Slide 31 text

A1. Supporting a type that can’t copy Q. How does swift improve performance?

Slide 32

Slide 32 text

A1. Supporting a type that can’t copy Q. How does swift improve performance? A2. Law of exclusivity

Slide 33

Slide 33 text

New keywords - shared

Slide 34

Slide 34 text

func ==(left: shared String, right: shared String) -> Bool { ... } shared String isn’t copied

Slide 35

Slide 35 text

What is different with inout?

Slide 36

Slide 36 text

- shared - inout

Slide 37

Slide 37 text

New keywords - shared - moveonly

Slide 38

Slide 38 text

moveonly struct Foo { // some parameters }

Slide 39

Slide 39 text

moveonly extension Foo { // Some extensions }

Slide 40

Slide 40 text

extension Foo { moveonly func bar(_ u: U) }

Slide 41

Slide 41 text

moveonly struct File { let descriptor: Int32 init(filename: String) throws { descriptor = Darwin.open(filename, O_RDONLY) } deinit { _ = Darwin.close(descriptor) } }

Slide 42

Slide 42 text

When we use it? - Performance tuning

Slide 43

Slide 43 text

When we use it? - More memory management - Performance tuning

Slide 44

Slide 44 text

When we use it? - More memory management - Performance tuning - Already know that no needed to copy

Slide 45

Slide 45 text

Thank you so much!