my question as the title says.obviously, the first parameter was used for this pointer , in some taste of c++.what about the second one? thak you.
my question as the title says.obviously, the first parameter was used for this pointer
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The signature of
objc_msgSend()is:Every method call is compiled down to a call to this function. I.e., if you call:
That will be compiled as if it were:
Now, to your question, why do methods get compiled down to a function that has the SEL as the second argument. Or, more specifically, why is this method:
Exactly equivalent to this C function:
The answer is speed speed speed.
Speed
Specifically, by doing this, then
objc_msgSend()never has to rewrite the stack frame* and it can also use a tail call optimization to jump directly to the method invocation. This is the same reason why you never seeobjc_msgSend()in backtraces in the debugger (save for when you actually crash/break in the messenger).objc_msgSend()uses theobjectand the_cmdto look up the implementation of the method and then, quite literally, jumps to that implementation.Very fast. Stack frame untouched.
And, as others have stated, having
_cmdaround in the method implementation can be handy for a variety of reasons. As well, it also means that the messenger can do neat tricks like proxy support via NSInvocation and the like.*rewriting the stack frame can be insanely complex and expensive. Some of the arguments might be in registers some of the time, etc… All architecture dependent ABI nastiness. One of the biggest challenges to writing things like
imp_implementationWithBlock()was figuring out how to do so without touching the stack because doing so would have been too slow and too bloated to be viable.