Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C method swizzling performance

What performance penalties arise while using method swizzling in Objective-C?

Which, if any, compiler optimisations are defeated with message swizzling?

like image 361
Ilea Cristian Avatar asked Jul 21 '15 13:07

Ilea Cristian


People also ask

What is method swizzling in Objective-C and why would you use it?

Method swizzling is the process of replacing the implementation of a function at runtime. Swift, as a static, strongly typed language, did not previously have any built-in mechanism that would allow to dynamically change the implementation of a function.

How does method swizzling work?

Method swizzling is the process of changing the implementation of an existing selector. It's a technique made possible by the fact that method invocations in Objective-C can be changed at runtime, by changing how selectors are mapped to underlying functions in a class's dispatch table.

What is swizzle iOS?

iOS Swift Tips. Swizzling (other languages call this “monkey patching”) is the process of replacing a certain functionality or adding custom code before the original code is called. For example, you could swizzle UIViewController. viewDidAppear to be informed whenever a view controller is displayed.


1 Answers

Swizzling makes the Objective-C runtime flush its method caches. If you do all your swizzling as early as possible in your program, this will probably have no noticeable impact.

The compiler can't do much to optimize method calls because Objective-C allows any method to be overridden, even at run time (as with swizzling). So you've already paid the performance price of swizzling whether you use it or not.

like image 111
rob mayoff Avatar answered Sep 28 '22 02:09

rob mayoff