[Mono-dev] [PATCH] Fold a + 'b' + c to a + "b" + c in mcs

Marek Safar marek.safar at seznam.cz
Tue Oct 11 02:59:57 EDT 2005


Hello Ben,

>Today I noticed a perf issue in corlib in a place where we did a + 'b' +
>c. The constant char in this case needs to be boxed and a string
>allocated for its value. A better way to write this is a + "b" + c,
>which saves two allocations.
>  
>
Good idea, so why not extend this to all other constant cases like. "a" 
+ 1 + "c" etc.

Marek

>The attached patch does this at the mcs level. The pattern occurs a few
>times in the mono classlibs, so this appears to be generally useful.
>
>-- Ben
>  
>
>------------------------------------------------------------------------
>
>Index: expression.cs
>===================================================================
>--- expression.cs	(revision 51070)
>+++ expression.cs	(working copy)
>@@ -3343,6 +3343,10 @@
> 		
> 		public void Append (EmitContext ec, Expression operand)
> 		{
>+			// Change a + 'b' + c to a + "b" + c
>+			if (operand is CharConstant)
>+				operand = new StringConstant (((CharConstant) operand).GetValue ().ToString (),operand.Location);
>+			
> 			//
> 			// Constant folding
> 			//
>@@ -3373,6 +3377,13 @@
> 		public override void Emit (EmitContext ec)
> 		{
> 			MethodInfo concat_method = null;
>+
>+			// happens when you have "a" + 'b'
>+			if (operands.Count == 1) {
>+				((Expression) operands [0]).Emit (ec);
>+				return;
>+			}
>+				
> 			
> 			//
> 			// Do conversion to arguments; check for strings only
>@@ -3405,12 +3416,7 @@
> 			//
> 			switch (operands.Count) {
> 			case 1:
>-				//
>-				// This should not be possible, because simple constant folding
>-				// is taken care of in the Binary code.
>-				//
>-				throw new Exception ("how did you get here?");
>-			
>+				throw new Exception ("How did you get here?");
> 			case 2:
> 				concat_method = is_strings_only ? 
> 					TypeManager.string_concat_string_string :
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Mono-devel-list mailing list
>Mono-devel-list at lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-devel-list
>  
>




More information about the Mono-devel-list mailing list