Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking strings against length seem to have better perf #36443

Merged
merged 15 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -329,7 +329,7 @@ private void FireChangeToken()
[NonEvent]
private static LoggerFilterRule[] ParseFilterSpec(string filterSpec, LogLevel defaultLevel)
{
if (filterSpec == string.Empty)
if (filterSpec.Length == 0)
{
return new[] { new LoggerFilterRule(typeof(EventSourceLoggerProvider).FullName, null, defaultLevel, null) };
}
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 (relationshipType.Trim().Length == 0)
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -98,7 +98,7 @@ public string? From
set
{
// Null and empty string are equivalent. In this case it means, remove the From header value (if any).
if (value == string.Empty)
if (value?.Length == 0)
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
{
value = null;
}
Expand All @@ -117,7 +117,7 @@ public string? Host
set
{
// Null and empty string are equivalent. In this case it means, remove the Host header value (if any).
if (value == string.Empty)
if (value?.Length == 0)
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
{
value = null;
}
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
Loading