Re: Boxing and Unboxing ??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




Jesse McGrew wrote:
Peter Olcott wrote:
"Jesse McGrew" <jmcgrew@xxxxxxxxx> wrote in message
news:1168918374.578116.244610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Why should the programmer have to change his code just to enable an
optimization? Why not just say "your methods will run faster if you
don't modify large value-type parameters"?

This aspect is not a matter of optimization, it is a matter of preventing
programming errors. By using the [const] parameter qualifier it is impossible to
inadvertently change a function parameter that was not intended to be changed.

It's already impossible! Those errors don't exist today: if you pass a
value type into a method, any changes the method makes will only affect
the local copy inside that method; the original value in the caller
won't be changed.

The potential errors are purely a side effect of the *optimization*
you've been proposing, which is to pass "in" value types by reference
even though you don't expect to get a changed value back from the
method (which is the purpose of "ref" and "out" parameters). If you
weren't passing them by reference, there'd be no need for those
parameters to be read-only, because any changes would be local to the
method being called.

Jesse, I think that you misunderstand. C++ has a concept of "const" by
which you can pass a reference type, but can indicate that none of its
fields can be changed.

To my knowledge, the C# team is considering such a construct, but they
still haven't figured out how to fit it cleanly into the language (and
possibly the CLR) taking into account security concerns, etc.

.



Relevant Pages

  • Re: Garbage collection problem
    ... The whole point of optimization is to ... > same camp as Chris Smith on the matter. ... declare a scope for the variable I may be depending on the object remaining ...
    (comp.lang.java.programmer)
  • Re: Boxing and Unboxing ??
    ... This aspect is not a matter of optimization, it is a matter of preventing ... which is to pass "in" value types by reference ... I can see the merits of applying a "const" attribute to reference types ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Evolution of PL/I
    ... Doesn't matter, the optimization is the samer anyway. ... create the trees and the optimizer will do what it can, maybe fold constants, ... You may end up generating differnet instructions sequences depending on the architecure. ...
    (comp.lang.pl1)
  • Re: Boxing and Unboxing ??
    ... don't modify large value-type parameters"? ... This aspect is not a matter of optimization, it is a matter of preventing ... which is to pass "in" value types by reference ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Efficiently passing strings?
    ... > I'm instantiating the same string object twice (once for the parameter ... first is probably fine after optimization. ... compiler; there's really no other way to tell. ... using a reference exposes some of your implementation. ...
    (alt.comp.lang.learn.c-cpp)