> On May 6, 2017, at 10:27 PM, Nate Birkholz via swift-users
> <[email protected]> wrote:
>
> Classes and structs are different things. Classes are passed by reference,
> structs are passed by value with copy on write.
This is not correct. Array, and other types, implement copy-on-write
themselves, but this is not true of arbitrary structs.
To answer Kelvin's question, yes, the optimizer will be able to see through
that code _assuming_ it can see the definition in a way it can optimize
(generally speaking, this means not something which will be dynamically
dispatched, and where the implementation is available (i.e. the same file, or
the same module with whole module optimization enabled)).
You can verify this by simply writing a small sample program and spot checking
the assembly matches what you want, for example:
--
$ cat z.swift
struct Vertex {
let x: Double, y: Double
var r:Int, g:Int, b:Int
let s:Double, t:Double
var selected:Bool
}
private func taxicab_distance(_ v1:Vertex, _ v2:Vertex) -> Double {
return v2.x - v1.x + v2.y - v1.y
}
func f0(a: Vertex, b: Vertex) -> Double {
return taxicab_distance(a, b)
}
$ swiftc -Ounchecked -S -o - z.swift | grep 'main2f0' -A8
.private_extern __T04main2f0SdAA6VertexV1a_AD1btF
.globl __T04main2f0SdAA6VertexV1a_AD1btF
.p2align 4, 0x90
__T04main2f0SdAA6VertexV1a_AD1btF:
pushq %rbp
movq %rsp, %rbp
movsd (%rsi), %xmm0
subsd (%rdi), %xmm0
addsd 8(%rsi), %xmm0
subsd 8(%rdi), %xmm0
popq %rbp
retq
--
- Daniel
>
> Strings and Arrays are structs, not classes.
>
> On Sat, May 6, 2017 at 10:24 PM, Kelvin Ma <[email protected]
> <mailto:[email protected]>> wrote:
> I hear that a lot but why do all the official sources sound like
> copy-on-write is something you have to manually implement with a class and
> isUniquelyReferenced if it’s not an array or a string?
>
> On May 7, 2017, at 12:02 AM, Nate Birkholz <[email protected]
> <mailto:[email protected]>> wrote:
>
>> Let me try one more time, my stupid phone keeps changing words.
>>
>> ACTUALLY, the struct is a pointer until it's written to, and is only copied
>> on write.
>>
>> Sent from my iPhone, please excuse brevity and errors
>>
>> On May 6, 2017, at 9:59 PM, Nate Birkholz <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>>> Oddly, until it's *written* you will be working with a pointer.
>>>
>>> Sent from my iPhone, please excuse brevity and errors
>>>
>>> On May 6, 2017, at 9:33 PM, Kelvin Ma via swift-users
>>> <[email protected] <mailto:[email protected]>> wrote:
>>>
>>>> If I have a “large” struct like
>>>>
>>>> struct Vertex
>>>> {
>>>> let x:Double, y:Double
>>>> var r:Int, g:Int, b:Int
>>>> let s:Double, t:Double
>>>> var selected:Bool
>>>> }
>>>>
>>>> and I want to pass it to a function without modifying it like
>>>>
>>>> func taxicab_distance(_ v1:Vertex, _ v2:Vertex) -> Double
>>>> {
>>>> return v2.x - v1.x + v2.y - v1.y
>>>> }
>>>>
>>>> Will the entire struct Vertex get copied and pushed onto the stack, or
>>>> will only the relevant members be passed to the function?
>>>>
>>>> In other words, does the compiler know to optimize this call down to
>>>>
>>>> func taxicab_distance(v2x:Double, v1x:Double, v2y:Double, v1y:Double) ->
>>>> Double
>>>> {
>>>> return v2x - v1x + v2y - v1y
>>>> }
>>>>
>>>> ?
>>>> _______________________________________________
>>>> swift-users mailing list
>>>> [email protected] <mailto:[email protected]>
>>>> https://lists.swift.org/mailman/listinfo/swift-users
>>>> <https://lists.swift.org/mailman/listinfo/swift-users>
>
>
>
> --
> Nate Birkholz
> _______________________________________________
> swift-users mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users