Skip to content

Commit

Permalink
UPath performance improvements (#77)
Browse files Browse the repository at this point in the history
* UPath performance improvements

* revert public API changes

* missed the documentation tags

* restore exception behaviour

---------

Co-authored-by: Sandro Bollhalder <[email protected]>
  • Loading branch information
bollhals and Sandro Bollhalder authored Oct 27, 2023
1 parent 99c16ec commit c2c62bd
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 168 deletions.
16 changes: 13 additions & 3 deletions src/Zio.Tests/TestUPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class TestUPath
[InlineData("...a/b../", "...a/b..")]
[InlineData("...a/..", "")]
[InlineData("...a/b/..", "...a")]
[InlineData("..a/b", "..a/b")]
[InlineData("c/..d", "c/..d")]
[InlineData("c/d..", "c/d..")]
public void TestNormalize(string pathAsText, string expectedResult)
{
var path = new UPath(pathAsText);
Expand All @@ -67,6 +70,16 @@ public void TestEquals()
Assert.False(pathInfo.Equals(null));
}

[Fact]
public void TestIsNullAndEmpty()
{
Assert.True(default(UPath).IsNull);
Assert.False(default(UPath).IsEmpty);
Assert.True(new UPath("").IsEmpty);
Assert.False(new UPath("").IsNull);
Assert.False(new UPath("/").IsEmpty);
}

[Fact]
public void TestAbsoluteAndRelative()
{
Expand All @@ -83,9 +96,6 @@ public void TestAbsoluteAndRelative()
Assert.True(path.IsAbsolute);

Assert.Equal(path, path.ToAbsolute());

path = new UPath();
Assert.True(path.IsNull);
}

[Theory]
Expand Down
10 changes: 4 additions & 6 deletions src/Zio/FileSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
Expand Down Expand Up @@ -430,7 +430,7 @@ public static string[] ReadAllLines(this IFileSystem fs, UPath path, Encoding en
using (var reader = new StreamReader(stream, encoding))
{
var lines = new List<string>();
string line;
string? line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -644,8 +643,7 @@ public static IEnumerable<UPath> EnumerateFiles(this IFileSystem fileSystem, UPa
public static IEnumerable<UPath> EnumerateFiles(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.File))
yield return subPath;
return fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.File);
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Zio/FileSystems/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,12 @@ private void AssertNotDisposed()
{
if (IsDisposing || IsDisposed)
{
throw new ObjectDisposedException($"This instance `{GetType()}` is already disposed.");
Throw(this.GetType());
}

static void Throw(Type type)
{
throw new ObjectDisposedException($"This instance `{type}` is already disposed.");
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/Zio/FileSystems/PhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,7 @@ protected override string ConvertPathToInternalImpl(UPath path)
if (absolutePath.Length > DrivePrefixOnWindows.Length + 1)
builder.Append(absolutePath.Replace(UPath.DirectorySeparator, '\\').Substring(DrivePrefixOnWindows.Length + 2));

var result = builder.ToString();
builder.Length = 0;
return result;
return builder.ToString();
}
return absolutePath;
}
Expand All @@ -841,9 +839,7 @@ protected override UPath ConvertPathFromInternalImpl(string innerPath)
if (absolutePath.Length > 2)
builder.Append(absolutePath.Substring(2));

var result = builder.ToString();
builder.Length = 0;
return new UPath(result);
return new UPath(builder.ToString());
}
return innerPath;
}
Expand Down
Loading

0 comments on commit c2c62bd

Please sign in to comment.