Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid boxing in string concatenation #35006

Merged
merged 12 commits into from
Jul 15, 2019

Large diffs are not rendered by default.

145 changes: 70 additions & 75 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenDeconstructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,28 @@ public void Deconstruct(out int a, out string b)
comp.VerifyDiagnostics();
comp.VerifyIL("C.Main", @"
{
// Code size 40 (0x28)
// Code size 43 (0x2b)
.maxstack 3
.locals init (string V_0, //y
int V_1,
string V_2)
.locals init (long V_0, //x
string V_1, //y
int V_2,
string V_3)
IL_0000: newobj ""C..ctor()""
IL_0005: ldloca.s V_1
IL_0007: ldloca.s V_2
IL_0005: ldloca.s V_2
IL_0007: ldloca.s V_3
IL_0009: callvirt ""void C.Deconstruct(out int, out string)""
IL_000e: ldloc.1
IL_000e: ldloc.2
IL_000f: conv.i8
IL_0010: ldloc.2
IL_0011: stloc.0
IL_0012: box ""long""
IL_0017: ldstr "" ""
IL_001c: ldloc.0
IL_001d: call ""string string.Concat(object, object, object)""
IL_0022: call ""void System.Console.WriteLine(string)""
IL_0027: ret
IL_0010: stloc.0
IL_0011: ldloc.3
IL_0012: stloc.1
IL_0013: ldloca.s V_0
IL_0015: call ""string long.ToString()""
IL_001a: ldstr "" ""
IL_001f: ldloc.1
IL_0020: call ""string string.Concat(string, string, string)""
IL_0025: call ""void System.Console.WriteLine(string)""
IL_002a: ret
}");
}

Expand Down Expand Up @@ -190,24 +193,27 @@ public void Deconstruct(out int a, out string b)
comp.VerifyDiagnostics();
comp.VerifyIL("C.Main", @"
{
// Code size 39 (0x27)
// Code size 42 (0x2a)
.maxstack 3
.locals init (string V_0, //y
int V_1,
string V_2)
.locals init (int V_0, //x
string V_1, //y
int V_2,
string V_3)
IL_0000: newobj ""C..ctor()""
IL_0005: ldloca.s V_1
IL_0007: ldloca.s V_2
IL_0005: ldloca.s V_2
IL_0007: ldloca.s V_3
IL_0009: callvirt ""void C.Deconstruct(out int, out string)""
IL_000e: ldloc.1
IL_000f: ldloc.2
IL_0010: stloc.0
IL_0011: box ""int""
IL_0016: ldstr "" ""
IL_001b: ldloc.0
IL_001c: call ""string string.Concat(object, object, object)""
IL_0021: call ""void System.Console.WriteLine(string)""
IL_0026: ret
IL_000e: ldloc.2
IL_000f: stloc.0
IL_0010: ldloc.3
IL_0011: stloc.1
IL_0012: ldloca.s V_0
IL_0014: call ""string int.ToString()""
IL_0019: ldstr "" ""
IL_001e: ldloc.1
IL_001f: call ""string string.Concat(string, string, string)""
IL_0024: call ""void System.Console.WriteLine(string)""
IL_0029: ret
}");
}

Expand Down Expand Up @@ -1159,7 +1165,7 @@ static void Main()
int y;

(x, x, x, x, x, x, x, x, x, y) = (1, 1, 1, 1, 1, 1, 1, 1, 4, 2);
System.Console.WriteLine(x + "" "" + y);
System.Console.WriteLine(string.Concat(x, "" "", y));
canton7 marked this conversation as resolved.
Show resolved Hide resolved
}
}
";
Expand Down Expand Up @@ -3019,18 +3025,21 @@ static void Main()
comp.VerifyDiagnostics();
comp.VerifyIL("C.Main", @"
{
// Code size 29 (0x1d)
// Code size 32 (0x20)
.maxstack 3
.locals init (string V_0) //x2
.locals init (int V_0, //x1
string V_1) //x2
IL_0000: ldc.i4.1
IL_0001: ldstr ""hello""
IL_0006: stloc.0
IL_0007: box ""int""
IL_000c: ldstr "" ""
IL_0011: ldloc.0
IL_0012: call ""string string.Concat(object, object, object)""
IL_0017: call ""void System.Console.WriteLine(string)""
IL_001c: ret
IL_0001: stloc.0
IL_0002: ldstr ""hello""
IL_0007: stloc.1
IL_0008: ldloca.s V_0
IL_000a: call ""string int.ToString()""
IL_000f: ldstr "" ""
IL_0014: ldloc.1
IL_0015: call ""string string.Concat(string, string, string)""
IL_001a: call ""void System.Console.WriteLine(string)""
IL_001f: ret
}");
}

Expand Down Expand Up @@ -4078,7 +4087,7 @@ static void Main()
comp.VerifyDiagnostics();
comp.VerifyIL("C.Main",
@"{
// Code size 91 (0x5b)
// Code size 75 (0x4b)
.maxstack 4
.locals init ((int, int)[] V_0,
int V_1,
Expand All @@ -4088,7 +4097,7 @@ .locals init ((int, int)[] V_0,
IL_0005: stloc.0
IL_0006: ldc.i4.0
IL_0007: stloc.1
IL_0008: br.s IL_0054
IL_0008: br.s IL_0044
IL_000a: ldloc.0
IL_000b: ldloc.1
IL_000c: ldelem ""System.ValueTuple<int, int>""
Expand All @@ -4097,38 +4106,24 @@ .locals init ((int, int)[] V_0,
IL_0017: stloc.2
IL_0018: ldfld ""int System.ValueTuple<int, int>.Item2""
IL_001d: stloc.3
IL_001e: ldc.i4.4
IL_001f: newarr ""object""
IL_0024: dup
IL_0025: ldc.i4.0
IL_0026: ldloc.2
IL_0027: box ""int""
IL_002c: stelem.ref
IL_002d: dup
IL_002e: ldc.i4.1
IL_002f: ldstr "" ""
IL_0034: stelem.ref
IL_0035: dup
IL_0036: ldc.i4.2
IL_0037: ldloc.3
IL_0038: box ""int""
IL_003d: stelem.ref
IL_003e: dup
IL_003f: ldc.i4.3
IL_0040: ldstr "" - ""
IL_0045: stelem.ref
IL_0046: call ""string string.Concat(params object[])""
IL_004b: call ""void System.Console.Write(string)""
IL_0050: ldloc.1
IL_0051: ldc.i4.1
IL_0052: add
IL_0053: stloc.1
IL_0054: ldloc.1
IL_0055: ldloc.0
IL_0056: ldlen
IL_0057: conv.i4
IL_0058: blt.s IL_000a
IL_005a: ret
IL_001e: ldloca.s V_2
IL_0020: call ""string int.ToString()""
IL_0025: ldstr "" ""
IL_002a: ldloca.s V_3
IL_002c: call ""string int.ToString()""
IL_0031: ldstr "" - ""
IL_0036: call ""string string.Concat(string, string, string, string)""
IL_003b: call ""void System.Console.Write(string)""
IL_0040: ldloc.1
IL_0041: ldc.i4.1
IL_0042: add
IL_0043: stloc.1
IL_0044: ldloc.1
IL_0045: ldloc.0
IL_0046: ldlen
IL_0047: conv.i4
IL_0048: blt.s IL_000a
IL_004a: ret
}");
}

Expand Down
Loading