I was searching the internet for tips to optimizing Objective-C code and came across this link. In the article I saw the note below, which I am not able to understand.
-
3Yes, if you carefully observe those rules in every class in your application, it will run 0.02% faster and take 0.03% less storage. – Hot Licks Aug 28 '14 at 19:23
-
Can you help me with some links where I can read more about this – Tamil Aug 28 '14 at 19:28
-
1This link should help you. http://c2.com/cgi/wiki?PrematureOptimization – CrimsonChris Aug 28 '14 at 19:29
-
3Is anybody still using instance variables in Obj-C? I thought most people are using properties... If byte aligning is useful for you, then go for pure C because just the overhead of Obj-C will be a bigger problem than byte aligning. – Sulthan Aug 28 '14 at 19:29
2 Answers
This article is out of date. It was at one time true that ivars were stored in an Objective-C instance just as the members of a struct are stored, and thus that memory alignment could (marginally) affect access time.
Now, however, ivars are indirectly accessed (at least in Apple's runtime); the instance now holds the offset of the ivar, and uses that to access the variable. Since all those offsets are of the same type, and you have no control over the other storage, this alignment issue is obviated.
Further, explicit ivar declaration has fallen out of routine usage with the introduction of declared properties.

- 63,694
- 13
- 151
- 195
-
1Unless I'm missing something the introduction of indirection to find the offset of an ivar has no impact on ivar layout. Also the variables storing the offsets are not part of the instance - the instance still only contains the actual ivars. The offsets are calculated at link time, the indirection solves the fragile base class problem (follow link in answer). – CRD Aug 28 '14 at 20:58
-
It certainly wouldn't be the first time I've been wrong, @CRD, but every time I read that article I come away with the understanding that the offsets are indeed part of the instance. I'd be happy to see information contradicting that so I understood better. – jscs Nov 13 '14 at 21:21
-
1If the offsets were stored in the instance surely it would just replace one problem with another - how do you add an offset for a new base class instance variable? By observation the offsets are stored in globals, with names constructed from the class + instance variable names. Typical instance variable access is then a load offset from global + load/store (instance pointer + offset). I don't know whether what is observed is publicly documented, however I think the theoretical reason to do it this way holds regardless of the details. – CRD Nov 15 '14 at 16:43
The layout of ivars is determined by the Objective-C compiler and there is no control provided in the language to effect this. While the article you've read might have been correct at some point in time for a particular compiler, it is reporting implementation-specific details you cannot rely on.
Trust the compiler to do a good job; or if layout is important to you use C or other language which provides control over layout.

- 52,522
- 5
- 70
- 86