Home > Articles > Programming > C/C++

  • Print
  • + Share This
From the author of Use the Compiler

Use the Compiler

The best optimizations are done for you automatically. Any of the optimizations that work for C will also work for Objective-C; if your code is too slow, simply compiling with -O3 or -O4 can make a difference.

Apple's runtime offers a slightly faster version of the message-send function that skips the test for whether the receiver is nil. If you have some code in which you can guarantee that every message send is to a non-nil receiver, move it to a separate compilation unit and set the compiler flag to use it.

If you're using the GNUstep Objective-C runtime and compiling with clang, you can run a set of extra LLVM optimizations that do things like eliminating the indirection for non-fragile instance variables when the class and all of its superclasses (except NSObject) are in the same compilation unit. This makes non-fragile ivars as fast as fragile ones, in the case where you don't benefit from the indirection (in other words, where you'd need a recompile to add new ivars).

Other optimizations in this set do things like automatically cache method lookups for class messages and for message sends in loops, using the GNUstep runtime's safe cache-invalidation mechanism. There is also a profile-driven optimization that will cache method lookups on hot paths where the receiver is normally the same class. Running these optimizations can make a significant difference in your program's execution speed.

  • + Share This
  • 🔖 Save To Your Account