What performance penalties arise while using method swizzling in Objective-C?
Which, if any, compiler optimisations are defeated with message swizzling?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With