(Morten Bek Ditlevsen) #1

Hi list,
I was looking at the NSDecimal.swift to learn a bit about the implementation of the Decimal type.
I get that a lot of the API in this type exists for compatibility reasons with the C implementation.

But I was wondering about how the NSDecimalCopy function differs from a simple assignment of one Decimal value to another.

NSDecimalCopy simply assigns each field of the struct one by one, so when would it ever be preferable to performing a simple assignment?

So basically my question is - is there any difference in the result of performing:

var a: Decimal = 1
var b: Decimal = 2
NSDecimalCopy(&b, &a)

and
var a: Decimal = 1
var b: Decimal = 2
b = a

Secondly - if there is no difference in the result - is it not more performant to assign the value directly?

This question also raised a question about Swift internals: If you have a pure value type (no nested reference types): Does an assignment then simply copy a chunk of memory with the size of the struct? Or is each field assigned/copied recursively?

Sincerely,
/morten

(Tony Parker) #2

Hi Morten,

Hi list,
I was looking at the NSDecimal.swift to learn a bit about the implementation of the Decimal type.
I get that a lot of the API in this type exists for compatibility reasons with the C implementation.

But I was wondering about how the NSDecimalCopy function differs from a simple assignment of one Decimal value to another.

NSDecimalCopy simply assigns each field of the struct one by one, so when would it ever be preferable to performing a simple assignment?

So basically my question is - is there any difference in the result of performing:

var a: Decimal = 1
var b: Decimal = 2
NSDecimalCopy(&b, &a)

and
var a: Decimal = 1
var b: Decimal = 2
b = a

I suspect that this is just due to some logic copying from the C version.

I’m actually not sure what the compiler does here. It should be possible to try it out in a test app, disassemble it, and check out the result. Let us know what you find. =)

- Tony

···

On Jan 9, 2017, at 2:16 AM, Morten Bek Ditlevsen via swift-corelibs-dev <swift-corelibs-dev@swift.org> wrote:

Secondly - if there is no difference in the result - is it not more performant to assign the value directly?

This question also raised a question about Swift internals: If you have a pure value type (no nested reference types): Does an assignment then simply copy a chunk of memory with the size of the struct? Or is each field assigned/copied recursively?

Sincerely,
/morten
_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev