Skip to content

Commit

Permalink
Eliminate a few allocations in ZipArchiveFileSystem and seal private …
Browse files Browse the repository at this point in the history
…classes
  • Loading branch information
iamcarbon committed Nov 9, 2023
1 parent 094643c commit 772c3ed
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/Zio/FileSystems/ZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ protected override bool FileExistsImpl(UPath path)
protected override FileAttributes GetAttributesImpl(UPath path)
{
var entry = GetEntry(path.FullName) ?? GetEntry(ConvertPathToDirectory(path));
if (entry == null)
if (entry is null)
{
throw FileSystemExceptionHelper.NewFileNotFoundException(path);
}
Expand Down Expand Up @@ -570,7 +570,7 @@ protected override void MoveDirectoryImpl(UPath srcPath, UPath destPath)
destDir = destDir.Replace('\\', DirectorySeparator);
#endif
_entriesLock.EnterReadLock();
var entries = new ZipArchiveEntry[0];
var entries = Array.Empty<ZipArchiveEntry>();
try
{
entries = _archive.Entries.Where(e => e.FullName.StartsWith(srcDir, _isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)).ToArray();
Expand Down Expand Up @@ -618,12 +618,8 @@ protected override void MoveDirectoryImpl(UPath srcPath, UPath destPath)
/// <inheritdoc />
protected override void MoveFileImpl(UPath srcPath, UPath destPath)
{
var srcEntry = GetEntry(srcPath.FullName);
if (srcEntry == null)
{
throw FileSystemExceptionHelper.NewFileNotFoundException(srcPath);
}

var srcEntry = GetEntry(srcPath.FullName) ?? throw FileSystemExceptionHelper.NewFileNotFoundException(srcPath);

if (!DirectoryExistsImpl(destPath.GetDirectory()))
{
throw FileSystemExceptionHelper.NewDirectoryNotFoundException(destPath.GetDirectory());
Expand All @@ -633,8 +629,7 @@ protected override void MoveFileImpl(UPath srcPath, UPath destPath)
if (destEntry != null)
{
throw new IOException("Cannot overwrite existing file.");
}

}

destEntry = CreateEntry(destPath.FullName);
TryGetDispatcher()?.RaiseCreated(destPath);
Expand Down Expand Up @@ -730,7 +725,7 @@ protected override Stream OpenFileImpl(UPath path, FileMode mode, FileAccess acc
protected override void ReplaceFileImpl(UPath srcPath, UPath destPath, UPath destBackupPath, bool ignoreMetadataErrors)
{
var sourceEntry = GetEntry(srcPath.FullName);
if (sourceEntry == null)
if (sourceEntry is null)
{
throw FileSystemExceptionHelper.NewFileNotFoundException(srcPath);
}
Expand Down Expand Up @@ -813,7 +808,7 @@ protected override void SetLastAccessTimeImpl(UPath path, DateTime time)
protected override void SetLastWriteTimeImpl(UPath path, DateTime time)
{
var entry = GetEntry(path.FullName) ?? GetEntry(ConvertPathToDirectory(path.FullName));
if (entry == null)
if (entry is null)
{
throw FileSystemExceptionHelper.NewFileNotFoundException(path);
}
Expand Down Expand Up @@ -879,17 +874,19 @@ private ZipArchiveEntry CreateEntry(string path)
}
}

private string GetName(ZipArchiveEntry entry)
private static readonly char[] s_slashChars = { '/', '\\' };

private static string GetName(ZipArchiveEntry entry)
{
var name = entry.FullName.TrimEnd('/', '\\');
var index = name.LastIndexOfAny(new[] { '/', '\\' });
var name = entry.FullName.TrimEnd(s_slashChars);
var index = name.LastIndexOfAny(s_slashChars);
return name.Substring(index + 1);
}

private static string GetParent(string path)
{
path = path.TrimEnd('/', '\\');
var lastIndex = path.LastIndexOfAny(new[] { '/', '\\' });
path = path.TrimEnd(s_slashChars);
var lastIndex = path.LastIndexOfAny(s_slashChars);
return lastIndex == -1 ? "" : path.Substring(0, lastIndex);
}

Expand Down Expand Up @@ -926,8 +923,7 @@ private static string RemoveLeadingSlash(string path)
}
}


private class ZipEntryStream : Stream
private sealed class ZipEntryStream : Stream
{
private readonly ZipArchiveEntry _entry;
private readonly ZipArchiveFileSystem _fileSystem;
Expand Down Expand Up @@ -1029,7 +1025,7 @@ public override void Close()
}
}

private class EntryState
private sealed class EntryState
{
public EntryState(FileShare share)
{
Expand Down

0 comments on commit 772c3ed

Please sign in to comment.