Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C / C static method performance

Here is a method from a class in some of Apple's example code. Why is this method defined as a static C method rather than an Objective C class method or a class method? In the context in which it is used I suppose it needs to be as performant as possible. Is this why? Is this the most performant way to declare a method?

static BOOL lineIntersectsRect(MKMapPoint p0, MKMapPoint p1, MKMapRect r)
{
    //Do stuff
    return MKMapRectIntersectsRect(r, r2);
}
like image 351
Undistraction Avatar asked Feb 23 '23 06:02

Undistraction


2 Answers

It's not a static method, but rather a function. And it's probably defined as a function because it operates on two data types (MKMapPoint and MKMapRect) which are not objects (they are C structs) and thus can't have methods associated with them.

like image 159
mipadi Avatar answered Mar 04 '23 22:03

mipadi


C functions are faster than Objective C methods because C functions bypass the Objective C runtime messaging system. The static keyword in the declaration limits the visibility of the function to the current compilation unit, so it is only visible in that particular file. The compiler can take a hint from the static keyword to optimize the assembler output for the function, so it is possible to increase performance further.

like image 31
Matt Wilding Avatar answered Mar 04 '23 22:03

Matt Wilding