iOS Development in Runloop,runtime

Source: Internet
Author: User

function calls in the 1.OBJECTIVE-C

For the C language, the function call is done directly by the compiler, and at compile time the program begins to look for the function to be executed (the principle of C-language function invocation). In OC, we refer to the function call as message sending. The program does not look for functions to execute at compile time, and it must wait until it is really run before the program looks for the function to execute.

Example: in C language, only one function is declared, not implemented. This function is called elsewhere. Compile time will be error (C language compile time to find the function to execute, not found so error). The same situation in OC will not error, only in the run time will be error. (The OC runtime only looks for functions to execute)

The underlying implementation of the 2.OBJECTIVE-C function call

The objective-c is able to run only to find functions to be executed thanks to the runtime SDK. Let's look at how Objective-c lets the program have runtime features.

There is a objc_msgsend () method under the Runtime SDK

Objc_export ID objc_msgsend (ID self, SEL op, ...)

When we write the next line of code [obj Dosth]; At compile time, the compiler will turn our code into

Objc_msgsend (obj, @selector (dosth));

The Objc_msgsend () method implements the function lookup and matching, and here's how it works:

    • The list of functions stored in the object class is found methodlists according to the object obj.
    • Then find the corresponding function pointer method_imp in methodlists according to [email protected] (DOSTH).
    • A function that invokes a response based on the function pointer method_imp.

Implementation details of the 3.objc_msgsend

Before we simply introduced the principle of objc_msgsend (), let's take a closer look at how objc_msgsend is implemented.

First, for any one of the NSObject objects, there is an Isa attribute, which points to the class class of the object @interface NSObject {class Isa objc_isa_availability;} The corresponding class can be obtained according to the object invocation. Next we look at classtypedef struct Objc_class *class; struct Objc_class {

  • Class Isa; Point to Metaclass Class superclass; Pointer to the parent class class const char *name;
  • Class name uint32_t version; The version information of the class uint32_t info; Some identification information, indicating whether it is a normal class or Metaclass uint32_t instance_size; The instance variable size of the class (including instance variables inherited from the parent class);
  • struct Old_ivar_list *ivars; Information about member variables in a class
  • struct Old_method_list **methodlists; Class in the method list cache cache; Find the cache of methods to increase efficiency
  • struct Old_protocol_list *protocols; Store the protocol that the class adheres to} by the above code we can see that the class is a struct pointer, pointing to the Objc_class struct. A list of methodlists methods is stored in the Objc_class. So, according to class, we can just find methodlists below and we'll see how to find the corresponding function pointer struct old_method_list from methodlists.
  • void *obsolete; Deprecated properties
  • int method_count; Number of methods/* variable length structure */struct old_method method_list[1]; The first address of the method}; struct Old_method {sel method_name;//method corresponds to SEL char *method_types;//method type IMP Method_imp;//method corresponding function pointer}; for Old_method_lis t struct, he stores the number of Old_method methods and the first address of the method. We can think of him as a variable-length Old_method array. At first I don't understand why the size of the array is 1 method_list[1]. Later figured out that because the size of the array is variable, different classes correspond to the number of different methods. So the definition only stores the first address, in the actual use of the extension length for the old_method structure, he is composed of sel,type,imp three members. So we can get the function pointer imp as long as we find the Old_method in the method_list. Here is the code to find: static inline Old_method *_findmethodinlist (old_method_list * mlist, sel sel) {int i; if (!mlist) return nil; fo R (i = 0; i < mlist->method_count; i++) {Old_method *m = &mlist->method_list[i]; if (m->method_name = = SE L) {return m;}} return nil; }
  • The lookup function is an inline function, passing in old_method_list and SEL, returning Old_method
  • First, the old_method_list array is empty, and if it is empty, nil is returned.
  • Iterate through the old_method_list array, and according to the SEL match, find Old_method

4 performance optimizations for function calls

In the above section we have finished the basic procedure of the function call. After reading the above section, you may wonder: Objective-c's function calls are so complex that it can cause very slow running. After all, every call to a function has to go through so many processes. Don't worry, in fact, in the process of calling Apple to do some performance optimization, so that its call is not much more than the C language. Let's take a look at what performance optimizations are being done:

4.1 Sel Use Everyone may have doubts, the front has been said Sel,sel what is a thing? An opaque type that represents a method selector. typedef struct OBJC_SELECTOR *sel; The Apple's official explanation for the SEL is an opaque type, which represents a method selector.
The SEL essence is actually an int type address that points to the stored method name. For each class, a special empty space is allocated that stores the method name in the class, and the SEL is the address that points to the corresponding method name. Because the method name string is unique, the SEL is unique.
Why not use the SEL directly with the method name? This issue did not find more official information, personally think because the method name is a string, sel is an int type, the use of the int type is more convenient, more efficient (especially when comparing equality, the comparison of the string is much less efficient than int)

4.2 Cache Use Let's take a closer look at the function call process: Obj->isa->methodlists Old_method->method_imp

      • Because Isa is a member variable of obj, Methodlists is a member variable of ISA, so you can get it directly with obj methodlists
      • Since Method_imp is a member variable of Old_method, it is possible to get method_imp directly with Old_method so that the main time of the function call process is spent looking for methodlists in Old_method.
        The cache is used to optimize the search process.
        We can make the cache simple as a hash table, key is Sel,value is Old_method. Finding Old_method from the cache is fairly simple and efficient.
        The real process of finding old_method from Methodlists is divided into the following two steps:
      • Find the Old_method in the cache via SEL, if you find a direct return, if no execution 2 is found
      • Find Old_method in Methodlists, find the first to insert the Old_method into the cache to facilitate the next lookup, and then return to Old_method, the first time a function is called, it is slower, because the cache does not have this function, It's going to be pretty fast the second time you call.
      • http://blog.zuics.com/runloop-runtime/

iOS Development in Runloop,runtime

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.