The principle of "go" objective-c message mechanism

Source: Internet
Author: User

Transferred from: http://dangpu.sinaapp.com/?p=119

In Objective-c, the true implementation of the message and method is bound at the execution stage, not the compile phase. The compiler translates the message into a call to the Objc_msgsend method.

The Objc_msgsend method contains two necessary parameters: Receiver, method name (i.e.: selector), such as: [Receiver message];     will be converted to: objc_msgsend (receiver, selector);      The Objc_msgsend method can hold the parameters of the message, such as: Objc_msgsend (receiver, selector, arg1, arg2, ...); The Objc_msgsend method does the following in order to complete the dynamic binding:
    1. Find the program that selector refers to (the true implementation of the method). Because different classes have different implementations of the same method, finding a true implementation of a method depends on the class of receiver
    2. Call the implementation and pass a series of parameters past
    3. Returns the return value of the implementation as its own return value, returning the
The key to messaging is the data structure that the compiler uses to build each class and object. Each class consists of the following two necessary elements:
    • A pointer to the parent class
    • A dispatch table (Dispatch table). The dispatch table associates the selector of the class with the actual memory address of the method

Each object has a pointer to the owning class, Isa. With this pointer, the object can find the class it belongs to, and it finds all of its parent classes, as shown in: When sending a message to an object, the Objc_msgsend method finds the object's class based on the object's Isa pointer, and then finds the selector in the class's dispatch table (Dispatch table). If the selector,objc_msgsend cannot be found, locate the parent class by pointing to the parent class's pointer, and look for selector in the parent class's dispatch table (Dispatch table), and so on until the NSObject class. Once the Selector,objc_msgsend method is found, the implementation is called based on the memory address of the dispatch table.     In this way, the true implementation of the message and method is bound at the execution stage. To ensure the efficiency of message delivery and execution, the system caches the memory addresses of all selector and used methods. Each class has a separate cache that contains the current class's own selector and the selector inherited from the parent class.  Before the dispatch table (Dispatch table) is found, the message sending system first checks the receiver object's cache. In the case of cache hits, message sending (messaging) is only a little bit slower than calling methods directly (function call).

The principle of "go" objective-c message mechanism

Related Article

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.