Can I inline static class methods in Objective-C?
Asked Answered
U

3

6

You can declare functions as inlines like this:

#ifdef DEBUG
void DPrintf(NSString *fmt,...);
#else
inline void DPrintf(NSString *fmt,...) {}
#endif

so that when you're not in DEBUG, there's no cost to the function because it's optimized and inline. What if you want to have the same thing but for a class method?

My class is declared like this:

@interface MyClass : NSObject {

}

    + (void)DPrintf:(NSString *)format, ...;
    // Other methods of this class
@end

I want to convert 'DPrintf' into something similar to the inline so that there's no cost to invoking the method.

But I can't do this:

inline +(void)DPrintf:(NSString *)format, ...; {}

How can I have a zero-cost static method of a class turned off for non-debug compilations?

Undershrub answered 19/1, 2011 at 21:26 Comment(0)
C
9

Be careful. Objective-C methods are not the same as C functions. An Objective-C method is translated by the compiler into the objc_msgSend() function call; you don't have control over whether a method is inline or not because that is irrelevant. You can read more about the Objective-C runtime here (Objective-C Runtime Programming Guide), here (Objective-C Runtime Reference), and here (CocoaSamurai post), and a quick Google search should bring up more info.

Christenechristening answered 19/1, 2011 at 21:36 Comment(2)
Also, beware premature optimization. An inline marking on a function which is printing stuff isn't likely to save much time, even if the function is called a lot. Inlining mostly only make sense where the return is very easy to compute, so the function call overhead is significant. Nothing involving any variation of printf is easy in terms of number of instructions called.Archdeacon
Besides the fact that today, most modern compilers (whether GCC or Clang) generally know how to recognize functions that work well inline on their own, and will mark them as such themselves. Humans are notoriously bad at estimating which functions work well inline, so it's recommended you just leave that alone for the compiler to figure out (I've heard of some C compilers that will even ignore whether you mark functions as inline or not, so be wary).Christenechristening
H
7

There is no such thing as a static method in Objective-C. There are only class methods, which are just like instance methods except they belong to a class. This means that, just like instance methods, a message send to a class must go through the message dispatch machinery to determine the correct method to call, and that is done at runtime. You could inline the call to the method dispatch machinery, but the method body still can't be inlined without a crazy level of optimization that doesn't exist in any Objective-C compiler at the moment.

At any rate, this is a micro-optimization. If profiling shows it to be necessary (which it almost never will), then you can go through the gymnastics. Otherwise, worry about the actual performance concerns in your application.

Hoeve answered 19/1, 2011 at 21:42 Comment(1)
For those (like me) coming from a Java background where class methods and static methods are synonymous, note that in Objective-C class methods are dynamically resolved -- operating on class objects, which is why they are not "static". en.wikipedia.org/wiki/Method_(computer_programming)Kevon
U
0

Yes!

You can accomplish this with blocks

-(void)viewDidLoad {

    void(^inlineFunction)(int) = ^(int argument) {
        NSLog(@"%i", argument);
    };
        
    inlineFunction(5);//logs '5'

}

Apple even documents this here (archive).

Enjoy!

Unfold answered 14/11, 2018 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.