> On 04 Nov 2016, at 13:59, Ryan Lovelett via swift-users 
> <swift-users@swift.org> wrote:
> 
> struct Foo {
>  init(from buffer: Data) {
>     bar = integer(withBytes: Array(buffer[4..<6]))
>     baz = integer(withBytes: Array(buffer[6..<8]))
>     ...
>  }
> 
> let d = Data(count: Int(3e+8))
> let f = Foo(from: d)
> 
> Did I just make two copies of the `Data`? How would I investigate this
> to understand it?

I often end up “printing” the addresses or using GDB to take an inside look.
However those snippets of inside information obtained that way are not 
necessary useful outside the specific problem that was studied.
As Brent already hinted: the compiler will generate different code in different 
situations. And for an outsider it is not always easy to understand why which 
code was generated.
The best approach imo is to “trust” the compiler and only start such 
investigations if you experience performance problems.

One thing that tripped me up: if you use inout variables, the observers will be 
triggered once the function completes. Even if the function never changed the 
data referred to. (This is now documented behaviour)


> 
> I _think_ that if I made it `inout` then it would not make a copy but
> now the data buffer is mutable. I want it to be clear I'm not mutating
> the buffer inside the initializer but I also want to be sure I'm not
> making a copy of the buffer either.
> 
> Help?
> 
> -- 
>  Ryan Lovelett
>  r...@lovelett.me
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl




_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to