Skip to content

Commit c47d8ca

Browse files
[BcNuGet] Reduce memory consumption, improve performance, prevent memory leaks (#3760)
We found that `Compress-Archive` and especially storing the `octet-stream` inside a string instead of using a Stream causes a very high memory consumption and a bad performance. We're invoking the BcNuGet Cmdlets from our own C# API and therefore were able to profile the memory and performance. The behaviour however is similar when invoking the Cmdlets directly (PowerShell Core 7.4.6). **Memory (Note: around 340 MB is the base load our API brings with it)** Before: Memory spikes when uploading big apps, around 1 GB memory consumption from BcNuGet Cmdlets <img width="502" alt="image" src="https://github.com/user-attachments/assets/81d81377-304f-459f-a8b3-dd1fbe187b8c"> After: <img width="497" alt="image" src="https://github.com/user-attachments/assets/6bb2fcd3-0889-4619-b333-341b200050b8"> **Performance** Before: <img width="516" alt="image" src="https://github.com/user-attachments/assets/62747a1e-751a-4b89-bd62-adcd347d31a0"> After: <img width="526" alt="image" src="https://github.com/user-attachments/assets/be427969-c7cc-4bf4-a05d-43894163998b">
1 parent a399bbd commit c47d8ca

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

NuGet/New-BcNuGetPackage.ps1

+5-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ Function New-BcNuGetPackage {
286286
if (Test-Path $nuPkgFile -PathType Leaf) {
287287
Remove-Item $nupkgFile -Force
288288
}
289-
Compress-Archive -Path "$rootFolder\*" -DestinationPath "$nupkgFile.zip" -Force
289+
if (Test-Path "$nupkgFile.zip" -PathType Leaf) {
290+
Remove-Item "$nupkgFile.zip" -Force
291+
}
292+
Add-Type -AssemblyName System.IO.Compression.FileSystem
293+
[System.IO.Compression.ZipFile]::CreateFromDirectory("$rootFolder", "$nupkgFile.zip", [System.IO.Compression.CompressionLevel]::Optimal, $false)
290294
Rename-Item -Path "$nupkgFile.zip" -NewName $nuPkgFileName
291295

292296
$size = (Get-Item $nupkgFile).Length

NuGet/NuGetFeedClass.ps1

+11-9
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,19 @@ class NuGetFeed {
353353
"X-NuGet-ApiKey" = $this.token
354354
"X-NuGet-Client-Version" = "6.3.0"
355355
}
356-
$FileContent = [System.IO.File]::ReadAllBytes($package)
357-
$boundary = [System.Guid]::NewGuid().ToString();
356+
$boundary = [System.Guid]::NewGuid().ToString();
358357
$LF = "`r`n";
359-
360-
$body = [System.Text.Encoding]::UTF8.GetBytes("--$boundary$LF")
361-
$body += [System.Text.Encoding]::UTF8.GetBytes("Content-Type: application/octet-stream$($LF)Content-Disposition: form-data; name=package; filename=""$([System.IO.Path]::GetFileName($package))""$($LF)$($LF)")
362-
$body += $fileContent
363-
$body += [System.Text.Encoding]::UTF8.GetBytes("$LF--$boundary--$LF")
364-
365358
$tmpFile = Join-Path ([System.IO.Path]::GetTempPath()) ([GUID]::NewGuid().ToString())
366-
[System.IO.File]::WriteAllBytes($tmpFile, $body)
359+
$fs = [System.IO.File]::OpenWrite($tmpFile)
360+
try {
361+
$fs.Write([System.Text.Encoding]::UTF8.GetBytes("--$boundary$LF"))
362+
$fs.Write([System.Text.Encoding]::UTF8.GetBytes("Content-Type: application/octet-stream$($LF)Content-Disposition: form-data; name=package; filename=""$([System.IO.Path]::GetFileName($package))""$($LF)$($LF)"))
363+
$fs.Write([System.IO.File]::ReadAllBytes($package))
364+
$fs.Write([System.Text.Encoding]::UTF8.GetBytes("$LF--$boundary--$LF"))
365+
} finally {
366+
$fs.Close()
367+
}
368+
367369
Write-Host "Submitting NuGet package"
368370
try {
369371
Invoke-RestMethod -UseBasicParsing -Uri $this.packagePublishUrl -ContentType "multipart/form-data; boundary=$boundary" -Method Put -Headers $headers -inFile $tmpFile | Out-Host

ReleaseNotes.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
6.0.29
22
Issue 3591 When using Publish-NAVApp to publish an app, which fails compilation in the service, the command might hang forever - the fix for this is a temporary hack put in place for the versions which doesn't work.
3+
Improve performance and reduce memory consumption when creating and pushing NuGet packages
34

45
6.0.28
56
Set useApproximateVersion to true in settings to increase performance of Get-BcArtifactUrl when selecting latest artifact, by using an approximate filtering on blobs

0 commit comments

Comments
 (0)