Objective C Class Methods vs C Functions Objective C Class Methods vs C Functions objective-c objective-c

Objective C Class Methods vs C Functions


In short, C (or C++) implementations are very useful:

  • For Abstraction
  • For Reusability
  • When making medium and large scale programs
  • In performance critical paths
  • For 'Interior' implementations

What benefit exists (other than coding style) for favouring a C function over a class method?

  • ObjC messaging introduces indirect function calls. These are firewalls for optimizers.
  • C functions can easily restrict access, whereas 'private' ObjC implementations may be looked up using the ObjC runtime, or accidentally overridden.
  • C functions may be removed from your executable if not referenced, or they may be made private. If you write reusable code (and you should), this can have a huge impact on your binary sizes and load times -- C functions which are not referenced/used may be removed, but ObjC types and methods will be preserved (including everything they reference). This is why your app's binary size may grow significantly when you use only small part of an ObjC static library -- every objc class in the library is preserved. If that library were C or C++, then you could get by with very small growth because you need only what is referenced. What is or is not referenced is easier to prove with C and C++.
  • C functions can be inlined, either during compilation or during Link Time Optimization phases.
  • The compiler and optimizers are able to do much optimization with C functions (e.g. inter-procedural optimizations), but very little with ObjC methods because they are always indirect.
  • To avoid ObjC message dispatch overhead (as you mentioned)
  • Potential for additional reference counting operations and autorelease pool activity when interacting with ObjC objects.

Of course you won't always hurt paying for things you don't need or use -- and remember that ObjC class methods have some benefits over C functions, too. So, just look at C or C++ implementations as another tool in your toolbox. I find them very useful as complexity and project sizes increase, and they can be used to make your programs much faster. Just do what you are least likely to regret in 2015 ;)


You already touched on the marginal performance difference of avoiding an objc_msgSend call. Objective-C class methods are also subject to overriding in subclasses, so implementing a method in C will prevent it from being overridden in a subclass. Relatedly, because of that runtime inheritance/polymorphism, an Objective-C method can never be inlined, whereas a C function can potentially be inlined by the compiler for added performance.

When it comes to avoiding objc_msgSend, a wise man once told me, "If the overhead of objc_msgSend is too great for you, Objective-C is probably the wrong tool for the job."