Checking strings against length seem to have better perf (#36443)
authorYoussef Victor <31348972+Youssef1313@users.noreply.github.com>
Tue, 19 May 2020 13:39:51 +0000 (15:39 +0200)
committerGitHub <noreply@github.com>
Tue, 19 May 2020 13:39:51 +0000 (09:39 -0400)
* Chckecing strings against length seem to have better perf

* Apply suggestions from code review

Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Co-authored-by: Ben Adams <thundercat@illyriad.co.uk>
* Apply suggestions from code review

Co-authored-by: Ben Adams <thundercat@illyriad.co.uk>
* Update src/libraries/System.Drawing.Common/src/System/Drawing/BitmapSelector.cs

Co-authored-by: Ben Adams <thundercat@illyriad.co.uk>
* Apply suggestions from code review

* Update src/libraries/System.Private.CoreLib/src/System/Reflection/AssemblyNameFormatter.cs

* fix build error

* Update SmtpClient.cs

* Update AssemblyNameFormatter.cs

* Update LoggingEventSource.cs

* Fix build error

* Apply suggestions from code review

* Update src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/Syndication/SyndicationContent.cs

* Fix build error

* Apply suggestions from code review

Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Co-authored-by: Ben Adams <thundercat@illyriad.co.uk>
28 files changed:
src/libraries/Common/src/Interop/Linux/cgroups/Interop.cgroups.cs
src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceCounter.cs
src/libraries/System.Diagnostics.PerformanceCounter/src/System/Diagnostics/PerformanceCounterLib.cs
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs
src/libraries/System.Drawing.Common/src/System/Drawing/BitmapSelector.cs
src/libraries/System.IO.IsolatedStorage/src/System/IO/IsolatedStorage/IsolatedStorageFile.cs
src/libraries/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs
src/libraries/System.IO.Packaging/src/System/IO/Packaging/PackUriHelper.PackUriScheme.cs
src/libraries/System.IO.Packaging/src/System/IO/Packaging/PackUriHelper.cs
src/libraries/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs
src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs
src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs
src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddressCollection.cs
src/libraries/System.Net.Mail/src/System/Net/Mail/MailMessage.cs
src/libraries/System.Net.Mail/src/System/Net/Mail/MailPriority.cs
src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs
src/libraries/System.Net.Mail/src/System/Net/Mime/ContentDisposition.cs
src/libraries/System.Net.Mail/src/System/Net/Mime/ContentType.cs
src/libraries/System.Net.Mail/src/System/Net/Mime/HeaderCollection.cs
src/libraries/System.Net.Requests/src/System/Net/FtpControlStream.cs
src/libraries/System.Private.CoreLib/src/System/Reflection/AssemblyNameFormatter.cs
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlJsonWriter.cs
src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs
src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCache.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs
src/mono/netcore/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs

index abb7baa..f2417e2 100644 (file)
@@ -348,7 +348,7 @@ internal static partial class Interop
                                 // cgroup v2: Find the first entry that matches the cgroup v2 hierarchy:
                                 //     0::$PATH
 
-                                if ((lineParts[0] == "0") && (lineParts[1] == string.Empty))
+                                if ((lineParts[0] == "0") && (lineParts[1].Length == 0))
                                 {
                                     path = lineParts[2];
                                     return true;
index f1637f4..37e4b41 100644 (file)
@@ -470,9 +470,9 @@ namespace System.Diagnostics
                     string currentCategoryName = _categoryName;
                     string currentMachineName = _machineName;
 
-                    if (currentCategoryName == string.Empty)
+                    if (currentCategoryName.Length == 0)
                         throw new InvalidOperationException(SR.CategoryNameMissing);
-                    if (_counterName == string.Empty)
+                    if (_counterName.Length == 0)
                         throw new InvalidOperationException(SR.CounterNameMissing);
 
                     if (ReadOnly)
index 93ad673..7facea0 100644 (file)
@@ -455,7 +455,7 @@ namespace System.Diagnostics
                         iniWriter.Write(languageId);
                         iniWriter.Write(HelpSufix);
                         iniWriter.Write("=");
-                        if (categoryHelp == null || categoryHelp == string.Empty)
+                        if (string.IsNullOrEmpty(categoryHelp))
                             iniWriter.WriteLine(SR.HelpNotAvailable);
                         else
                             iniWriter.WriteLine(categoryHelp);
index 998ec9f..af85258 100644 (file)
@@ -536,7 +536,7 @@ namespace System.Diagnostics
                         environmentBlock = GetEnvironmentVariablesBlock(startInfo._environmentVariables!);
                     }
                     string workingDirectory = startInfo.WorkingDirectory;
-                    if (workingDirectory == string.Empty)
+                    if (workingDirectory.Length == 0)
                         workingDirectory = Directory.GetCurrentDirectory();
 
                     bool retVal;
index 06ccdf8..ae11c6e 100644 (file)
@@ -64,7 +64,7 @@ namespace System.Drawing
         /// </summary>
         public static string GetFileName(string originalPath)
         {
-            if (Suffix == string.Empty)
+            if (string.IsNullOrEmpty(Suffix))
                 return originalPath;
 
             string newPath = AppendSuffix(originalPath);
index d7d09f9..1d13427 100644 (file)
@@ -262,7 +262,7 @@ namespace System.IO.IsolatedStorage
             if (path == null)
                 throw new ArgumentNullException(nameof(path));
 
-            if (path == string.Empty)
+            if (path.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
             }
@@ -284,7 +284,7 @@ namespace System.IO.IsolatedStorage
             if (path == null)
                 throw new ArgumentNullException(nameof(path));
 
-            if (path == string.Empty)
+            if (path.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
             }
@@ -306,7 +306,7 @@ namespace System.IO.IsolatedStorage
             if (path == null)
                 throw new ArgumentNullException(nameof(path));
 
-            if (path == string.Empty)
+            if (path.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
             }
@@ -331,12 +331,12 @@ namespace System.IO.IsolatedStorage
             if (destinationFileName == null)
                 throw new ArgumentNullException(nameof(destinationFileName));
 
-            if (sourceFileName == string.Empty)
+            if (sourceFileName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(sourceFileName));
             }
 
-            if (destinationFileName == string.Empty)
+            if (destinationFileName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(destinationFileName));
             }
@@ -352,12 +352,12 @@ namespace System.IO.IsolatedStorage
             if (destinationFileName == null)
                 throw new ArgumentNullException(nameof(destinationFileName));
 
-            if (sourceFileName == string.Empty)
+            if (sourceFileName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(sourceFileName));
             }
 
-            if (destinationFileName == string.Empty)
+            if (destinationFileName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(destinationFileName));
             }
@@ -393,12 +393,12 @@ namespace System.IO.IsolatedStorage
             if (destinationFileName == null)
                 throw new ArgumentNullException(nameof(destinationFileName));
 
-            if (sourceFileName == string.Empty)
+            if (sourceFileName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(sourceFileName));
             }
 
-            if (destinationFileName == string.Empty)
+            if (destinationFileName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(destinationFileName));
             }
@@ -434,12 +434,12 @@ namespace System.IO.IsolatedStorage
             if (destinationDirectoryName == null)
                 throw new ArgumentNullException(nameof(destinationDirectoryName));
 
-            if (sourceDirectoryName == string.Empty)
+            if (sourceDirectoryName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(sourceDirectoryName));
             }
 
-            if (destinationDirectoryName == string.Empty)
+            if (destinationDirectoryName.Length == 0)
             {
                 throw new ArgumentException(SR.Argument_EmptyPath, nameof(destinationDirectoryName));
             }
index 09413c8..db8cdf8 100644 (file)
@@ -161,7 +161,7 @@ namespace System.IO.Packaging
         internal static void ThrowIfInvalidRelationshipType(string relationshipType)
         {
             // Look for empty string or string with just spaces
-            if (relationshipType.Trim() == string.Empty)
+            if (string.IsNullOrWhiteSpace(relationshipType))
                 throw new ArgumentException(SR.InvalidRelationshipType);
         }
 
index c2c79ee..c43ca1a 100644 (file)
@@ -79,7 +79,7 @@ namespace System.IO.Packaging
 
             if (fragment != null)
             {
-                if (fragment == string.Empty || fragment[0] != '#')
+                if (fragment.Length == 0 || fragment[0] != '#')
                     throw new ArgumentException(SR.Format(SR.FragmentMustStartWithHash, nameof(fragment)));
             }
 
@@ -323,7 +323,7 @@ namespace System.IO.Packaging
 
             string partName = GetStringForPartUriFromAnyUri(packUri);
 
-            if (partName == string.Empty)
+            if (partName.Length == 0)
                 return null;
             else
                 return ValidatePartUri(new Uri(partName, UriKind.Relative));
index 2b50ad0..404e5e6 100644 (file)
@@ -49,7 +49,7 @@ namespace System.IO.Packaging
 
             string partName = GetStringForPartUriFromAnyUri(resolvedUri);
 
-            if (partName == string.Empty)
+            if (partName.Length == 0)
                 throw new ArgumentException(SR.PartUriIsEmpty);
 
             ThrowIfPartNameEndsWithSlash(partName);
@@ -399,7 +399,7 @@ namespace System.IO.Packaging
 
             //We need to make sure that the URI passed to us is not just "/"
             //"/" is a valid relative uri, but is not a valid partname
-            if (partName == string.Empty)
+            if (partName.Length == 0)
                 return new ArgumentException(SR.PartUriIsEmpty);
 
             if (partName[0] != ForwardSlashChar)
@@ -804,7 +804,7 @@ namespace System.IO.Packaging
                 // String.Split, will always return an empty string as the
                 // first member in the array as the string starts with a "/"
 
-                Debug.Assert(segments.Length > 0 && segments[0] == string.Empty);
+                Debug.Assert(segments.Length > 0 && segments[0].Length == 0);
 
                 //If the extension was not equal to .rels, we would have exited early.
                 Debug.Assert(string.CompareOrdinal((Path.GetExtension(segments[segments.Length - 1])), RelationshipPartUpperCaseExtension) == 0);
index f044bf7..f44daf6 100644 (file)
@@ -964,7 +964,7 @@ namespace System.IO.Packaging
                 ThrowIfXmlAttributeMissing(attributeName, attributeValue, tagName, reader);
 
                 //Checking for empty attribute
-                if (attributeValue == string.Empty)
+                if (attributeValue.Length == 0)
                     throw new XmlException(SR.Format(SR.RequiredAttributeEmpty, tagName, attributeName), null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
             }
 
index 44e48f7..91ffd95 100644 (file)
@@ -75,7 +75,7 @@ namespace System.Net.Mail
                 throw new ArgumentNullException(nameof(fileName));
             }
 
-            if (fileName == string.Empty)
+            if (fileName.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(fileName)), nameof(fileName));
             }
@@ -91,7 +91,7 @@ namespace System.Net.Mail
                 throw new ArgumentNullException(nameof(fileName));
             }
 
-            if (fileName == string.Empty)
+            if (fileName.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(fileName)), nameof(fileName));
             }
@@ -154,7 +154,7 @@ namespace System.Net.Mail
                 _part.Stream.Close();
             }
 
-            if (mediaType == null || mediaType == string.Empty)
+            if (string.IsNullOrEmpty(mediaType))
             {
                 mediaType = MediaTypeNames.Text.Plain;
             }
@@ -340,7 +340,7 @@ namespace System.Net.Mail
         public Attachment(string fileName, ContentType contentType) :
             base(fileName, contentType)
         {
-            if (contentType.Name == null || contentType.Name == string.Empty)
+            if (string.IsNullOrEmpty(contentType.Name))
             {
                 Name = Path.GetFileName(fileName);
             }
index 9519b3f..76d97dd 100644 (file)
@@ -124,7 +124,7 @@ namespace System.Net.Mail
             {
                 throw new ArgumentNullException(nameof(address));
             }
-            if (address == string.Empty)
+            if (address.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(address)), nameof(address));
             }
index 5b1b687..bc63f11 100644 (file)
@@ -23,7 +23,7 @@ namespace System.Net.Mail
             {
                 throw new ArgumentNullException(nameof(addresses));
             }
-            if (addresses == string.Empty)
+            if (addresses.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(addresses)), nameof(addresses));
             }
index 83eddcd..4bb5e9e 100644 (file)
@@ -44,10 +44,10 @@ namespace System.Net.Mail
             if (to == null)
                 throw new ArgumentNullException(nameof(to));
 
-            if (from == string.Empty)
+            if (from.Length == 0)
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(from)), nameof(from));
 
-            if (to == string.Empty)
+            if (to.Length == 0)
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(to)), nameof(to));
 
             _message = new Message(from, to);
index 6c912e3..ee4618c 100644 (file)
@@ -51,10 +51,10 @@ namespace System.Net.Mail
             if (to == null)
                 throw new ArgumentNullException(nameof(to));
 
-            if (from == string.Empty)
+            if (from.Length == 0)
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(from)), nameof(from));
 
-            if (to == string.Empty)
+            if (to.Length == 0)
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(to)), nameof(to));
 
             _from = new MailAddress(from);
index 834b5ba..0def572 100644 (file)
@@ -187,7 +187,7 @@ namespace System.Net.Mail
                     throw new ArgumentNullException(nameof(value));
                 }
 
-                if (value == string.Empty)
+                if (value.Length == 0)
                 {
                     throw new ArgumentException(SR.net_emptystringset, nameof(value));
                 }
index be078ae..891d17e 100644 (file)
@@ -83,7 +83,7 @@ namespace System.Net.Mime
                 {
                     throw new ArgumentNullException(nameof(value));
                 }
-                if (value == string.Empty)
+                if (value.Length == 0)
                 {
                     throw new ArgumentException(SR.net_emptystringset, nameof(value));
                 }
index d3a183f..7394cfd 100644 (file)
@@ -50,7 +50,7 @@ namespace System.Net.Mime
             {
                 throw new ArgumentNullException(nameof(contentType));
             }
-            if (contentType == string.Empty)
+            if (contentType.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(contentType)), nameof(contentType));
             }
@@ -65,7 +65,7 @@ namespace System.Net.Mime
             get { return Parameters["boundary"]; }
             set
             {
-                if (value == null || value == string.Empty)
+                if (string.IsNullOrEmpty(value))
                 {
                     Parameters.Remove("boundary");
                 }
@@ -81,7 +81,7 @@ namespace System.Net.Mime
             get { return Parameters["charset"]; }
             set
             {
-                if (value == null || value == string.Empty)
+                if (string.IsNullOrEmpty(value))
                 {
                     Parameters.Remove("charset");
                 }
@@ -105,7 +105,7 @@ namespace System.Net.Mime
                     throw new ArgumentNullException(nameof(value));
                 }
 
-                if (value == string.Empty)
+                if (value.Length == 0)
                 {
                     throw new ArgumentException(SR.net_emptystringset, nameof(value));
                 }
index 1162b01..1b468f7 100644 (file)
@@ -31,7 +31,7 @@ namespace System.Net.Mime
                 throw new ArgumentNullException(nameof(name));
             }
 
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(name)), nameof(name));
             }
@@ -60,7 +60,7 @@ namespace System.Net.Mime
                 throw new ArgumentNullException(nameof(name));
             }
 
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(name)), nameof(name));
             }
@@ -87,7 +87,7 @@ namespace System.Net.Mime
                 throw new ArgumentNullException(nameof(name));
             }
 
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(name)), nameof(name));
             }
@@ -138,12 +138,12 @@ namespace System.Net.Mime
                 throw new ArgumentNullException(nameof(value));
             }
 
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(name)), nameof(name));
             }
 
-            if (value == string.Empty)
+            if (value.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(value)), nameof(value));
             }
@@ -187,11 +187,11 @@ namespace System.Net.Mime
             {
                 throw new ArgumentNullException(nameof(value));
             }
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(name)), nameof(name));
             }
-            if (value == string.Empty)
+            if (value.Length == 0)
             {
                 throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(value)), nameof(value));
             }
index f36b484..3cd68e4 100644 (file)
@@ -595,7 +595,7 @@ namespace System.Net
 
             if (request.MethodInfo.Operation == FtpOperation.Rename)
             {
-                string baseDir = (requestDirectory == string.Empty)
+                string baseDir = (requestDirectory.Length == 0)
                     ? string.Empty : requestDirectory + "/";
                 commandList.Add(new PipelineEntry(FormatFtpCommand("RNFR", baseDir + requestFilename), flags));
 
index 09585f8..3ead4ce 100644 (file)
@@ -54,7 +54,7 @@ namespace System.Reflection
 
             if (cultureName != null)
             {
-                if (cultureName == string.Empty)
+                if (cultureName.Length == 0)
                     cultureName = "neutral";
                 sb.Append(", Culture=");
                 sb.AppendQuoted(cultureName);
index 6f09bd3..2f0194b 100644 (file)
@@ -245,7 +245,7 @@ namespace System.Runtime.Serialization.Json
             {
                 return JsonGlobals.xmlPrefix;
             }
-            if (ns == string.Empty)
+            if (ns.Length == 0)
             {
                 return string.Empty;
             }
index e829011..9d73bfa 100644 (file)
@@ -919,7 +919,7 @@ namespace System.Xml
             {
                 return _nameTable.Add("xml");
             }
-            if (namespaceName == string.Empty)
+            if (namespaceName.Length == 0)
             {
                 return string.Empty;
             }
index c071cda..e9d9f7e 100644 (file)
@@ -958,7 +958,7 @@ namespace System.Xml.Schema
                 {
                     return schema;
                 }
-                else if (tns == string.Empty)
+                else if (tns.Length == 0)
                 { //There could be a chameleon for same ns
                     // It is OK to pass in the schema we have found so far, since it must have the schemaUri we're looking for
                     // (we found it that way above) and it must be the original chameleon schema (the one without target ns)
index 3dcbf43..ffb420c 100644 (file)
@@ -344,7 +344,7 @@ namespace System.Runtime.Caching
             {
                 throw new ArgumentNullException(nameof(name));
             }
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Empty_string_invalid, nameof(name));
             }
@@ -364,7 +364,7 @@ namespace System.Runtime.Caching
             {
                 throw new ArgumentNullException(nameof(name));
             }
-            if (name == string.Empty)
+            if (name.Length == 0)
             {
                 throw new ArgumentException(SR.Empty_string_invalid, nameof(name));
             }
index 0c9f12a..5f6e79e 100644 (file)
@@ -154,7 +154,7 @@ namespace System.Reflection.Emit
         {
             if (name == null)
                 throw new ArgumentNullException(nameof(name));
-            if (name == string.Empty)
+            if (name.Length == 0)
                 throw new ArgumentException("name cannot be empty", nameof(name));
             if (global_type_created != null)
                 throw new InvalidOperationException("global fields already created");
index 5d110fe..d4efac9 100644 (file)
@@ -197,7 +197,7 @@ namespace System.Reflection
         {
             if (className == null)
                 throw new ArgumentNullException(nameof(className));
-            if (className == string.Empty)
+            if (className.Length == 0)
                 throw new ArgumentException("Type name can't be empty");
             return assembly.InternalGetType(this, className, throwOnError, ignoreCase);
         }
index 7df75e0..90a36a0 100644 (file)
@@ -289,7 +289,7 @@ namespace System
             if (typeName == null)
                 throw new ArgumentNullException(nameof(typeName));
 
-            if (typeName == string.Empty)
+            if (typeName.Length == 0)
                 if (throwOnError)
                     throw new TypeLoadException("A null or zero length string does not represent a valid Type.");
                 else