Speed of direct CILS vs interfaces vs delegates vs events als

Source: Internet
Author: User
Speed of direct CILS vs interfaces vs delegates vs events als

Consider the following code:

interface IProcessor
{
    void Process();
}
class Processor: IProcessor
{
    public void Process()
    {
    }
}

If I write something like:

Processor P = new processor ();
P. Process ();

The compiler will emit code that tightly binds to processor. Process (). In other words, the only function that cocould be called is that function.

That means that the JIT can easily inline the function call, eliminating the function call overhead totally. A Discussion of when the JIT will and won't inline is beyond the scope of this post, but suffice it to say that functions below a given size will be inlined, subject to some constraints.

A brief aside: Even though C # is doing a direct call, you'll find that it's using the callvirt (ie virtual call) to do it. it does this because callvirt has a built-in null check, which means you get an exception on the invocation, rather than on the dereference inside the function.

Anyway, the direct call can easily be inlined, which is very good from a speed perspective.

But what if I have code like this:

Class D
{
Public void dispatch (iprocessor processor)
{
Processor. Process ();
}
}

Processor P = new processor ();
D = new D ();
D. Dispatch (P );

In the calling code, we know that the function cocould only be processor. process (), but in the dispatch () function, all the compiler knows is that it has an iprocessor reference, which cocould be pointing to any instance of an type that implementes iprocessor.

There is therefore no way for the JIT to inline this call-the fact that there is a level of indirection in interfaces prevents it. That's the source of the slowdown.

Virtual functions present a similar case. like interfaces, there's a level of indirection, And the compiler can't know what type it will really get (OK, perhaps it can, but I'll cover that later.

Delegates also have a level of indirection. in the first release. net, our implementation of delegates wasn't as optimal as it was, and had additional overhead abve the non-inlineable overhead. in Whidbey, that overhead is gone, and my tests (don't trust my tests) show that it's about as fast as interfaces, which is pretty much what one wowould expect CT.

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.