[Mono-dev] mono numerical performance

Jonathan Shore jonathan.shore at gmail.com
Sun Nov 20 12:33:31 EST 2011


Yes, I see what you are doing.  There is no way to modify the structure.  However doesn't it still boil down to determining whether a local variable reference is changed.  So for instance:

FinalBox<int> len = <some expression>
for (int i = 0 ; i < len ; i++)
	<do something on array[i]>

vs

var len = <some expression>
for (int i = 0 ; i < len ; i++)
	<do something on array[i]>

In both of these cases, the variable len may not be invariant in that could be re-assigned somewhere within the loop.  Of course one could check this.   A check for invariance within a scope should not be all that hard I would think.   So I think these are equivalent problems.

A much harder case, would be if the variable is a member of the class, outside of the local scope of the function.    In the context of threading it would be possible for state to change.


On Nov 20, 2011, at 12:22 PM, Konrad M. Kruczyński wrote:

> Hi again,
> 
>> 
>> It is too bad that one cannot declare a primitive expression to be
>> locally const in C#.  
> 
> Maybe one could get away with something like this:
> 
>    public struct FinalBox<T>
>    {
>        public FinalBox(T value)
>        {
>            this.value = value;
>        }
> 
>        public T Value
>        {
>            get
>            {
>                return value;
>            }
>        }
> 
>        public static implicit operator T(ReadOnlyBox<T> box)
>        {
>            return box.value;
>        }
> 
>        private readonly T value;
>    }
> 
> Such construct may inform JIT that given value won't change (if
> getter/operator is inlined which I hope happens). And with implicit
> operator it's quite usable.
> 
> --
> Regards,
> Konrad
> 



More information about the Mono-devel-list mailing list