Skip to content

Commit

Permalink
Checking strings against length seem to have better perf (#36443)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored May 19, 2020
1 parent 8c6c765 commit 1c048bd
Show file tree
Hide file tree
Showing 28 changed files with 57 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ internal static bool TryFindCGroupPathForSubsystem(CGroupVersion cgroupVersion,
// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@ private void InitializeImpl()
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ private static void CreateIniFile(string categoryName, string categoryHelp, Coun
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private unsafe bool StartWithCreateProcess(ProcessStartInfo startInfo)
environmentBlock = GetEnvironmentVariablesBlock(startInfo._environmentVariables!);
}
string workingDirectory = startInfo.WorkingDirectory;
if (workingDirectory == string.Empty)
if (workingDirectory.Length == 0)
workingDirectory = Directory.GetCurrentDirectory();

bool retVal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal static string AppendSuffix(string filePath)
/// </summary>
public static string GetFileName(string originalPath)
{
if (Suffix == string.Empty)
if (string.IsNullOrEmpty(Suffix))
return originalPath;

string newPath = AppendSuffix(originalPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public DateTimeOffset GetCreationTime(string path)
if (path == null)
throw new ArgumentNullException(nameof(path));

if (path == string.Empty)
if (path.Length == 0)
{
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
}
Expand All @@ -284,7 +284,7 @@ public DateTimeOffset GetLastAccessTime(string path)
if (path == null)
throw new ArgumentNullException(nameof(path));

if (path == string.Empty)
if (path.Length == 0)
{
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
}
Expand All @@ -306,7 +306,7 @@ public DateTimeOffset GetLastWriteTime(string path)
if (path == null)
throw new ArgumentNullException(nameof(path));

if (path == string.Empty)
if (path.Length == 0)
{
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
}
Expand All @@ -331,12 +331,12 @@ public void CopyFile(string sourceFileName, string destinationFileName)
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));
}
Expand All @@ -352,12 +352,12 @@ public void CopyFile(string sourceFileName, string destinationFileName, bool ove
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));
}
Expand Down Expand Up @@ -393,12 +393,12 @@ public void MoveFile(string sourceFileName, string destinationFileName)
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));
}
Expand Down Expand Up @@ -434,12 +434,12 @@ public void MoveDirectory(string sourceDirectoryName, string destinationDirector
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal void Flush()
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static Uri Create(Uri packageUri, Uri partUri, string fragment)

if (fragment != null)
{
if (fragment == string.Empty || fragment[0] != '#')
if (fragment.Length == 0 || fragment[0] != '#')
throw new ArgumentException(SR.Format(SR.FragmentMustStartWithHash, nameof(fragment)));
}

Expand Down Expand Up @@ -323,7 +323,7 @@ private static PackUriHelper.ValidatedPartUri GetPartUriComponent(Uri packUri)

string partName = GetStringForPartUriFromAnyUri(packUri);

if (partName == string.Empty)
if (partName.Length == 0)
return null;
else
return ValidatePartUri(new Uri(partName, UriKind.Relative));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Uri CreatePartUri(Uri partUri)

string partName = GetStringForPartUriFromAnyUri(resolvedUri);

if (partName == string.Empty)
if (partName.Length == 0)
throw new ArgumentException(SR.PartUriIsEmpty);

ThrowIfPartNameEndsWithSlash(partName);
Expand Down Expand Up @@ -399,7 +399,7 @@ private static Exception GetExceptionIfPartUriInvalid(Uri partUri, out string pa

//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)
Expand Down Expand Up @@ -804,7 +804,7 @@ private bool IsRelationshipUri()
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ private void ValidateXmlAttribute(string attributeName, string attributeValue, s
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal void SetContentFromFile(string fileName, ContentType? contentType)
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));
}
Expand All @@ -91,7 +91,7 @@ internal void SetContentFromFile(string fileName, string? mediaType)
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));
}
Expand Down Expand Up @@ -154,7 +154,7 @@ internal void SetContentFromString(string content, Encoding? encoding, string? m
_part.Stream.Close();
}

if (mediaType == null || mediaType == string.Empty)
if (string.IsNullOrEmpty(mediaType))
{
mediaType = MediaTypeNames.Text.Plain;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ public Attachment(string fileName, string? mediaType) :
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static bool TryParse(string address, string? displayName, Encoding? disp
{
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Add(string addresses)
{
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public MailMessage(string from, string to)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ internal Message(string from, string to) : this()
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public string? Host
throw new ArgumentNullException(nameof(value));
}

if (value == string.Empty)
if (value.Length == 0)
{
throw new ArgumentException(SR.net_emptystringset, nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public string DispositionType
{
throw new ArgumentNullException(nameof(value));
}
if (value == string.Empty)
if (value.Length == 0)
{
throw new ArgumentException(SR.net_emptystringset, nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ContentType(string contentType)
{
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));
}
Expand All @@ -65,7 +65,7 @@ public string? Boundary
get { return Parameters["boundary"]; }
set
{
if (value == null || value == string.Empty)
if (string.IsNullOrEmpty(value))
{
Parameters.Remove("boundary");
}
Expand All @@ -81,7 +81,7 @@ public string? CharSet
get { return Parameters["charset"]; }
set
{
if (value == null || value == string.Empty)
if (string.IsNullOrEmpty(value))
{
Parameters.Remove("charset");
}
Expand All @@ -105,7 +105,7 @@ public string MediaType
throw new ArgumentNullException(nameof(value));
}

if (value == string.Empty)
if (value.Length == 0)
{
throw new ArgumentException(SR.net_emptystringset, nameof(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void Remove(string name)
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));
}
Expand Down Expand Up @@ -60,7 +60,7 @@ public override void Remove(string name)
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));
}
Expand All @@ -87,7 +87,7 @@ public override void Remove(string name)
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));
}
Expand Down Expand Up @@ -138,12 +138,12 @@ public override void Set(string name, string value)
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));
}
Expand Down Expand Up @@ -187,11 +187,11 @@ public override void Add(string name, string value)
{
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ protected override PipelineEntry[] BuildCommandsList(WebRequest req)

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));

Expand Down
Loading

0 comments on commit 1c048bd

Please sign in to comment.