Avoid objmsg function overhead?

Can we avoid the overhead of the objmsg function by using the selector and implementation selector sel and imp. Please tell us how we can avoid it?

+3
source share
3 answers
// first, access the SELector you're interested in (example)
SEL sel = @selector(<#SOMETHING#>);

// this is the definition of IMP
id (*IMP)(id, SEL, ...);

Access IMP for an instance of a specific class using:  + (IMP)instanceMethodForSelector:(SEL)aSelector;

Ideally, you will cast the result of instanceMethodForSelector: to the exact typedef of the function you are calling so that the compiler can get the sig on the right.

when you have an object, SEL and IMP, then use IMP as a normal C function pointer.

IMP (= self), (= _cmd). 2 objc.

+5

, , .

1) (. ). - objc_msgSend . nil self, IMP IMP (http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/). , ...

2)... C . .

0

:

-, Objective-C

Honestly, any way to avoid is obj_msg_send()guaranteed to be fragile and will probably save you nanoseconds per call. If you have a very short function, which is called very often, then you should probably just use C to override this function. In almost all other cases, this is not worth the trouble.

0
source

All Articles