Search code examples
objective-cpointersperformanceabstract-data-type

objective-c small class overhead


I was writing a class file and I included a CGPoint as an Ivar. This got me wondering about the overhead associated with smaller objective-c data structures. Is the memory footprint of something like a CGPoint significant enough to justify making a pointer to it, or would I just be making a pointer to 2 CGfloat values? For that matter, if all I need are x/y coordinates why not just stick 2 ints in as ivars?

On a related note, is there a nomenclature for describing tiny data structures, like "petty data structures", or "trivial data structures"; a word that describes a a struct made of a few primitives.


Solution

  • There are certainly issues with small objects, but there are supposedly several small-object optimizations in the system already.

    In general, if you need an object, use an object. If not, then don't.

    However, the bigger issue is to write your code so that it is easy to read by humans, and easy to maintain by humans. Use performance tools (like Instruments) to isolate places where system resource utilization needs to be addressed, and only then address those issues.

    Of course, there are obvious stupid things to avoid, but in general, focus on a clean design, and easy to read/change implementation. Running the performance tools on your test suite should easily spot anything too wrong.