4

Objective-C uses something fancy called tagged pointers to reduce memory usage and increase speed when dealing with certain small object types like strings, dates, and numbers. Basically, if all the data in the object can fit in the pointer, the runtime puts it there instead of allocating and tracking heap memory.

Does Swift have a similar mechanism?

Community
  • 1
  • 1
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • Is this a performance question? It doesn't seem to have much bearing on *how* we would use Swift... – Grimxn Feb 23 '16 at 19:11
  • Yeah, tagged pointers don't matter that much practically, though they do mean that there's less gain to be had than one would think in, for example, switching from creating `NSNumber`s in a loop to using straight `int64_t`s (or whatever size is appropriate). This is just a question out of curiosity. – Tom Hamming Feb 23 '16 at 19:13
  • 1
    Swift strings and numbers are already value types rather than reference types, so there is no pointer to tag. Foundation strings and numbers are reference types and may be tagged pointers. – Darren Feb 23 '16 at 19:27
  • @Darren Swift strings and numbers and other value types *behaves* like value types. It is an implementation detail, whether they are allocated on stack or heap (aka pointers or not). – Amin Negm-Awad Feb 23 '16 at 20:51
  • @TomHamming Surely you know, that tagged pointers are transparent to the developer. So tagged pointers in Swift would be transparent, too. So it is a question fr the implementation detail. However, I'm pretty sure that *if* something is implemented using a reference, they would chose the same optimizations in Swift. Some (all?) of the Swift guys come from the clang/rte team. They know about what Objective-C does. – Amin Negm-Awad Feb 23 '16 at 20:54
  • @AminNegm-Awad yeah, I know they're a transparent optimization. As I said I'm just curious whether Swift does similar things. I forgot that there are no object types in Swift for strings and numbers. But it does use `NSDate` for its date type...would tagged pointers be used in that case, I wonder? – Tom Hamming Feb 23 '16 at 23:49

1 Answers1

3

From The Swift ABI:

Class Layout

Swift relies on the following assumptions about the Objective-C runtime, which are therefore now part of the Objective-C ABI:

  • (information about Objective-C tagged pointer layout ...)

The following assumptions are part of the Swift ABI:

  • Swift class pointers are never tagged pointers.

As I understand that, Swift handles Objective-C tagged pointers which are obtained from the Objective-C runtime (e.g. NSNumber, NSDate), but does not use tagged pointers for pure Swift classes.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Your quote is incomplete, since right before that is a a bunch of commentary on how Swift **does** use tagged pointers when dealing with the Objective-C ABI, which is a pretty common use case. – rob mayoff Feb 23 '16 at 19:18
  • 1
    Dang, that's kind of unfortunate. But understandable, since there might not be an equivalent in Swift to `objc_msgSend` that can be the One Place to act on a tagged pointer. – Tom Hamming Feb 23 '16 at 19:23
  • There is an equivalent: The compiler. All the things done by the RTE in Objective-C are done by the compiler. *If* – and this is important – it is decidable at compile time. So Swift could use tagged pointers for numeric literals i. e. But it seems to be better to boil it down to ordinary scalars in such a case. I do not think that in Swift there is a high pressure to have tagged pointers. – Amin Negm-Awad Feb 23 '16 at 21:00
  • @MartinR I do not think that the last statement gives an understanding that there are never tagged pointers in Swift. It only says that pointers to *classes* are never tagged. Numbers are not classes in Swift. – So, if there is a situation that let the compiler decide to use pointers for a scalar value like an integer, you simply cannot say, whether Swift would take the option of tagged pointers. – Amin Negm-Awad Feb 23 '16 at 21:05
  • @AminNegm-Awad: Sorry, I don't get you. Tagged pointers is about pointers to objects. It is an "optimization" where the pointer does not point to an object, but contains the information in itself. It does not (as I see it) make sense to speak about tagged pointers in connection with non-class types. – Martin R Feb 23 '16 at 21:09
  • Talking about reference types and value types is talking about the *behavior*. Of course, one can *implement* the behavior of value types using pointers. (Basically you need nothing else than an implicit copy operator.) Tagged pointers are an optimization (aka transparent feature) of pointers. So, *if* one decide to implement value types with an indirection for whatever reason, you could additionally use the optimization of tagged pointers. Both techniques are orthogonal. – Amin Negm-Awad Feb 23 '16 at 21:30
  • @MartinR However, if you do think that value types are always implemented without indirection (pointer) the quote in your answer is still no answer to the Q.In this case the quote would deal with something that scalars are not. Darren's comment to the Q would be the right answer. – Amin Negm-Awad Feb 23 '16 at 21:34
  • @AminNegm-Awad: The question was if a similar mechanism like Objective-C tagged pointers is used in Swift: Put information into the pointer itself instead of allocating memory. Swift *could* do this for reference types if the information fits into a pointer, but the cited documentation states that it doesn't. – As I understand the question, it is completely unrelated to value types. You also wouldn't speak about tagged pointers in Objective-C in connection with struct or scalar types. – Anyway: I have tried to answer the question as I understand it. You are welcome to post another answer. – Martin R Feb 23 '16 at 21:41
  • @MartinR I posted my opinion in some comments. I do not think that the Q is unrelated to value types, because he gave examples for types that *are* value types. And again: Being a value type or not is a discussion about the *behavior* of a type. It is not a discussion about implementations and possible optimizations. – Amin Negm-Awad Feb 23 '16 at 21:45
  • @AminNegm-Awad: The examples are the Foundation types NSDate, NSNumber,... which are reference types. – Martin R Feb 23 '16 at 21:46
  • No, the examples are "small object types like strings, dates, and numbers. Basically, if all the data in the object can fit in the pointer," and the counterpart in Swift. (Obviously he does not want to discuss how Swift handles Obejctive-C/Cocoa objects of this type in Swift. He wants to discuss the counterpart in Swift.) – Amin Negm-Awad Feb 23 '16 at 21:49
  • @AminNegm-Awad: Indeed, object types, i.e. instances of NSObject subclasses. That are reference types in Swift. Value types are struct, enum, tuples. – Martin R Feb 23 '16 at 21:54
  • @MartinR Indeed, strings numbers and dates are value types in Swift. – Amin Negm-Awad Feb 23 '16 at 21:57
  • @AminNegm-Awad: `String`, `Int` are value types in Swift, but `NSString`, `NSNumber` are reference types. There is no "date" type in Swift. – Martin R Feb 23 '16 at 21:58
  • He is not asking about NSNumber et al., but about the counterpart in Swift. – Amin Negm-Awad Feb 23 '16 at 22:00
  • @AminNegm-Awad: That is not how I interpret the question. *"Basically, if all the data in the object can fit in the pointer, the runtime puts it there instead of allocating and tracking heap memory. – Does Swift have a similar mechanism?"* – This question only makes sense with pointer types (called reference types in Swift). – Martin R Feb 23 '16 at 22:02
  • No, this Q makes sense for everything that is *implemented* using pointers. That *can* be value types, too. (To be honest it makes no sense at all for both reference and value types, for both Swift and Objective-C, because it is an implementation detail, an optimization. So none should even ask for it. But I understand his curiosity.) But it makes definetely no sense to discuss on SO, whether a Q makes sense. ;-) – Amin Negm-Awad Feb 23 '16 at 22:06
  • @AminNegm-Awad: That is a bit too theoretical for me. Swift clearly distinguishes between reference and value types, and I don't know of any "value types implemented using pointers" in Swift. I have looked-up and provided a link to the official docs which in my opinion answers the question. I don't know what else to say. Also it is quite late now here in Germany. Let's agree that we disagree :) – Martin R Feb 23 '16 at 22:14