Does it matter in performance if I use self.fooBar instead of fooBar?

Note. I know exactly what a property is. This question is about performance.

Using self.fooBar to access READ seems like a waste of time for me. Unnecessary Objective-C messaging continues. Getters usually just go along the ivar, so while he’s sure that no sensible getter method will be written, I think it's great to get around this heavy guy.

Objective-C messaging is about 20 times slower than direct calls. So if there is some high-performance high-frequency code with hundreds of properties that can be used, maybe this helps a lot to avoid unessessary Objective-C messages?

Or am I wasting my time thinking about this?

+3
source share
6 answers

This kind of premature optimization should really be postponed until you notice or measure (using Instru.app) the real problem.

+8
source

Do not be offended, but you are probably wasting your time thinking about it. If you do not have code that accesses this property thousands of times per second, then you will not see differences in performance.

+2
source

getters , . getter self ( ) , gett self.ivar, getter - .

( self.ivar ?)

, self.ivar self->ivar, :

  • atomic, self.ivar

    spin_lock(&ivar_lock);
    id retval = [ivar retain];
    spin_unlock(&ivar_lock);
    return [retval autorelease];
    

    id

    spin_lock(&ivar_lock);
    spin_lock(&destination_lock);
    memcpy(&destination, &ivar, sizeof(ivar));
    spin_unlock(&ivar_lock);
    spin_unlock(&destination_lock);
    

    . , nonatomic.

  • . getter - . , .

, , , - , IVAR .

+2

( ). ivar, , , , . , , , .. ..

, , . , - -foobar, @ "BOO", , superClass -printFooBar @ ", ", foobar , self.foobar?

accessor , , , , . , " " , " ", .

+2

, , , ivar , . , , - getter ( ..), .

+1

, . OO, , Objective-C: "getter" "setter" , KVO. ivars, , , .

, " ", , , .

0
source

Source: https://habr.com/ru/post/1747214/


All Articles