T
ThomasR
I understand that virtual methods on inherited objects are slower than
non-virtual methods because of the indirection required to support the
call.
However, when looking at IL code produced by the compiler, I notice
that all methods on object variables use callvirt instead of call,
regardless of whether the method is truly virtual or not.
I assume this allows .NET to check if the object reference is null or
not before invoking the method. However, this seems very wasteful
when many methods are called on 1 object reference.
So, my questions are:
1) Can anyone quantify how much slower callvirt is than call?
2) Why couldn't the compiler check object reference for null on first
call, then replace all subsequent calls (when it knows the object ref
coudln't change) with IL call instead of IL callvirt?
3) If the overhead is at all significant, is there anyway to make the
compiler more efficient?
non-virtual methods because of the indirection required to support the
call.
However, when looking at IL code produced by the compiler, I notice
that all methods on object variables use callvirt instead of call,
regardless of whether the method is truly virtual or not.
I assume this allows .NET to check if the object reference is null or
not before invoking the method. However, this seems very wasteful
when many methods are called on 1 object reference.
So, my questions are:
1) Can anyone quantify how much slower callvirt is than call?
2) Why couldn't the compiler check object reference for null on first
call, then replace all subsequent calls (when it knows the object ref
coudln't change) with IL call instead of IL callvirt?
3) If the overhead is at all significant, is there anyway to make the
compiler more efficient?