> I'm at a bit of a loss to understand why it would be ok to
> *not* box that integer. WriteLine is expecting an object. It
There is no doubt that when Writeline in called it expects an object,
the question is whether this boxing takes place when the argument is
passed to Writeline or prepared in advance. Furthermore, it seems unwise
that in the following code would perform boxing 10,000 (0x2710) times.
for(int i=0; i<10000; i++) WriteLine("{0}",1);
Hope the listing would be formatted ok:
// compiled with Microsoft Visual C# 2005 Compiler v. 8.00.50727.42
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 39 (0x27)
.maxstack 2
.locals init ([0] int32 i,
[1] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: br.s IL_001a
IL_0005: ldstr "{0}"
// loop start
IL_000a: ldc.i4.1
IL_000b: box [mscorlib]System.Int32
// box
IL_0010: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_0015: nop
IL_0016: ldloc.0
IL_0017: ldc.i4.1
IL_0018: add
IL_0019: stloc.0
IL_001a: ldloc.0
IL_001b: ldc.i4 0x2710
IL_0020: clt
IL_0022: stloc.1
IL_0023: ldloc.1
IL_0024: brtrue.s IL_0005
// jump to loop start
IL_0026: ret
} // end of method Program::Main
> could in theory call GetType on the passed object therefore
> requiring it to definitely be a reference type - unless you
> think we should special case the known primitives (there is
> some code in place special casing situations like boxing Nullable<T>).
> Then we come back to Fabian's point - assuming you have a
> shared boxed int, how long do you expect to keep this shared
> int around? If not forever (which is possible if you think
> about the behaviour of interned strings) then you have just
> created a bunch of gen2 garbage that the GC will have to work
> very hard to collect.
It is a fine point you raise - what is the essential difference between
a string literal and an "int literal"? Sure it must be boxed (once) by
JIT/CSC precompilation. And of course there is no need for all that GC
work, since the number of "literals" in the program, be it string
literals or "int literals" is known in compile time.
Igal
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com