Skip to content

Commit c2c62bd

Browse files
bollhalsSandro Bollhalder
and
Sandro Bollhalder
authored
UPath performance improvements (#77)
* UPath performance improvements * revert public API changes * missed the documentation tags * restore exception behaviour --------- Co-authored-by: Sandro Bollhalder <[email protected]>
1 parent 99c16ec commit c2c62bd

File tree

6 files changed

+161
-168
lines changed

6 files changed

+161
-168
lines changed

src/Zio.Tests/TestUPath.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class TestUPath
4141
[InlineData("...a/b../", "...a/b..")]
4242
[InlineData("...a/..", "")]
4343
[InlineData("...a/b/..", "...a")]
44+
[InlineData("..a/b", "..a/b")]
45+
[InlineData("c/..d", "c/..d")]
46+
[InlineData("c/d..", "c/d..")]
4447
public void TestNormalize(string pathAsText, string expectedResult)
4548
{
4649
var path = new UPath(pathAsText);
@@ -67,6 +70,16 @@ public void TestEquals()
6770
Assert.False(pathInfo.Equals(null));
6871
}
6972

73+
[Fact]
74+
public void TestIsNullAndEmpty()
75+
{
76+
Assert.True(default(UPath).IsNull);
77+
Assert.False(default(UPath).IsEmpty);
78+
Assert.True(new UPath("").IsEmpty);
79+
Assert.False(new UPath("").IsNull);
80+
Assert.False(new UPath("/").IsEmpty);
81+
}
82+
7083
[Fact]
7184
public void TestAbsoluteAndRelative()
7285
{
@@ -83,9 +96,6 @@ public void TestAbsoluteAndRelative()
8396
Assert.True(path.IsAbsolute);
8497

8598
Assert.Equal(path, path.ToAbsolute());
86-
87-
path = new UPath();
88-
Assert.True(path.IsNull);
8999
}
90100

91101
[Theory]

src/Zio/FileSystemExtensions.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public static string[] ReadAllLines(this IFileSystem fs, UPath path)
401401
using (var reader = new StreamReader(stream))
402402
{
403403
var lines = new List<string>();
404-
string line;
404+
string? line;
405405
while ((line = reader.ReadLine()) != null)
406406
{
407407
lines.Add(line);
@@ -430,7 +430,7 @@ public static string[] ReadAllLines(this IFileSystem fs, UPath path, Encoding en
430430
using (var reader = new StreamReader(stream, encoding))
431431
{
432432
var lines = new List<string>();
433-
string line;
433+
string? line;
434434
while ((line = reader.ReadLine()) != null)
435435
{
436436
lines.Add(line);
@@ -601,8 +601,7 @@ public static IEnumerable<UPath> EnumerateDirectories(this IFileSystem fileSyste
601601
public static IEnumerable<UPath> EnumerateDirectories(this IFileSystem fileSystem, UPath path, string searchPattern, SearchOption searchOption)
602602
{
603603
if (searchPattern is null) throw new ArgumentNullException(nameof(searchPattern));
604-
foreach (var subPath in fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.Directory))
605-
yield return subPath;
604+
return fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.Directory);
606605
}
607606

608607
/// <summary>
@@ -644,8 +643,7 @@ public static IEnumerable<UPath> EnumerateFiles(this IFileSystem fileSystem, UPa
644643
public static IEnumerable<UPath> EnumerateFiles(this IFileSystem fileSystem, UPath path, string searchPattern, SearchOption searchOption)
645644
{
646645
if (searchPattern is null) throw new ArgumentNullException(nameof(searchPattern));
647-
foreach (var subPath in fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.File))
648-
yield return subPath;
646+
return fileSystem.EnumeratePaths(path, searchPattern, searchOption, SearchTarget.File);
649647
}
650648

651649
/// <summary>

src/Zio/FileSystems/FileSystem.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,12 @@ private void AssertNotDisposed()
612612
{
613613
if (IsDisposing || IsDisposed)
614614
{
615-
throw new ObjectDisposedException($"This instance `{GetType()}` is already disposed.");
615+
Throw(this.GetType());
616+
}
617+
618+
static void Throw(Type type)
619+
{
620+
throw new ObjectDisposedException($"This instance `{type}` is already disposed.");
616621
}
617622
}
618623

src/Zio/FileSystems/PhysicalFileSystem.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,7 @@ protected override string ConvertPathToInternalImpl(UPath path)
815815
if (absolutePath.Length > DrivePrefixOnWindows.Length + 1)
816816
builder.Append(absolutePath.Replace(UPath.DirectorySeparator, '\\').Substring(DrivePrefixOnWindows.Length + 2));
817817

818-
var result = builder.ToString();
819-
builder.Length = 0;
820-
return result;
818+
return builder.ToString();
821819
}
822820
return absolutePath;
823821
}
@@ -841,9 +839,7 @@ protected override UPath ConvertPathFromInternalImpl(string innerPath)
841839
if (absolutePath.Length > 2)
842840
builder.Append(absolutePath.Substring(2));
843841

844-
var result = builder.ToString();
845-
builder.Length = 0;
846-
return new UPath(result);
842+
return new UPath(builder.ToString());
847843
}
848844
return innerPath;
849845
}

0 commit comments

Comments
 (0)