Skip to content

Commit

Permalink
restore: trying out mmap-based file extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jul 17, 2020
1 parent f359ca0 commit e1c1e9c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace NuGet.Packaging.Core
/// <param name="sourceFile">The path of the file in the package.</param>
/// <param name="targetPath">The path to write to.</param>
/// <param name="fileStream">The file <see cref="Stream"/>.</param>
/// <param name="size">The size of the incoming file.</param>
/// <returns>The file name if the file was written; otherwise <c>null</c>.</returns>
public delegate string ExtractPackageFileDelegate(string sourceFile, string targetPath, Stream fileStream);
public delegate string ExtractPackageFileDelegate(string sourceFile, string targetPath, Stream fileStream, long? size);
}
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Packaging/PackageArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public override IEnumerable<string> CopyFiles(

using (var stream = entry.Open())
{
var copiedFile = extractFile(packageFileName, targetFilePath, stream);
var copiedFile = extractFile(packageFileName, targetFilePath, stream, entry.Length);
if (copiedFile != null)
{
entry.UpdateFileTimeFromEntry(copiedFile, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.IO.MemoryMappedFiles;

namespace NuGet.Packaging
{
public static class StreamExtensions
{
public static string CopyToFile(this Stream inputStream, string fileFullPath)

/**
Only files smaller than this value will be mmap'ed
*/
private const long MAX_MMAP_SIZE = 10 * 1024 * 1024;
public static string CopyToFile(this Stream inputStream, string fileFullPath) => CopyToFile(inputStream, fileFullPath, null);
public static string CopyToFile(this Stream inputStream, string fileFullPath, long? size)
{
if (Path.GetFileName(fileFullPath).Length == 0)
{
Expand All @@ -27,12 +34,24 @@ public static string CopyToFile(this Stream inputStream, string fileFullPath)
return fileFullPath;
}

// For files of a certain size, we can do some Cleverness and mmap them instead
// of writing directly to disk. This can increase performance by a LOT, at the
// cost of files taking longer to get to disk -- but that's fine!
if (size > 0 && size <= MAX_MMAP_SIZE)
{
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileFullPath, FileMode.Create, null, (long)size))
{
MemoryMappedViewStream mmstream = mmf.CreateViewStream();
inputStream.CopyTo(mmstream);
return fileFullPath;
}
}

using (var outputStream = NuGetExtractionFileIO.CreateFile(fileFullPath))
{
inputStream.CopyTo(outputStream);
return fileFullPath;
}

return fileFullPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static string SaveAsFile(this ZipArchiveEntry entry, string fileFullPath,
{
using (var inputStream = entry.Open())
{
inputStream.CopyToFile(fileFullPath);
inputStream.CopyToFile(fileFullPath, entry.Length);
}

entry.UpdateFileTimeFromEntry(fileFullPath, logger);
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Packaging/PackageFileExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static string GetBinaryForLanguageSpecificXml(string file)
return null;
}

public string ExtractPackageFile(string source, string target, Stream stream)
public string ExtractPackageFile(string source, string target, Stream stream, long? size)
{
if ((_xmlDocFileSaveMode == XmlDocFileSaveMode.Skip) && _intellisenseXmlFiles.Contains(source))
{
Expand Down Expand Up @@ -125,7 +125,7 @@ public string ExtractPackageFile(string source, string target, Stream stream)
}
else
{
stream.CopyToFile(target);
stream.CopyToFile(target, size);
}

return target;
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Packaging/PackageFolderReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public override IEnumerable<string> CopyFiles(

using (var fileStream = sourceFile.OpenRead())
{
targetPath = extractFile(sourceFile.FullName, targetPath, fileStream);
targetPath = extractFile(sourceFile.FullName, targetPath, fileStream, null);
if (targetPath != null)
{
File.SetLastWriteTimeUtc(targetPath, sourceFile.LastWriteTimeUtc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ public void CanVerifySignedPackages_ReturnsValueBasedOnOperatingSystemAndFramewo
}
}

private static string ExtractFile(string sourcePath, string targetPath, Stream sourceStream)
private static string ExtractFile(string sourcePath, string targetPath, Stream sourceStream, long? size)
{
using (var targetStream = File.OpenWrite(targetPath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ await Assert.ThrowsAsync<NotImplementedException>(
}
}

private static string ExtractFile(string sourcePath, string targetPath, Stream sourceStream)
private static string ExtractFile(string sourcePath, string targetPath, Stream sourceStream, long? size)
{
using (var targetStream = File.OpenWrite(targetPath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ public async Task CopyNupkgAsync_CopiesNupkgOnSuccess()
}
}

private string ExtractPackageFile(string sourceFile, string targetPath, Stream fileStream)
private string ExtractPackageFile(string sourceFile, string targetPath, Stream fileStream, long? size)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit e1c1e9c

Please sign in to comment.