Objective-C runtime features

I experimented using Objective-C (in particular, the GNU runtime, I don't use GNUStep or anything like that) to extend C with objects. So far, everything is going well - it works well, and I really like the syntax, however, from what I read, there is a performance limitation when using Objective-C objects.

I usually don’t optimize prematurely, but I’m developing a game, and therefore the performance will ultimately be quite critical - I would like to know what I'm getting into! Several people have suggested that I will be fine in using Objective-C in the game while my inner loops are written in standard C, however the question is to find the point where I should make this switch.

So my question is twofold:

  • What exactly will result in a performance penalty? (so I know what I should avoid)
  • Roughly How significant is this decline in productivity? (so that I can predict in advance which bits of my game will need to be written in C anyway)

My assumption is that method calls on an Objective-C object will incur a penalty, but I was not sure what else would be - deleting references to an Objective-C object would result in a similar fine?

int a = myobj->a; 

How about using try-catch-finally - this will result in a performance penalty in situations where there is no exception?

I understand that to some extent I could understand that most of them use tests, but I'm relatively new to this, and I was hoping for a deeper understanding of how Objective-C works and the nature of any performance penalties.

I'm not interested in C ++ at the moment, I'm just experimenting with Objective-C.

+4
source share
2 answers

So my question is twofold:

What exactly will entail a performance penalty? (so I know what I should avoid)

How significant is this performance assessment? (so that I can predict in advance which bits of my game should be written in C anyway)

(my base is from apple implementations :)

in most cases, the biggest penalty is that each instance (messaging) method is a dynamic call

the cost of this call / message is about 4x as expensive as a virtual C ++ call.

these calls cannot be optimized (unless someone makes a good JIT / HotSpot implementation), and methods, unlike c or C ++, may not be nested.

but there is more than messaging:

Your objects will be counted according to the schedule. this means: many locks and many individual, small distributions, plus atoms for counting ref.

objc_msgSend and options (meat of the execution of the runtime) can take, for example, 10% of the execution time (extreme cases of the real world).

It can also add to binary size.

Using idiomatic applications pushes it further away from C ++ or c speed in many areas. (for example, consider how numbers are transferred / processed).

how much it costs depends on the size of your methods. a good ood means your methods will usually be short. you will either receive a ton of messages and noticeable weight (if it is high performance), or it will jeopardize your projects.

There are several gotchas, for example, in real-time applications.

int a = myobj-> a;

no diff compared to c

... Exceptions ...

(iirc) Apple's previous exception model was used for expensive customization, now they correspond to C ++ zero-cost exception lines (it is optimized that exceptions will not be regularly thrown and marginal at the input / configuration, but expensive to throw / catch).

If performance is really important, I would avoid it and just go with C ++. It has many features and speed. you will have a little work to do to make some of objc's amenities - most of them are in accessible libraries, not in the language.

+4
source

If I were you, I would program the prototype, give it a realistic workload, and then figure out what parts of it were problems. Here is an example of what I mean.

In other words, you can get answers telling you that such and such is “effective” or “inefficient”, but whether it really applies in your case is a common WAG. Therefore, you need to try, and let the program itself tell you which parts need something better.

+3
source

All Articles