1.15 Optimizing Method Calls
Objective-C's message-passing
implementation of method calls is simple and flexible. What it
sacrifices is the speed of a C++-style method call, or of a direct
function call.
If you are writing time-critical code, you may want to relinquish the
dynamic nature of the Objective-C method call for some extra speed.
For example, if your code calls the same method on an object many
times in a loop, you may not want to send a dynamic message each time
you invoke the method. Objective-C provides a way for you to get a
pointer to the function implementing the method, and then call the
method via the pointer, bypassing the Objective-C method dispatch
process.
 |
If you only call a method once, you should use a standard Objective-C
method call. This optimization's gain in efficiency
is directly related to the number of times you invoke the method.
|
|
For example, suppose you want to send the following message to invoke
a method that takes an integer and has no return value:
[obj methodName:anInt];
You can replace this ordinary Objective-C method call with the
following code:
1 SEL sel = @selector (methodName :);
2 typedef void (*MpType ) (id , SEL , int );
3 MpType mptr = (MpType ) [obj methodFor:sel ];
4 ...
5 mptr (obj , sel , anInt );
Line 1. Get the selector for the method you want to call. You will
use this both to acquire the method pointer and to use it.
Line 2. Define the type of your method pointer. This makes line 3
easier to read. Recall that methods are just C functions with two
hidden parameters: the receiver and the selector. Here, the type of
mptr takes these into account, as well as the
return (void) and parameter
(int) types of the method.
Line 3. Get the function pointer. If you already have the receiver at
hand (as in this example) you can use
-methodFor: (for descendants
of Object) or
-methodForSelector: (for descendants of
NSObject). If you don't have the
receiver or an object of the same type, you can call the class
methods +instanceMethodFor: (for subclasses of
Object) or
+instanceMethodForSelector:
(for subclasses of NSObject) on the class of the
object whose method you will call.
Since these methods are declared to return an
IMP, which is a pointer to a function whose
signature is (id,
SEL, ...) you need to cast
to your own type the value it returns, to prevent compiler warnings
at line 5.
Line 5. Use the function pointer as in C. Pass in the receiver and
selector as the first two arguments. After you execute lines 1-3,
line 5 will have the same effect as calling
[obj
methodName:anInt],
but will execute faster. Typically this line will be in a loop.
Optimizing a method call in this manner only makes sense when you are
going to invoke the method repeatedly, such as within a loop. Lines 1
through 3 represent a one-time setup that should be done prior to
entering the loop. Line 5 is the optimized method call that would be
used inside the loop.
|