> So don't you think that at compile time it's obvious for the compiler to > create a "compile-time boxed version of the constant", thus no boxing at > all at run time (what I've called "object(1)", below) ? It is really a > trivial optimization taught at most compilation courses - I would expect > to think most compilers would implement it.
I find that rather overstated, as most compilerbuilding courses don't focus on languages which have a separate type system for value types and object types. 'Boxing' is a term .NET uses because it has optimized paths for value types. That doesn't mean it's CompilerBuilding 101 to optimize this away. Also, isn't object(1) boxing the constant as well? The only optimization which works is if the compiler rephrases the statement as: Console.WriteLine("{0} {1}", 1.ToString(),(new MyStruct()).ToString())); Otherwise there still has to be passed an object to the framework routine. FB, who finds this kind of optimizations rather useless as one event raise completely demolishes all optimizations done this way in one swoop. > > Igal Yoffe > > > > > > -----Original Message----- > > From: Discussion of advanced .NET topics. > > [mailto:[EMAIL PROTECTED] On Behalf Of > > Richard Blewett > > Sent: Tuesday, August 07, 2007 11:09 AM > > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > > Subject: Re: [ADVANCED-DOTNET] compiler considerations > > > > This will always cause one box operation. The version of > > WriteLine that will be called will be the one with the signature > > > > WriteLine(string format, params object[] args); > > > > Therefore all the args will be treated as object - this means > > the 1 will be boxed. However, the second argument due to > > operator precedence will create the struct, and call its > > ToString method. The compiler knows exactly which ToString > > method must be called - if MyStruct overrides ToString then > > it will be the MyStruct version if not it will be > > System.Object's version - therefore, no virtual dispatch is > > required and therefore no boxing. > > > > Regards > > > > Richard Blewett - DevelopMentor > > > > > -----Original Message----- > > > From: Discussion of advanced .NET topics. [mailto:ADVANCED- > > > [EMAIL PROTECTED] On Behalf Of Igal Yoffe > > > Sent: 07 August 2007 08:20 > > > To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM > > > Subject: [ADVANCED-DOTNET] compiler considerations > > > > > > Consider the following code: > > > Console.Writeline("{0} {2}",1,(new MyStruct()).ToString()); > > > > > > The number of boxings in such code is between 0 to 2, > > depending on the > > > implementation of the compiler. > > > > > > Straightforwardly, two boxings are obvious, viz., int boxing and > > > struct's boxing before calling ToString. However, it could > > be the case > > > that in compile time the compiler creates "object(1)" (really a > > > trivial > > > optimization) for the integer, and moreover have special > > vtable logic > > > for structures (recall that structures could not be > > inherited) which > > > calls ToString directly, thus no boxings at all. > > > > > > While I could examine the ilasm generated by _my_ compiler > > I would be > > > really glad to know what do you think _typical_ (e.g. > > microsoft's csc) > > > compiler would do? > > > > > > =================================== > > > This list is hosted by DevelopMentorR http://www.develop.com > > > > > > View archives and manage your subscription(s) at > > > http://discuss.develop.com > > > > =================================== > > 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 =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com