I think the only relevant question is "would it be worth the effort", and probably we don't have enough actual data and usage statistics to answer that question in our current discussion. If however the answer is positive, then you would expect any decent compiler/JIT (see below) to perform that optimization, like you would expect for dead code, constant folding and hoisting. And that even if allocating an object on the heap is negligible. Typical constant folding is also negligible, though there is a difference as constant folding is actually required by the ECMA spec.
I agree that maybe it is more applicable for JIT to do all optimizations, that however doesn't change the original question regarding the number of boxings at the time Console.WriteLine executes (be it JIT or CSC optimization). And if it is, statistically, worth the effort, it would be of course a shame to box that integer. Igal > -----Original Message----- > From: Discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED] On Behalf Of > Fabian Schmied > Sent: Wednesday, August 08, 2007 10:06 AM > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > Subject: Re: [ADVANCED-DOTNET] compiler considerations > > > Arguably, best place to optimize a constant in this case > would still > > be compile time. > > How exactly would you want the C# compiler to optimize this? > As Barry said, the boxed int must live on the heap anyway, so > the only "optimization" you can get is by sharing that heap > object between different boc instructions. Then, however, > there is the question of the lifetime of the box. Should it > be shared and kept alive on method level? On class level? > Application-wide? How many boxed integers (and other > primitives) should be kept alive at a single time? > > And, the most important question, would it be worth the effort? > Allocating a heap object is negligible in nearly all > scenarios anyway; it definitely is in calls to Console.WriteLine. > > All these are questions the JIT compiler can give better > answers to because it knows more about the actual execution > of the code than the C# compiler does. And in addition, it > wouldn't be economic to add optimzers to single .NET language > compilers anyway; optimizations built into the JIT compiler > automatically apply to any .NET code. > > Fabian > > =================================== > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com