1

Does Clang / LLVM optimize memory use for auto-synthesized properties to avoid alignment padding? As far as I understand, an Objective-C object is basically a struct in memory, so an object like

@interface MyObj {
  BOOL b;
  id obj;
}

will have padding between b and obj in order to align obj on a pointer-size boundary, while an object like

@interface MyObj2 {
  id obj;
  BOOL b;
}

does not need padding between obj and b, since BOOLs are naturally byte-aligned.

For auto-synthesized properties, how does Xcode lay out the backing ivars in memory? Will my objects use less memory if I manually order the ivars to improve their padding?

Greg
  • 10,360
  • 6
  • 44
  • 67
  • 1
    Closely related: [Does the order of instance variable declaration matter in ObjC?](http://stackoverflow.com/q/25556463) – jscs Nov 13 '14 at 21:22

1 Answers1

2

While I don't know, I'm pretty sure the engineers who designed this would say it's an implementation detail you shouldn't care about. Of all of the things that could impact your performance, this isn't likely to be one.

If you really wanted to find out, perusing the clang or objc runtime code could tell you.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
  • It's a micro optimization for sure, but when transitioning from 32-bit to 64-bit code having objects peppered with padding can really increase their memory usage. Just hoping to learn whether it's a thing I might need to be concerned about. – Greg Nov 13 '14 at 16:43
  • @Greg I believe that Apple's "Architecting Modern iOS Games" video discusses the only situations where you need to concern yourself with that: https://developer.apple.com/tech-talks/videos/. It isn't really about games; it's about what 64-bit means to you. – matt Nov 13 '14 at 16:50
  • Thanks, I'll check that video out. – Greg Nov 13 '14 at 16:53
  • The video only addresses padding and layout of C structs, not Objective C objects. While the exact layout of objects in memory is an implementation detail, I don't believe it's likely to change except between major versions of iOS and infrequently even then. The knowledge would still be useful for optimization. – Greg Nov 13 '14 at 17:40