11

I realize that what counts as premature optimization has a subjective component, but this is an empirical or best-practices question.

When programming for iOS, should I prefer using struct and typedefs where the object has no "behavior" (methods, basically)? My feeling is that the struct syntax is a bit strange for a non-C person, but that it should be WAY lower profile. Then again, testing some cases with 50K NSObject instances, it doesn't seem bad (relative, I know). Should I "get used to it" (use structs where possible) or are NSObject instances okay, unless I have performance problems?

The typical case would be a class with two int member variables. I've read that using a struct to hold two NSString instances (or any NSObject subclass) is a bad idea.

3
  • 1
    I would like to read the place to tell that using struct to hold NSObject class is a bad idea, just for learning
    – vodkhang
    Sep 14, 2010 at 15:40
  • That's a bad idea because, um, where are you going to maintain retain and release?
    – Yuji
    Sep 14, 2010 at 15:56
  • @vodkhang, somewhere in here stackoverflow.com/questions/1064500/… Sep 14, 2010 at 15:57

6 Answers 6

14

Structs with NSObject instances in them are definitely a bad idea. You need -init and -dealloc to handle the retain count correctly. Writing retain and releases from the caller side is just insane. It will never pay off.

Structs with two int or four doubles are borderline cases. The Cocoa framework itself implements NSRect, NSPoint etc. as a struct. But that fact has confused lots and lots of newcomers. Honestly, even the distinction between primitive types and object types confused them. It becomes even confusing to me when you have structs as properties of an object: you can't do

object.frame.origin.x=10;

If you start making your own structs, you need to remember which is which. That's again a hassle. I think the reason why they (NSRect etc.) are structs are basically historical.

I would prefer to make everything objects. And use garbage collection if available.

And, don't ask people if something is worth optimizing or not. Measure it yourself by Instruments or whatever. Depending on the environment (ppc vs intel, OS X vs iOS, iPad vs iPhone) one way which was faster in a previous system might be slower in a new system.

11

An Objective C object has almost the same storage as a struct, except it is 4 bytes (8 bytes on 64 bit) bigger. That's it - just one pointer into a place where the runtime holds all the class information.

If you are that tight on memory, then lose the 4 bytes, but usually that's only for large numbers of objects: 50,000 Nsobjects vs structs is only 200k - you get a lot of stuff for that 200k. For a million objects, the cost will add up on an iPhone.

If you want to say transfer the items to openGL or need a c array for other purposes, then another option is to make ONE NSObject that has a malloc'ed pointer to all 50,000 integers. Then the objective c memory overhead is ~0, and you can encapsulate all the nasty malloc and free() stuff into the innards of one .m file.

3
  • Hummm, I think that it is not just the problem with memory. It is also about referencing. I remember that if you use struct, then you don't have to access to the heap object memory, only stack access, which is much much faster:) (my knowledge is from C#, not sure about obj-C)
    – vodkhang
    Sep 14, 2010 at 15:45
  • 1
    @vodkhang: the speed does not matter, unless you want to code something like a particle engine with thousands of individual objects recalculated at 30 fps.
    – zoul
    Sep 14, 2010 at 15:47
  • @vodkhang: Stack memory is not faster than heap memory, it’s just the same. A compiler might keep things in registers which are way faster instead of on the stack, but this optimization usually applies to scalars only.
    – Sven
    Sep 14, 2010 at 17:25
10

Go with regular objects until you hit a measurable performance bottleneck. I’ve used high-level code even in tight game loops without problems – messaging, collection classes, autorelease pools, no problems.

3
  • Thanks, that was my decision last night at 3am. Just wanted to check if I've gone mad. Sep 14, 2010 at 15:39
  • 1
    It’s an important decision and it’s a good idea to think about it a little while. But the performance of today’s hardware – even the mobile, lower-level stuff – is astounding and makes this issue almost a no-brainer.
    – zoul
    Sep 14, 2010 at 15:43
  • 1
    Yes. It's a really strange world in which some people are working one level of abstraction UP (MonoTouch) or, in other worlds, MANY levels of abstractions up (Groovy in the JVM) with acceptable performance penalties, and other people are insisting on optimizing everything as much as possible. Thanks for you note on "tight game loops," it does definitely help me understand what's possible. Sep 14, 2010 at 15:53
5

I see no problem at all with using structs to hold small quantities of primitive (i.e. non object) types where there is no behaviour required. There are already several examples of this in the Cocoa frameworks (CGRect, CGSize, CGPoint, NSRange for example).

Do not use structs to hold Objective-C objects. It complicates the memory management in the reference counted environment and may break it altogether in the GC environment.

1
  • I thought I answered the question perfectly well. You said "should I get used to it?" The answer is yes, because, for small structures it is already quite common.
    – JeremyP
    Sep 15, 2010 at 7:50
3

For me, I would prefer to use regular objects because you can easily do Object job with it like retain, release, autorelease. I only see quite few structs in Cocoa Framework like CGSize, CGRect and CGPoint. I think the reason is that they are being used a lot

0
1

I believe is a good idea to use structs specially if you are dealing with C-based frameworks , lets says OpenGL, CoreGraphics, CoreText specially stuff that will require a couple/triple of ints, doubles, chars, etc. (If they are already not implemented in some of Apple Frameworks: CGRect, CGPoint, CTRect, NSRange, etc...) C stuff plays and looks better with other C stuff.

I don't think I would write a subclass of NSObject containing a couple of ints. It's almost ridiculous. lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.