Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Errrhhh ..... CSharp or VB
Message
De
28/09/2011 02:42:45
 
 
À
27/09/2011 13:25:20
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01524423
Message ID:
01525009
Vues:
82
>>>>>Actually I wouldn't be surprised if the compiler optimised it out to
>>>>>string s = "TomDickHarry";
>>>>>but I haven't time to check :-{
>>>>>OTOH if the values weren't literals I think you'd be right.....
>>>>
>>>>
>>>>I have checked with ild
>>>>
>>>>The compiler optimises
>>>>- class properties, ie string s1 = "A" + "b" + "C";
>>>>- assignments in methode, ie string xx = "D" + "E" + "F";
>>>
>>>Again, I'm pretty sure that at least in one of the previous versions there was an upper limit
>>>on the number of operands optimizable this way - AFAIR 4. Dunno if that still is the case.
>>>
>>>my 0.02€
>>>
>>>thomas
>>
>>
>>tested with 12 - still one string assignment
>
>Thx. Did you also confirm the behaviour you expected when using variables rather than literals ?

Just did - and the results were not what I expected

(1) In the case of the static initialisation ( see listing 1), it creates an array, stores the reference of each field (sa-sg) in the array, puts the array on the stack, then calls String.Concat( string[] )

In a method Main - see listing 2
(2) adding 7 variables - same as (1)

(3) adding two variables. Put the references of each of the variables on the stack, then call String.Concat( string, string)

(4) adding 4 variables - same as (3) but ends with calling String.Concat( string, string, string, string)
Think that is the number 4 Thomas remembered. Beyond 4 there's the overhead of creating the array


I would expect (3) and (4) to apply for static initialisation as well

Conclusion
(a) It was not what I expected it to be
(b) you were right
(c) it optimises where possible. By putting individual string references on the stack, then calling concat. When it cannot put individual string references on the stack any more, it populates an array with the individual string references and calls Concat( string[] )

Concat signatures are here http://msdn.microsoft.com/en-us/library/0wkb0y3w.aspx
	class Program
	{
		static string sa = "a";
		static string sb = "b";
		static string sc = "c";
		static string sd = "d";
		static string se = "e";
		static string sf = "f";
		static string sg = "g";
		static string s = sa + sb + sc + sd + se + sf + sg;




		static void Main(string[] args)
		{


			string sa = "a";
			string sb = "b";
			string sc = "c";
			string sd = "d";
			string se = "e";
			string sf = "f";
			string sg = "g";
			string s = sa + sb + sc + sd + se + sf + sg;



			string x = sa + sb;
			string pp = sa + sb + sc + sd;
		}
	}
Listing1 - static constructor
.method private hidebysig specialname rtspecialname static 
        void  .cctor() cil managed
{
  // Code size       145 (0x91)
  .maxstack  3
  .locals init ([0] string[] CS$0$0000)
  IL_0000:  ldstr      "a"
  IL_0005:  stsfld     string CoreTest.Program::sa
  IL_000a:  ldstr      "b"
  IL_000f:  stsfld     string CoreTest.Program::sb
  IL_0014:  ldstr      "c"
  IL_0019:  stsfld     string CoreTest.Program::sc
  IL_001e:  ldstr      "d"
  IL_0023:  stsfld     string CoreTest.Program::sd
  IL_0028:  ldstr      "e"
  IL_002d:  stsfld     string CoreTest.Program::se
  IL_0032:  ldstr      "f"
  IL_0037:  stsfld     string CoreTest.Program::sf
  IL_003c:  ldstr      "g"
  IL_0041:  stsfld     string CoreTest.Program::sg
  IL_0046:  ldc.i4.7
  IL_0047:  newarr     [mscorlib]System.String
  IL_004c:  stloc.0
  IL_004d:  ldloc.0
  IL_004e:  ldc.i4.0
  IL_004f:  ldsfld     string CoreTest.Program::sa
  IL_0054:  stelem.ref
  IL_0055:  ldloc.0
  IL_0056:  ldc.i4.1
  IL_0057:  ldsfld     string CoreTest.Program::sb
  IL_005c:  stelem.ref
  IL_005d:  ldloc.0
  IL_005e:  ldc.i4.2
  IL_005f:  ldsfld     string CoreTest.Program::sc
  IL_0064:  stelem.ref
  IL_0065:  ldloc.0
  IL_0066:  ldc.i4.3
  IL_0067:  ldsfld     string CoreTest.Program::sd
  IL_006c:  stelem.ref
  IL_006d:  ldloc.0
  IL_006e:  ldc.i4.4
  IL_006f:  ldsfld     string CoreTest.Program::se
  IL_0074:  stelem.ref
  IL_0075:  ldloc.0
  IL_0076:  ldc.i4.5
  IL_0077:  ldsfld     string CoreTest.Program::sf
  IL_007c:  stelem.ref
  IL_007d:  ldloc.0
  IL_007e:  ldc.i4.6
  IL_007f:  ldsfld     string CoreTest.Program::sg
  IL_0084:  stelem.ref
  IL_0085:  ldloc.0
  IL_0086:  call       string [mscorlib]System.String::Concat(string[])
  IL_008b:  stsfld     string CoreTest.Program::s
  IL_0090:  ret
} // end of method Program::.cctor
listing 2

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 124 (0x7c)
.maxstack 4
.locals init ([0] string sa,
[1] string sb,
[2] string sc,
[3] string sd,
[4] string se,
[5] string sf,
[6] string sg,
[7] string s,
[8] string x,
[9] string pp,
[10] string[] CS$0$0000)
IL_0000: nop
IL_0001: ldstr "a"
IL_0006: stloc.0
IL_0007: ldstr "b"
IL_000c: stloc.1
IL_000d: ldstr "c"
IL_0012: stloc.2
IL_0013: ldstr "d"
IL_0018: stloc.3
IL_0019: ldstr "e"
IL_001e: stloc.s se
IL_0020: ldstr "f"
IL_0025: stloc.s sf
IL_0027: ldstr "g"
IL_002c: stloc.s sg
IL_002e: ldc.i4.7
IL_002f: newarr [mscorlib]System.String
IL_0034: stloc.s CS$0$0000
IL_0036: ldloc.s CS$0$0000
IL_0038: ldc.i4.0
IL_0039: ldloc.0
IL_003a: stelem.ref
IL_003b: ldloc.s CS$0$0000
IL_003d: ldc.i4.1
IL_003e: ldloc.1
IL_003f: stelem.ref
IL_0040: ldloc.s CS$0$0000
IL_0042: ldc.i4.2
IL_0043: ldloc.2
IL_0044: stelem.ref
IL_0045: ldloc.s CS$0$0000
IL_0047: ldc.i4.3
IL_0048: ldloc.3
IL_0049: stelem.ref
IL_004a: ldloc.s CS$0$0000
IL_004c: ldc.i4.4
IL_004d: ldloc.s se
IL_004f: stelem.ref
IL_0050: ldloc.s CS$0$0000
IL_0052: ldc.i4.5
IL_0053: ldloc.s sf
IL_0055: stelem.ref
IL_0056: ldloc.s CS$0$0000
IL_0058: ldc.i4.6
IL_0059: ldloc.s sg
IL_005b: stelem.ref
IL_005c: ldloc.s CS$0$0000
IL_005e: call string [mscorlib]System.String::Concat(string[])
IL_0063: stloc.s s
IL_0065: ldloc.0
IL_0066: ldloc.1
IL_0067: call string [mscorlib]System.String::Concat(string,
string)
IL_006c: stloc.s x
IL_006e: ldloc.0
IL_006f: ldloc.1
IL_0070: ldloc.2
IL_0071: ldloc.3
IL_0072: call string [mscorlib]System.String::Concat(string,
string,
string,
string)
IL_0077: stloc.s pp
IL_0079: br.s IL_007b
IL_007b: ret
} // end of method Program::Main
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform