Only call string.Format in Debug.Assert if really necessary
authormain() <main@ehvag.eu.org>
Wed, 11 Feb 2015 18:42:29 +0000 (19:42 +0100)
committermain() <main@ehvag.eu.org>
Wed, 11 Feb 2015 18:42:29 +0000 (19:42 +0100)
Right now (without this change), Debug.Assert **always** formats its string parameters, even if doing so wouldn't be necessary because the condition is true. This has a huge impact on code that asserts frequently because it assumes that calling Assert(true, ...) is cheap.

mcs/class/System/System.Diagnostics/Debug.cs

index d4aac126e88b2991b87eaba54e131ef2026c24d8..14790efb1a67220d30ab571f24d01ecd42766725 100644 (file)
@@ -80,6 +80,10 @@ namespace System.Diagnostics {
                public static void Assert (bool condition, string message,
                        string detailMessageFormat, params object [] args)
                {
+                       if (condition)
+                               // Return early to avoid the string formatting
+                               return;
+
                        TraceImpl.Assert (condition,
                                message,
                                string.Format (detailMessageFormat, args));