casting vs creating an instance

  • Thread starter Yankee Imperialist Dog
  • Start date
J

Jon Skeet [C# MVP]

Analysis ought to show that the return value is unused and optimize away the
cast.  The function itself can't be optimized away because you've specified
NoInlining, of course, and I guess that prevents the JIT from inspecting it
for side effects and removing it, but there's no such barrier to eliminating
storage of the return value, is there?

True, true. I did at one point use the value, but removed it to keep
things simpler.

Would you expect a simple nullity test to be enough to prohibit that?

(Of course the return value *is* used when there's a cast, because the
cast might fail.)

Jon
 
B

Ben Voigt [C++ MVP]

Jon said:
True, true. I did at one point use the value, but removed it to keep
things simpler.

Would you expect a simple nullity test to be enough to prohibit that?

(Of course the return value *is* used when there's a cast, because the
cast might fail.)

Good point. I guess whether you think the compiler needs to preserve that
depends on whether you see the InvalidCastException as error recovery or
general-purpose flow control. So ultimately it is a side-effect which is
part of the public contract and the compiler can't remove it.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
Good point. I guess whether you think the compiler needs to preserve that
depends on whether you see the InvalidCastException as error recovery or
general-purpose flow control. So ultimately it is a side-effect which is
part of the public contract and the compiler can't remove it.

Yes, I think it's sufficiently well-defined that it can't be removed.

However, the storage *could* be removed for the non-casting version,
which is what I thought you were originally referring to :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Object casting 2
GridView question 1
nested gridview 1
RowDataBound trouble 4
GridView question 1
GridView Setting a checkbox 1
GridView edit mode issue 2
trying to access the row.databound 2

Top