6. Value Semantics & Reference Semantics
Value Semantics
Copy-by-Value :
(Identity) < (Equality)
Struct, Enum, Tuple Objective-C Swift Value Type
Swift class ReferenceType
ValueType
7. Stack
Heap -> Reference Counting
Value Semantics & Reference Semantics
Value Type
8. Struct Value Type
struct SomeStruct {
var value = 10
}
func foo() {
var bar1 = SomeStruct()
var bar2 = bar1
bar2.value = 11
}
foo()
Value Semantics & Reference Semantics
9. Struct Value Type
struct SomeStruct {
var value = 10
}
func foo() {
var bar1 = SomeStruct()
var bar2 = bar1
bar2.value = 11
}
foo()
Value Semantics & Reference Semantics
bar2 : (7FFEEFBFF4B0) : 0B 00 00 00
00 00 00 00
bar1 : (7FFEEFBFF4B8) : 0A 00 00 00
00 00 00 00
10. Struct Value Type
struct SomeStruct {
var value = 10
}
func foo() {
var bar1 = SomeStruct()
var bar2 = bar1
bar2.value = 11
}
foo()
bar2 : (7FFEEFBFF4B0) : 0B 00 00 00
00 00 00 00
bar1 : (7FFEEFBFF4B8) : 0A 00 00 00
00 00 00 00!
Value Semantics & Reference Semantics
Stack 8 Byte
bar1.value // 10
bar2.value // 11
I
11. class SomeClass {
var value = 10
}
func foo() {
var bar1 = SomeClass()
var bar2 = bar1
bar2.value = 11
}
foo()
Value Semantics & Reference Semantics
Class Value Type
12. Value Semantics & Reference Semantics
Class Value Type
Stack 8 Byte
class SomeClass {
var value = 10
}
func foo() {
var bar1 = SomeClass()
var bar2 = bar1
bar2.value = 11
}
foo()
bar2 : (7FFEEFBFF4A0) : 20 FF 0A 03
01 00 00 00
bar1 : (7FFEEFBFF4A8) : 20 FF 0A 03
01 00 00 00
(1030AFF20) : 80 8F 00 00 01 00 00 00
02 00 00 00 02 00 00 00
0B 00 00 00 00 00 00 00
a
28. Value Semantics & Reference Semantics
Reference Type Immutable ?
Value Type , Thread-Safe
Reference Type Immutable ?
29. Value Semantics & Reference Semantics
Reference Type Immutable ?
Reference Type Immutable Thread
Foundation
(NSAttributedString <-> NSMutableAttributedString )
Immutable
Mutable
30. Value Semantics & Reference Semantics
Reference Type Immutable ?
Immutable
var array = NSArray()
for number in [1, 2, 3] {
array = array.adding(number) as NSArray
}
GO
31. Value Semantics & Reference Semantics
Reference Type Immutable ?
Immutable
var array = NSArray()
for number in [1, 2, 3] {
array = array.adding(number) as NSArray
}
var array = NSMutableArray()
for number in [1, 2, 3] {
array.add(number)
}
i
32. Value Semantics & Reference Semantics
Reference Type Immutable ?
API
// Mutable API
car.dashboard.speed = 100
// Immutable API
car.dashboard = Car.Dashboard(speed: 100)
// car = Car(dashboard: Car.Dashboard(speed: 100))
TO
of
Dashboard
33. Value Semantics & Reference Semantics
Class .
Identity Equality
Objective-C : Cocoa / Cocoa Touch Objective-C
Indirect Storage : Struct
100P t
37. Heap
//
let key = "(color)(theme)(selected)"
//
enum Color {}
enum Theme {}
struct Attribute: Hashable {
var color: Color
var theme: Theme
var selected: Bool
}
let key = Attribute(color: color, theme: theme, selected: selected)
string
let L
t Dictionary Key Hashable
iii
in j
it
78. Swift
Struct
Reference Type Struct
struct Label {
var text: String
var font: UIFont
}
// 1
let font = UIFont.systemFont(ofSize: 15)
// 2
let label = Label(text: "msg", font: font)
// 3
let label2 = label
UIFont Class (Reference Type) -> Reference Counting
String Heap -> Reference Counting
79. Swift
Struct
Reference Type Struct
struct Label {
var text: String
var font: UIFont
}
// 1
let font = UIFont.systemFont(ofSize: 15)
// 2
let label = Label(text: "msg", font: font)
// 3
let label2 = label
...
refCount 3
...
HeapStack
...font
text(storage)
font
text(storage)
font
...
refCount 2
...
label
label2
80. Swift
Struct
Reference Type
struct HTTPRequest {
var `protocol`: String
var domain: String
var path: String
var filename: String
var `extension`: String
var query: [String: String]
var httpMethod: String
var httpVersion: String
var httpHost: String
}
81. Swift
Struct
Reference Type
enum HTTPMethod {
case get
case post
case put
case delete
}
enum HTTPVersion {
case v1
case v2
}
struct HTTPRequest {
var urlString: String
var httpMethod: HTTPMethod
var httpVersion: HTTPVersion
var httpHost: String
}
struct HTTPRequest {
var `protocol`: String
var domain: String
var path: String
var filename: String
var `extension`: String
var query: [String: String]
var httpMethod: String
var httpVersion: String
var httpHost: String
}
85. Swift
Protocol
Protocol Type : Memory Allocation
protocol Drawable {}
struct Point: Drawable {
var a: Int
}
struct Line: Drawable {
var x: Int
var y: Int
}
let drawables: [Drawable] =
[Point(), Line()]
refCount
HeapStack
...
storage
drawables
a x
y
8 Byte
16 Byte
87. Swift
Protocol
Existential Container
Protocol Type
5 Word (64bit -> 1 Word == 8 Byte)
- 3 Word : Value Buffer
- 2 Word : Buffer (VWT / PWT )
Protocol Type Existential Container
Protocol 3 Word ,
- 3 Word : Existential Container Value Buffer
- 3 Word : Heap ,
Value Buffer
97. Swift
Protocol
Indirect Storage
Value Type Reference Type
Value Semantics Copy-on-Write
isKnownUniquelyReferenced(_:)
if !isKnownUniquelyReferenced(&object) {
storage = Storage(storage)
}
102. protocol SomeProtocol {
var a: Int { get }
var b: Int { get }
}
struct SomeStruct: SomeProtocol {
var a: Int = 10
var b: Int = 11
}
func foo() {
var someStruct1 = SomeStruct()
var someStruct2: SomeProtocol = SomeStruct()
withUnsafeBytes(of: &someStruct1) { print($0) }
withUnsafeBytes(of: &someStruct2) { print($0) }
}
foo()
106. protocol SmallProtocol {
var a: Int { get }
var b: Int { get }
var c: Int { get }
}
protocol LargeProtocol {
var a: Int { get }
var b: Int { get }
var c: Int { get }
var d: Int { get }
}
struct SmallStruct: SmallProtocol {
var a: Int = 10
var b: Int = 11
var c: Int = 12
}
struct LargeStruct: LargeProtocol {
var a: Int = 10
var b: Int = 11
var c: Int = 12
var d: Int = 13
}
func foo() {
var smallStruct: SmallProtocol = SmallStruct()
var largeStruct: LargeProtocol = LargeStruct()
withUnsafeBytes(of: &smallStruct) { print($0) }
withUnsafeBytes(of: &largeStruct) { print($0) }
}
foo()
111. protocol SmallProtocol {
var a: Int { get set }
var b: Int { get set }
var c: Int { get set }
}
struct SmallStruct: SmallProtocol {
var a: Int = 10
var b: Int = 11
var c: Int = 12
}
func foo() {
var bar1: SmallProtocol = SmallStruct()
var bar2 = bar1
// breakpoint 1
bar2.a = 11
// breakpoint 2
withUnsafeBytes(of: &bar1) { print($0) }
withUnsafeBytes(of: &bar2) { print($0) }
}
foo()
3 Word Protocol Type
113. protocol LargeProtocol {
var a: Int { get set }
var b: Int { get set }
var c: Int { get set }
var d: Int { get set }
}
struct LargeStruct: LargeProtocol {
var a: Int = 10
var b: Int = 11
var c: Int = 12
var d: Int = 13
}
func foo() {
var bar1: LargeProtocol = LargeStruct()
var bar2 = bar1
// breakpoint 1
bar2.a = 11
// breakpoint 2
withUnsafeBytes(of: &bar1) { print($0) }
withUnsafeBytes(of: &bar2) { print($0) }
}
foo()
3 Word Protocol Type
116. Swift
Protocol
3 Word Protocol Type vs. Indirect Storage
3 Word Protocol Type Indirect Storage
Heap
Reference Counting : X
Copy-on-Write -> Value Semantics
Heap
Reference Counting : O
Value Semantics
Copy-on-Write
117. Swift
Generics
3 Word Protocol Type vs. Indirect Storage
protocol Drawable { func draw() }
struct Point: Drawable { func draw() { ... } }
struct Line: Drawable { func draw() { ... } }
func draw<T: Drawable>(_ drawable: T) {
drawable.draw()
}
draw(Point())
draw(Line())
Generic Type T Drawable
Existential Container
T :
->
120. protocol SomeProtocol {
var a: Int { get }
}
struct SomeStruct1: SomeProtocol {
var a: Int = 10
}
struct SomeStruct2: SomeProtocol {
var a: Int = 11
var b: Int = 12
}
func bar<T: SomeProtocol>(_ bar: T) {
var bar = bar
print(type(of: bar))
withUnsafeBytes(of: &bar) { print($0) }
}
func foo() {
bar(SomeStruct1())
bar(SomeStruct2())
}
foo()