Often we're told things like,
If you're calling a method with a return value that doesn't change, take it out of the loop.
for example when writing code like:
for(int i=0; i < Instance.ExpensiveNonChangingMethod(); i++)
{
// Stuff
}
I was wondering if you could some how tell the compiler that given the same inputs (and object instance) you would get the same outputs, so it would know it could move it out of the loop as part of the optimisation process.
Does anything like this exist? Perhaps something like code-contracts or spec#? Or can they already do this sort of thing?
Is it conceivable that the compiler could reason that a method is a pure function its-self?
Is there anything I can use in C# that will enable the compiler to perform more optimisation that it otherwise would? Yes i'm aware of premature optimisation. I'm asking mostly out of curiosity, but also if something like above did exist it would be 'for free' once the method was marked.