<story><title>An Illustrated History of objc_msgSend</title><url>http://sealiesoftware.com/msg/index.html</url></story><parent_chain><item><author>taspeotis</author><text>Does anyone know if this function is actually written in asm? Case in point:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    The code was probably originally written for NeXT
    by an engineer who was familiar with load-store
    architectures like PowerPC but not so familiar with
    register-memory architectures like x86 ... Those of
    you who do know x86 better may be able to identify
    some of the inefficiencies in this code
&lt;/code&gt;&lt;/pre&gt;
Why wouldn&amp;#x27;t&amp;#x2F;couldn&amp;#x27;t this function be written in C (are there any instructions that would need some non-portable intrinsics?) and leave it to an optimizing compiler to get the instructions right. Sure, sending messages is low-level and needs to be high performance but that, to me, doesn&amp;#x27;t necessitate &amp;quot;we have to do this by hand&amp;quot; asm instead of C.</text></item></parent_chain><comment><author>anextio</author><text>Because objc_msgSend exists outside of the normal ABI and calling convention.&lt;p&gt;If you were to write it in C then unless you tore out the ASM generated by the compiler manually you&amp;#x27;d have calls to objc_msgSend in the call stack in between every method call, and even still you&amp;#x27;d probably emit a bunch of register spills and argument handling.&lt;p&gt;The thing is, calls to objc_msgSend are _set up_ at the call site as if it were a call to the class&amp;#x27;s method&amp;#x27;s function pointer. objc_msgSend is a small trampoline that sits between the call site and the method call, it&amp;#x27;s not a proper function.&lt;p&gt;Besides, for something so small, getting a few good engineers (probably engineers who have a background in writing optimizing compilers) to hand-optimize the code and improve on it with new ideas every major release is probably the best option for everybody.&lt;p&gt;Consider also that objc_msgSend doesn&amp;#x27;t exist on its own. objc_msgSend_stret handles methods that return a struct, which is a pain in the butt most of the time, and there&amp;#x27;s also a bunch of hand-rolled variable-argument trampolines for dealing with ObjC blocks (IIRC the file is called a1a2_blocktramps.a, if you want to see something truly disturbing) that do things like calculating byte offsets and jumping to them based on how many arguments we have.&lt;p&gt;I&amp;#x27;m going off on a tangent now but hopefully the point is made. These procedures are so far removed from normal function calls that writing them in C is not the best approach.</text></comment>
<story><title>An Illustrated History of objc_msgSend</title><url>http://sealiesoftware.com/msg/index.html</url></story><parent_chain><item><author>taspeotis</author><text>Does anyone know if this function is actually written in asm? Case in point:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    The code was probably originally written for NeXT
    by an engineer who was familiar with load-store
    architectures like PowerPC but not so familiar with
    register-memory architectures like x86 ... Those of
    you who do know x86 better may be able to identify
    some of the inefficiencies in this code
&lt;/code&gt;&lt;/pre&gt;
Why wouldn&amp;#x27;t&amp;#x2F;couldn&amp;#x27;t this function be written in C (are there any instructions that would need some non-portable intrinsics?) and leave it to an optimizing compiler to get the instructions right. Sure, sending messages is low-level and needs to be high performance but that, to me, doesn&amp;#x27;t necessitate &amp;quot;we have to do this by hand&amp;quot; asm instead of C.</text></item></parent_chain><comment><author>chrisdevereux</author><text>objc_msgSend would actually be impossible to implement in C.&lt;p&gt;Its role is to look up the function pointer that implements a method, then call that function pointer with the arguments passed to objc_msgSend, returning the result of the implementation.&lt;p&gt;Implementing objc_msgSend in asm guarantees that the argument &amp;amp; return registers aren&amp;#x27;t touched by the method lookup. Similarly to how ffi libraries and implementations of setjmp&amp;#x2F;longjmp need to be implemented in asm, it operates at a lower level than C&amp;#x27;s stack &amp;amp; function abstractions.</text></comment>