Is Objective-C fast enough for programming DSP / audio

I'm moving forward with audio programs for the iPhone. Now I am doing some performance tuning, trying to figure out if I can squeeze more out of this small machine. Running shark, I see that a significant part of my processor power (16%) is absorbed by objc_msgSend. I understand that I can speed this up a bit by storing function pointers (IMPs) rather than naming them with [object message] notation. But if I am going to go through all these problems, I wonder if I can just use C ++ better.

Any thoughts on this?

+7
performance objective-c iphone audio
source share
4 answers

The problem with Objective-C and features like DSPs is not speed as such, but rather the uncertainty of when inevitable bottlenecks will occur.

All languages ​​have bottlenecks, but in static related languages ​​such as C ++, you can better predict when and where in the code they will appear. In the case of Objective-C communication, the runtime, the time required to find the corresponding object, the time required to send the message is not slow, but it is variable and unpredictable. Objective-C flexibility in the user interface, data management and reuse against it in case of a tough task.

Most of the audio processing in the Apple API is done in C or C ++ because of the need to nail down the time needed to execute the code. However, it is easy to mix Objective-C, C, and C ++ in one application. This allows you to choose the best language for your immediate task.

+4
source share

The goal of C is absolutely sufficient for programming DSP / audio, since Objective-C is a superset of C. You do not need (and should not) do the whole message. If performance is critical, use simple C function calls (or use the built-in assembly if there are hardware functions that you can use this way). In cases where performance is not critical and your application can take advantage of message indirection functions, use square brackets.

The Accelerate view on OS X, for example, is an excellent high-performance Objective-C library. It uses only standard C99 function calls, and you can call them from Objective-C code without any wrapping or indirectness.

+13
source share

Is Objective-C fast enough for programming DSP / audio

real time rendering

Definitely not . The Objective-C runtime and its libraries are simply not designed for real-time rendering requirements. The fact is that it is almost impossible to guarantee that using the ObjC runtime or libraries such as Foundation (or even CoreFoundation) will not cause your rendering to fail.

A common case is locking β€” even a simple heap allocation ( malloc , new / new[] , [[NSObject alloc] init] ) will likely require locking.

To use ObjC, you must use libraries and a runtime that assume that locks are acceptable at any time during their execution. A lock can pause a rendering stream (for example, during a render callback), waiting for a lock to be received. Then you can skip the deadline because the rendering stream is held up, which ultimately leads to crashes / crashes.

Ask the pro audio plugin developer: they will tell you that blocking inside the real-time rendering domain is prohibited. You cannot, for example, run on a file system or create heap allocations because you do not have a practical upper bound on the time it takes to complete.

Here's a good introduction: http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing

Offline rendering

Yes, in most scenarios, high-level messaging would be acceptable quickly. At lower levels, I recommend not using ObjC because it would be wasteful - it can take many, many times to do if ObjC messaging is at that level (compared to the C or C ++ implementation).

See also: Will an iPhone app use performance if I use Objective-C for low level code?

+3
source share

objc_msgSend is just a utility. The cost of sending a message is not only the cost of sending a message. This is the cost of doing everything that triggers the message. (In the same way that the true cost of a function call is its inclusive cost, including I / O, if any.)

What you need to know is where the important time messages come from. Stack samples tell you which procedures / methods are called so often that you need to figure out how to use them more efficiently.

You may find that you call them more than you need.

Especially if you find that many calls are designed to create and delete data structures, you can probably find more efficient ways to do this.

+1
source share

All Articles