Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="SharpCompress" Version="0.37.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Calamari.ConsolidateCalamariPackages.Api\Calamari.ConsolidateCalamariPackages.Api.csproj" />
Expand Down
25 changes: 10 additions & 15 deletions source/Calamari.ConsolidateCalamariPackages/ConsolidatedPackage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Octopus.Calamari.ConsolidatedPackage.Api;
using SharpCompress.Archives.Zip;

namespace Octopus.Calamari.ConsolidatedPackage
{
Expand All @@ -28,21 +28,16 @@ public ConsolidatedPackage(IConsolidatedPackageStreamProvider packageStreamProvi
throw new Exception($"Could not find platform {platform} for {calamariFlavour}");
}

using (var sourceStream = packageStreamProvider.OpenStream())
var platformFilesLookup = platformFiles.ToDictionary(f => f.Source);

using var sourceStream = packageStreamProvider.OpenStream();
using var source = new ZipArchive(sourceStream, ZipArchiveMode.Read);
foreach (var sourceEntry in source.Entries)
{
using (var source = ZipArchive.Open(sourceStream))
{
foreach (var fileTransfer in platformFiles)
{
var sourceEntry = source.Entries.FirstOrDefault(e => e.Key is not null && e.Key.Equals(fileTransfer.Source));
Copy link
Copy Markdown
Contributor Author

@borland borland May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested loop means that if there were thousands of PlatformFiles and thousands of Entries, we'd end up doing millions of string-equals checks which can take meaningful amounts of time.

The numbers are probably smaller than that, and it's probably not a big deal, but why not pick the low-hanging fruit while we're in here 🤷

Also: The new code iterates the entries once in a forward-only direction, which is much more friendly to things like disk caches. Iterating in platformfiles order could potentially have us jumping around randomly. In the modern days of SSD's, random jumping around isn't so bad, but sequential reads are still better than random ones

if(sourceEntry is null) continue;

using (var sourceEntryStream = sourceEntry.OpenEntryStream())
{
yield return (fileTransfer.Destination, sourceEntry.Size, sourceEntryStream);
}
}
}
if (!platformFilesLookup.TryGetValue(sourceEntry.FullName, out var fileTransfer)) continue;

using var sourceEntryStream = sourceEntry.Open();
yield return (fileTransfer.Destination, sourceEntry.Length, sourceEntryStream);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using System;
using System.IO;
using System.Linq;
using System.IO.Compression;
using Newtonsoft.Json;
using Octopus.Calamari.ConsolidatedPackage.Api;
using SharpCompress.Archives.Zip;

namespace Octopus.Calamari.ConsolidatedPackage
{
public class ConsolidatedPackageIndexLoader
{
public IConsolidatedPackageIndex Load(Stream zipStream)
{
using var zip = ZipArchive.Open(zipStream);
var entry = zip.Entries.First(e => e.Key == "index.json");
using var zip = new ZipArchive(zipStream, ZipArchiveMode.Read);
var entry = zip.GetEntry("index.json");
if (entry == null)
{
throw new Exception($"index.json not found in supplied stream.");
}

using var entryStream = entry.OpenEntryStream();
using var entryStream = entry.Open();
return From(entryStream);
}

Expand Down