Skip to content

Commit 43a78bc

Browse files
committed
Disabled in memory caching of decompressed zip content in the VFS
1 parent 94612bf commit 43a78bc

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed

Wazzy.Tests/TestFolder.zip

317 Bytes
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Wazzy.WasiSnapshotPreview1.FileSystem;
2+
using Wazzy.WasiSnapshotPreview1.FileSystem.Implementations.VirtualFileSystem.Builder;
3+
4+
namespace Wazzy.Tests;
5+
6+
[TestClass]
7+
public class VirtualFileSystemTests
8+
{
9+
[TestMethod]
10+
public void ReadMappedZipEntry()
11+
{
12+
var vfs = (IWasiFileSystem)new VirtualFileSystemBuilder()
13+
.Readonly(true)
14+
.WithVirtualRoot(root =>
15+
{
16+
root.MapReadonlyZipArchiveDirectory("TestFolder", "TestFolder.zip");
17+
})
18+
.Build();
19+
}
20+
}

Wazzy.Tests/Wazzy.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
<None Update="Scripts\Simple_Async.wasm">
5757
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5858
</None>
59+
<None Update="TestFolder.zip">
60+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
61+
</None>
5962
<None Update="wasi-testsuite\tests\assemblyscript\testsuite\args_get-multiple-arguments.json">
6063
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6164
</None>

Wazzy/WasiSnapshotPreview1/FileSystem/Implementations/VirtualFileSystem/Builder/DirectoryBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,20 @@ public DirectoryBuilder MapDirectory(
159159
/// </remarks>
160160
/// <param name="name"></param>
161161
/// <param name="hostPath"></param>
162+
/// <param name="contentCaching">Set whether or not decompressed file content should be cached in memory after the first read</param>
162163
/// <returns></returns>
163164
public DirectoryBuilder MapReadonlyZipArchiveDirectory(
164165
string name,
165-
string hostPath)
166+
string hostPath,
167+
bool contentCaching = false)
166168
{
167169
if (!File.Exists(hostPath))
168170
throw new ArgumentException($"File {hostPath} does not exist", nameof(hostPath));
169171

170172
var path = _fullPath + '/' + ValidatePath(name);
171173

172174
_contentConstructors.Add(
173-
(path, context => new MappedZipArchiveDirectoryContent(hostPath, context.Clock))
175+
(path, context => new MappedZipArchiveDirectoryContent(hostPath, context.Clock, contentCaching))
174176
);
175177

176178
return this;

Wazzy/WasiSnapshotPreview1/FileSystem/Implementations/VirtualFileSystem/Directories/MappedZipArchiveDirectoryContent.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ public override IReadOnlyList<DirectoryItem> EnumerateChildren(ulong timestamp)
4747
public ulong ModificationTime { get; set; }
4848
public ulong ChangeTime { get; set; }
4949

50-
public MappedZipArchiveDirectoryContent(string archivePath, IVFSClock clock)
50+
private readonly bool _contentCaching;
51+
52+
public MappedZipArchiveDirectoryContent(string archivePath, IVFSClock clock, bool contentCaching)
5153
: this(File.OpenRead(archivePath), clock)
5254
{
55+
_contentCaching = contentCaching;
5356
}
5457

5558
public MappedZipArchiveDirectoryContent(Stream archiveStream, IVFSClock clock)
@@ -225,7 +228,7 @@ private MappedZipEntryFile GetOrCreateFileByEntry(string entry)
225228
{
226229
if (!_fileEntryCache.TryGetValue(entry, out var file))
227230
{
228-
file = new MappedZipEntryFile(this, entry);
231+
file = new MappedZipEntryFile(this, entry, _contentCaching);
229232
_fileEntryCache[entry] = file;
230233
}
231234

Wazzy/WasiSnapshotPreview1/FileSystem/Implementations/VirtualFileSystem/Files/MappedZipEntryFile.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ public override void Truncate(ulong timestamp, long size)
7474
}
7575
}
7676

77-
public MappedZipEntryFile(MappedZipArchiveDirectoryContent archive, string fullPath)
77+
public MappedZipEntryFile(MappedZipArchiveDirectoryContent archive, string fullPath, bool contentCaching)
7878
{
79+
_contentCaching = contentCaching;
7980
_entry = archive.GetEntry(fullPath) ?? throw new InvalidOperationException("No such ZipArchive entry");
8081
}
8182

@@ -85,22 +86,30 @@ public MappedZipEntryFile(MappedZipArchiveDirectoryContent archive, string fullP
8586
public ulong ModificationTime { get; set; }
8687
public ulong ChangeTime { get; set; }
8788

89+
private readonly bool _contentCaching;
8890
private readonly ZipArchiveEntry _entry;
8991
private byte[]? _decompressedCache;
9092

9193
public ulong Size => checked((ulong)_entry.Length);
9294

9395
private byte[] GetDecompressedContent()
9496
{
95-
if (_decompressedCache == null)
96-
{
97-
using var stream = _entry.Open();
98-
_decompressedCache = new byte[_entry.Length];
99-
var output = new MemoryStream(_decompressedCache);
100-
stream.CopyTo(output);
101-
}
97+
var content = _decompressedCache ?? Decompress();
98+
99+
if (_contentCaching)
100+
_decompressedCache = content;
101+
102+
return content;
103+
}
104+
105+
private byte[] Decompress()
106+
{
107+
using var stream = _entry.Open();
108+
var content = new byte[_entry.Length];
109+
var output = new MemoryStream(content);
110+
stream.CopyTo(output);
102111

103-
return _decompressedCache;
112+
return content;
104113
}
105114

106115
public IFilesystemEntry ToInMemory()

Wazzy/Wazzy.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
88
<Title>Wazzy</Title>
9-
<Version>0.0.38</Version>
9+
<Version>0.0.39</Version>
1010
<Authors>Martin Evans</Authors>
1111
<Description>A pure C# implementation of WASI</Description>
1212
<PackageProjectUrl>https://github.com/martindevans/Wazzy</PackageProjectUrl>

0 commit comments

Comments
 (0)