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

UPath performance improvements #77

Merged
merged 4 commits into from
Oct 27, 2023
Merged

UPath performance improvements #77

merged 4 commits into from
Oct 27, 2023

Conversation

bollhals
Copy link
Contributor

@bollhals bollhals commented Oct 6, 2023

In my project, we're using your great library.

Lately we've been analyzing our performance, and your library came up a few times. This collection of fixes were mostly some low hanging fruits to improve things here and there a bit. I'll comment on them for better understanding.

There are a few other places that I can improve on, but that's for later if wanted.

src/Zio.Tests/TestSearchPattern.cs Outdated Show resolved Hide resolved
@@ -67,6 +70,16 @@ public void TestEquals()
Assert.False(pathInfo.Equals(null));
}

[Fact]
public void TestIsNullAndEmpty()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Empty did not have any tests. Moved IsNull to here from the test below

@@ -401,7 +401,7 @@ public static string[] ReadAllLines(this IFileSystem fs, UPath path)
using (var reader = new StreamReader(stream))
{
var lines = new List<string>();
string line;
string? line;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NRT fix

@@ -601,8 +601,7 @@ public static IEnumerable<UPath> EnumerateDirectories(this IFileSystem fileSyste
public static IEnumerable<UPath> EnumerateDirectories(this IFileSystem fileSystem, UPath path, string searchPattern, SearchOption searchOption)
{
if (searchPattern is null) throw new ArgumentNullException(nameof(searchPattern));
foreach (var subPath in fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.Directory))
yield return subPath;
return fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.Directory);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

avoids enumerator allocation

Throw(this.GetType());
}

static void Throw(Type type)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving the trowing of the exception out of the method, allows the method to be inlined.

{
dotCount++;
}
else if (c == DirectorySeparator || c == '\\')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added elseif, as it can only be either a dot or a separator

lastIndex = i;
if (dotCount > 0 && // has dots
dotCount == part.Length && // only dots
(dotCount != 2 || parts.Count > 1)) // Skip ".." if it's the first part
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made that logic a bit more complex, to only really set processParts, when really needed, so more paths are using the fast path only.

@@ -67,7 +67,7 @@ public static UPath GetDirectory(this UPath path)
var lastIndex = fullname.LastIndexOf(UPath.DirectorySeparator);
if (lastIndex > 0)
{
return fullname.Substring(0, lastIndex);
return new UPath(fullname.Substring(0, lastIndex), true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Substring should be ok to use no validation right?

if (path.FullName is null)
throw new ArgumentNullException(name);
return path;
if (path.IsNull)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FullName is null -> IsNull and the local method to move out the exception to help inline it.

/// <exception cref="System.ArgumentException">If the path is not absolute using the parameter name from <paramref name="name"/></exception>
public static UPath AssertAbsolute(this UPath path, string name = "path")
public static void AssertAbsolute(this UPath path, string name = "path")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the return, as it created a new UPath where only one occurrance actually used it. (New Upath went through the normalization method, which is rather expensive when unused.

@xoofx
Copy link
Owner

xoofx commented Oct 14, 2023

Thanks, I'm ok with the changes.
Looks like some tests are failing, if you could have a look.

@bollhals
Copy link
Contributor Author

I removed the explicit null assert, as if it is null, it can never be absolute. This is the cause of the failing tests, let me know if I should adjust the tests to ArgumentException instead of ArgumentNullException, or whether to put this back.

From here: #77 (comment)

Can you let me know what you prefer?

@bollhals
Copy link
Contributor Author

Reminder: Which way shall I adjust it to fix the tests?

@xoofx
Copy link
Owner

xoofx commented Oct 19, 2023

I'm on a business trip and pretty busy, will come back hopefully next week, my apologies.

src/Zio/UPathExtensions.cs Outdated Show resolved Hide resolved
@xoofx
Copy link
Owner

xoofx commented Oct 25, 2023

The CI is failing because dotnet-releaser is requiring net7.0, so I bumped it on main, you can merge main back.

@xoofx
Copy link
Owner

xoofx commented Oct 25, 2023

I had to make further change to main (drop some old targets, but that's ok)

@bollhals
Copy link
Contributor Author

rebased and solved the failing tests, should be ready.

@coveralls
Copy link

Coverage Status

coverage: 90.435% (+0.001%) from 90.434% when pulling 76aae5c on bollhals:perf_1 into 99c16ec on xoofx:main.

@xoofx xoofx merged commit c2c62bd into xoofx:main Oct 27, 2023
1 check passed
@xoofx
Copy link
Owner

xoofx commented Oct 27, 2023

Thanks for the contribution, my apologies that it took some time and ping/pong to review it. 🙂

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

Successfully merging this pull request may close these issues.

3 participants