Redundant Object.ToString() call - Disagree

Hi,

Resharper comments on the following code:

public static void Method2()
{
Console.WriteLine(1.0.ToString() + "TEST");
}

"Redundant Object.ToString() call".

However, I think this should be removed, as it is correct to call ToString() for performance reasons. Reason is that if you don't call ToString, you end up boxing, resulting in additional overhead. Calling the value type's .ToString() implementation and concatenating is definitely faster than boxing.
Some IL as proof:

.method public hidebysig static void Method1() cil managed
{
// Code size 30 (0x1e)
.maxstack 8
IL_0000: ldc.r8 1.
IL_0009: box System.Double
IL_000e: ldstr "TEST"
IL_0013: call string System.String::Concat(object,
object)
IL_0018: call void System.Console::WriteLine(string)
IL_001d: ret
} // end of method Program::Method1


vs.

.method public hidebysig static void Method2() cil managed
{
// Code size 33 (0x21)
.maxstack 2
.locals init ( float64 CS$0$0000)
IL_0000: ldc.r8 1.
IL_0009: stloc.0
IL_000a: ldloca.s CS$0$0000
IL_000c: call instance string System.Double::ToString()
IL_0011: ldstr "TEST"
IL_0016: call string System.String::Concat(string,
string)
IL_001b: call void System.Console::WriteLine(string)
IL_0020: ret
} // end of method Program::Method2

Regards,

Tom

4 comments
Comment actions Permalink

See this thread, which discusses your concerns at some length:

http://www.intellij.net/forums/thread.jspa?messageID=5183414

0
Comment actions Permalink

// Code size 30 (0x1e)
// Code size 33 (0x21)


What does "Code size" measure? Who measured it?


0
Comment actions Permalink

Hi James,

thanks for the link. Just looked over it but I find the opinion of some people a bit stubborn :) Not changing it because the boxing doesn't cost much performance isn't really a reason for me. Anyways, the thread also mentions that I'll be able to turn this off in 3.0... so I can live with it although I still disagree with Intellij opinion :)

Regards,

Tom

0
Comment actions Permalink

Code size measures the byte length of the generated IL code. Ildasm measured it :)
The point of critique here is th "box" operation.

0

Please sign in to leave a comment.