Permalink
Browse files

Automatically avoid trivial Castclass

  • Loading branch information...
mgravell committed Feb 13, 2013
1 parent 50d444d commit 83efc6cf4c80045da00c3ee2346bdf3c56c0dbb1
Showing with 10 additions and 1 deletion.
  1. +10 −1 Sigil/Emit.CastClass.cs
@@ -41,7 +41,16 @@ public Emit<DelegateType> CastClass(Type referenceType)
FailStackUnderflow(1);
}

UpdateState(OpCodes.Castclass, referenceType, TypeOnStack.Get(referenceType), pop: 1);
var newType = TypeOnStack.Get(referenceType);
if (newType.IsAssignableFrom(top[0]))
{
// already trivially castable; we don't need any IL for this
Stack = Stack.Pop().Push(newType);
}
else
{
UpdateState(OpCodes.Castclass, referenceType, newType, pop: 1);
}

return this;
}

0 comments on commit 83efc6c

Please sign in to comment.