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

Avoid creation of temporary strings where possible #11380

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Erarndt
Copy link

@Erarndt Erarndt commented Feb 3, 2025

Fixes #

Context

Changes Made

Testing

Notes

string extension = Path.GetExtension(path);

if (string.Equals(extension, ".dll", StringComparison.OrdinalIgnoreCase))
if (!string.IsNullOrEmpty(path) && path.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path.HasExtension?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means "does this path have an extension", not "does this path have the specified extension", sadly.

string extension = Path.GetExtension(path);

if (string.Equals(extension, ".dll", StringComparison.OrdinalIgnoreCase))
if (!string.IsNullOrEmpty(path) && path.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means "does this path have an extension", not "does this path have the specified extension", sadly.

@@ -299,7 +299,11 @@ public bool IsTraversal
{
if (!_isTraversalProject.HasValue)
{
if (String.Equals(Path.GetFileName(ProjectFullPath), "dirs.proj", StringComparison.OrdinalIgnoreCase))
#if NET471_OR_GREATER
if (MemoryExtensions.Equals(Microsoft.IO.Path.GetFileName(ProjectFullPath.AsSpan()), "dirs.proj".AsSpan(), StringComparison.OrdinalIgnoreCase))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you doing this explicitly here to be conservative/localized (totally legit!) or because there's a reason not to shift to Microsoft.IO.Redist elsewhere in this file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be subtle differences in behavior between NetFX System.IO.Path and Microsoft.IO.Path, so this was meant to be a surgical update to minimize churn. In other repositories, we've seen measurable improvement fully switching over to Microsoft.IO, so this should be considered as well.

One thing to note: Since Microsoft.IO is a snapshot of improvements backported for NetFX, we want to use System.IO in .NET 5+. That way we'll continue to benefit from future improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants