Skip to content

Instantly share code, notes, and snippets.

@rogeralsing
Last active August 29, 2015 14:05
Show Gist options
  • Save rogeralsing/965c3ba740f0c43bd90e to your computer and use it in GitHub Desktop.
Save rogeralsing/965c3ba740f0c43bd90e to your computer and use it in GitHub Desktop.
public class Foo<T> where T: IDoStuff
{
private T _stuffer;
public Foo(T stuffer)
{
_stuffer = stuffer;
}
public void Bar()
{
//will this be a direct call or resolved through interface lookup?
_stuffer.StuffTheStuff();
}
}
@rogeralsing
Copy link
Author

since _stuffer is of a concrete type when used, will the calls to it's methods be direct?
or will they be resolved through the interface IDoStuffdue to the constraint?

in short, is this faster, than if the _stuffer field would have been declared as IDoStuff

@mgravell
Copy link

It will be via the interface, but as a "constrained call". This has the advantage that it works without boxing if T is a value type that implements the interface. If you just type the parameter / field / whatever as the interface it will force value-types to be boxed. Likewise, assigning a T to an IDoStuff will force value-type to be boxed; keeping it as a T throughout: does not.

Constrained calls result in virtual call if T is a reference-type, and static call if T is a value-type that implements or overrides the method. If you call .ToString / .Equals etc and the value-type doesn't override them, it will have to box/virtual-call. It makes this decision at the JIT stage per distinct final T: the IL is identical for all T. A good reason for custom structs to always override all the object virtual methods! Yet another reason why the JIT output can be shared for all ref - types, but needs to be per value-type: it isn't just the size - individual method calls can be very different depending on what methods the value-type overrides.

@rogeralsing
Copy link
Author

Awesome answer!
Too bad I couldn't hack my way past virtual lookup though.

@mgravell
Copy link

Well you can: use structs :)

But really, I don't think that is going to be an important part of any performance issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment