iOS:调用 Objective-C 方法的处理开销是多少?
我正在编写一些实时音频处理代码,该代码将在音频单元的渲染回调中执行。
该线程处于系统识别的最高优先级。
Apple 指示最大限度地减少此调用中进行的处理量。他们的建议之一是避免 Objective-C 方法调用。
但为什么?
调用 Objective-C 方法时会发生什么?实际开销是多少?
I am writing some real-time audio processing code, which is to be executed in an audio unit's render callback.
This thread is at the highest priority level the system recognises.
Apple instructs to minimise the amount of processing that goes on in this call. One of their recommendations is to avoid Objective-C method invocation.
But why?
What happens when an Objective-C method is invoked? what is the actual overhead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Objective-C 方法解析是动态的。在其他语言(例如 C 或 C++)中,函数调用是在编译时设置的,本质上是跳转到包含该函数的地址。然而,在 Objective-C 中,方法调用被实现为“发送消息”,其工作方式不同。涉及一个查找过程而不是硬编码的跳转。
此查找过程是与定位要运行的方法的地址相关的开销。它非常优化,但对于某些类型的代码,开销可能会导致性能问题。
Mike Ash 就发生的情况发表了精彩的文章如果您对更多细节感兴趣,请参阅 Objective-C 消息传递。
Objective-C method resolution is dynamic. In other languages such as C or C++, a function call is set at compile time, essentially as a jump to the address that contains the function. In Objective-C however, method calls are implemented as 'sending messages' which don't work in the same way. There is a lookup process involved instead of a hardcoded jump.
This lookup process as an overhead associated with locating the address of the method to run. It is very optimised, but for certain types of code the overhead can cause performance issues.
Mike Ash gives a great writeup on what happens with Objective-C messaging if you're interested in additional detail.